File Coverage

blib/lib/OTRS/OPM/Installer/Utils/OTRS.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package OTRS::OPM::Installer::Utils::OTRS;
2             $OTRS::OPM::Installer::Utils::OTRS::VERSION = '0.02';
3             # ABSTRACT: class that provides helper functionality regarding the OTRS installation
4              
5 2     2   130064 use strict;
  2         26  
  2         74  
6 2     2   15 use warnings;
  2         5  
  2         65  
7              
8 2     2   12 use Carp;
  2         5  
  2         108  
9 2     2   753 use Moo;
  2         20910  
  2         12  
10 2     2   3440 use Module::Path qw/module_path/;
  2         1203  
  2         117  
11 2     2   735 use Types::Standard qw(ArrayRef Str);
  2         152615  
  2         27  
12              
13 2     2   3042 use OTRS::OPM::Installer::Types qw(OTRSVersion);
  2         8  
  2         75  
14 2     2   630 use OTRS::OPM::Installer::Utils::File;
  0            
  0            
15              
16             has path => ( is => 'ro' );
17             has obj_env => ( is => 'ro', lazy => 1, default => \&_obj_env );
18             has os_env => ( is => 'ro', lazy => 1, default => \&_os_env );
19             has otrs_version => ( is => 'rwp', lazy => 1, default => \&_find_version);#isa => OTRSVersion );
20             has inc => ( is => 'rwp', lazy => 1, default => \&_build_inc );#isa => ArrayRef[Str] );
21             has manager => ( is => 'rwp', lazy => 1, default => \&_build_manager );#isa => Object );
22             has db => ( is => 'rwp', lazy => 1, default => \&_get_db ); #sub { my $class = $self->obj_env; my $string = $class . '::_get_db'; $self->$string(); } );#isa => Object );
23              
24             sub is_installed {
25             my ($self, %param) = @_;
26              
27             my $sql = 'SELECT name, version FROM package_repository WHERE name = ?';
28              
29             return if !$self->db;
30              
31             $self->db->Prepare(
32             SQL => $sql,
33             Bind => [ \$param{package} ],
34             );
35              
36             my %info;
37             while ( my @row = $self->db->FetchrowArray() ) {
38             %info = (
39             name => $row[0],
40             version => $row[1],
41             );
42             }
43              
44             return if !%info;
45              
46             my $is_installed = $self->_check_version(
47             installed => $info{version},
48             requested => $param{version},
49             );
50              
51             return 1 if $is_installed;
52             return;
53             }
54              
55             sub _check_version {
56             my ($self, %param) = @_;
57              
58             my @i_parts = split /\./, $param{installed} || 0;
59             my @r_parts = split /\./, $param{requested} || 10000000;
60              
61             my $installed = sprintf "%03d%03d%03d", map{ $i_parts[$_] && $i_parts[$_] =~ m{\A[0-9]+\z} ? $i_parts[$_] : 0 }( 0 .. 2);
62             my $requested = sprintf "%03d%03d%03d", map{ $r_parts[$_] && $r_parts[$_] =~ m{\A[0-9]+\z} ? $r_parts[$_] : 0 }( 0 .. 2);
63              
64             return $installed >= $requested;
65             }
66              
67             sub _get_db {
68             my ($self) = @_;
69              
70             my $class = $self->obj_env;
71             my $path = module_path $class;
72             require $path;
73              
74             my $string = $class . '::_get_db';
75              
76             $self->$string();
77             }
78            
79             sub _build_manager {
80             my ($self) = @_;
81              
82             my $class = $self->obj_env;
83             my $path = module_path $class;
84             require $path;
85              
86             my $string = $class . '::_build_manager';
87              
88             $self->$string();
89             }
90            
91             sub _find_version {
92             my ($self) = @_;
93              
94             my $file = $self->path . '/RELEASE';
95             my $content = do { local ( @ARGV, $/ ) = $file; <> };
96              
97             my ($version) = $content =~ m{VERSION \s+ = \s+ ([0-9.]+)}xms;
98             return $version;
99             }
100              
101             sub _build_inc {
102             my ($self) = @_;
103              
104             return [ map{ $self->path . "/" . $_ }( '', 'Kernel/cpan-lib' ) ];
105             }
106              
107             sub BUILDARGS {
108             my $class = shift;
109              
110             if ( @_ % 2 != 0 ) {
111             croak 'Check the parameters for ' . __PACKAGE__ . '. You have to pass a hash.';
112             }
113              
114             my %args = @_;
115             if ( !exists $args{path} ) {
116             my $utils = OTRS::OPM::Installer::Utils::Config->new;
117             my $cfg = $utils->rc_config;
118              
119             $args{path} = $cfg->{otrs_path} if defined $cfg->{otrs_path};
120             }
121              
122             return \%args;
123             }
124              
125             sub _obj_env {
126             my ($self) = @_;
127              
128             my ($major) = $self->otrs_version =~ m{\A(\d+)\.};
129             if ( $major <= 3 ) {
130             return 'OTRS::OPM::Installer::Utils::OTRS::OTRS3';
131             }
132             else {
133             return 'OTRS::OPM::Installer::Utils::OTRS::OTRS4';
134             }
135             }
136              
137             sub _os_env {
138             if ( $ENV{OTRSOPMINSTALLERTEST} ) {
139             return 'OTRS::OPM::Installer::Utils::OTRS::Test';
140             }
141             else {
142             return 'OTRS::OPM::Installer::Utils::OTRS::Linux';
143             }
144             }
145              
146             1;
147              
148             __END__
149              
150             =pod
151              
152             =encoding UTF-8
153              
154             =head1 NAME
155              
156             OTRS::OPM::Installer::Utils::OTRS - class that provides helper functionality regarding the OTRS installation
157              
158             =head1 VERSION
159              
160             version 0.02
161              
162             =head1 AUTHOR
163              
164             Renee Baecker <reneeb@cpan.org>
165              
166             =head1 COPYRIGHT AND LICENSE
167              
168             This software is Copyright (c) 2017 by Renee Baecker.
169              
170             This is free software, licensed under:
171              
172             The Artistic License 2.0 (GPL Compatible)
173              
174             =cut