OpenInteract2/Config/Package.pm

filter: Statementstmt 90.0% 145 / 161 Branchbran 68.5% 37 / 54 Conditioncond 66.6% 16 / 24 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 95.4% 21 / 22 Podpod 10.0% 1 / 10 totaltotal 79.3% 226 / 285 CC 45 SCAR 19.9
CC
45
Cov
83%
check_required_fields
13.7
new
12.1
_read_config
11.7
CRAP 7.3
« OpenInteract2/Setup/RegisterActionTypes.pm ↑ Summary OpenInteract2/Response/Apache2.pm »
1 package OpenInteract2::Config::Package;
2  
3 # $Id: Package.pm,v 1.24 2005/03/17 14:58:00 sjn Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use OpenInteract2::Config::Ini;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use File::Spec::Functions qw( catfile rel2abs );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 87 $OpenInteract2::Config::Package::VERSION = sprintf("%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/);
15  
16 87 my ( $log );
17  
18 87 ▶use constant DEFAULT_FILENAME => 'package.ini';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 87 my @REQUIRED_FIELDS = qw( name version );
21 0 ▶sub get_required_fields { return [ @REQUIRED_FIELDS ] }
Subroutine get_required_fields: not called CC=1 SCAR=6.9
Pod undocumented
22  
23 # Fields in our package/configuration. These are ordered --
24 # save_config() will output to the file in this order.
25  
26 87 my @SERIAL_FIELDS = qw( name version author url
27 spops_file action_file message_file module
28 sql_installer template_plugin
29 observer config_watcher description );
30 87 my @OBJECT_FIELDS = qw( filename package_dir );
31  
32 # Define the keys in 'package.ini' that can be a list, meaning you
33 # can have multiple items defined:
34 #
35 # author = Larry Wall <larry@wall.org>
36 # author = Chris Winters E<lt>chris@cwinters.comE<gt>
37  
38 # NOTE: If you add a field here you must also add it to @SERIAL_FIELDS
39  
40 87 my %LIST_FIELDS = map { $_ => 1 } qw( author module spops_file action_file
41 message_file config_watcher );
42  
43 # Define the keys in 'package.ini' that should be a hash
44 #
45 # [package template_plugin]
46 # MyNew = OpenInteract2::TT::Plugin::New
47 # MyURL = OpenInteract2::TT::Plugin::URL
48  
49 # NOTE: If you add a field here you must also add it to @SERIAL_FIELDS
50  
51 87 my %HASH_FIELDS = map { $_ => 1 } qw( template_plugin observer );
52  
53 87 my @FIELDS = ( @SERIAL_FIELDS, @OBJECT_FIELDS );
54 87 OpenInteract2::Config::Package->mk_accessors( @FIELDS );
55  
56 ########################################
57 # CLASS METHODS
58  
59 10 ▶sub new {
Subroutine new: called CC=11 SCAR=25.0
Pod documented
60 10 my ( $class, $params ) = @_;
61 10 ▶ $log ||= get_logger( LOG_OI );
Condition66%2/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
640
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table66%2/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
62 10 my $self = bless( {}, $class );
63 10 my $filename = $params->{filename};
64 10 ▶ my $directory = $params->{package_dir} || $params->{directory};
Condition66%2/3
$params->{'package_dir'} || $params->{'directory'}
l!l&&r!l&&!r
073
MC/DC50%1/2
$params->{'package_dir'} || $params->{'directory'}
$params->{'package_dir'} $params->{'directory'}
Truth table66%2/3
A || B
A= $params->{'package_dir'}B= $params->{'directory'}
ABresult
1X1
011
000
65 10 ▶ if ( ! $filename and $directory ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $filename and $directory) 7 3 10
Condition100%3/3
not $filename and $directory
!ll&&!rl&&r
217
MC/DC100%2/2
not $filename and $directory
not $filename $directory
Truth table100%3/3
not(A) and B
A= not $filenameB= $directory
ABresult
0X0
100
111
66 7 $filename = $self->create_filename( $directory );
67 7 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Will read package config from '${filename}' ", "given directory '${directory}'") 0 7 7
68 $log->debug( "Will read package config from '$filename' ",
69 "given directory '$directory'" );
70 }
71 10 ▶ if ( $filename and -f $filename ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename and -f $filename) { } 9 1 10
elsif ($params->{'content'}) { } 0 1 1
Condition66%2/3
$filename and -f $filename
!ll&&!rl&&r
109
MC/DC50%1/2
$filename and -f $filename
$filename -f $filename
Truth table66%2/3
A and B
A= $filenameB= -f $filename
ABresult
0X0
100
111
72 9 my $new_params = $class->_read_config( file => $filename );
73 9 $params->{ $_ } = $new_params->{ $_ } for ( keys %{ $new_params } );
74 9 $self->filename( $filename );
75 9 $self->package_dir( rel2abs( dirname( $filename ) ) );
76 }
77 elsif ( $params->{content} ) {
78 0 my $new_params = $class->_read_config( content => $params->{content} );
79 0 $params->{ $_ } = $new_params->{ $_ } for ( keys %{ $new_params } );
80  
81 }
82 10 return $self->_initialize( $params );
83 }
84  
85 7 ▶sub create_filename {
Subroutine create_filename: called CC=2 SCAR=7.6
Pod undocumented
86 7 my ( $class, $dir ) = @_;
87 7 ▶ unless ( $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir) 0 7 7
88 0 oi_error "Must pass in directory to create package config filename";
89 }
90 7 return catfile( $dir, DEFAULT_FILENAME );
91 }
92  
93  
94 ########################################
95 # OBJECT METHODS
96  
97 10 ▶sub _initialize {
Subroutine _initialize: called CC=3 SCAR=11.0
98 10 my ( $self, $params ) = @_;
99 10 for ( @FIELDS ) {
100 150 ▶ next unless ( $params->{ $_ } );
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$_} 80 70 150
101 70 $self->{ $_ } = $params->{ $_ };
102 }
103 10 return $self;
104 }
105  
106  
107 2 ▶sub author_names {
Subroutine author_names: called CC=1 SCAR=0.0
Pod undocumented
108 2 my ( $self ) = @_;
109 2 my ( $names, $emails ) = $self->_parse_authors;
110 2 return @{ $names };
111 }
112  
113 2 ▶sub author_emails {
Subroutine author_emails: called CC=1 SCAR=0.0
Pod undocumented
114 2 my ( $self ) = @_;
115 2 my ( $names, $emails ) = $self->_parse_authors;
116 2 return @{ $emails };
117 }
118  
119 4 ▶sub _parse_authors {
Subroutine _parse_authors: called CC=4 SCAR=13.9
120 4 my ( $self ) = @_;
121 4 my $authors = $self->author;
122 4 ▶ return ( [], [] ) unless ( ref $authors );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $authors 0 4 4
123 4 my @names = ();
124 4 my @emails = ();
125 4 foreach my $author ( @{ $authors } ) {
126 8 my ( $name, $d1, $email, $d2 ) = $author =~ /^([\w\s]+)(\(|<)\s*(.*)\s*(\)|>)\s*$/;
127 # ...they didn't put an email in
128 8 ▶ unless ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 2 6 8
129 2 $name = $author;
130 2 $email = '';
131 }
132 8 $name =~ s/^\s+//;
133 8 $name =~ s/\s+$//;
134 8 push @names, $name;
135 8 push @emails, $email;
136 }
137 4 return ( \@names, \@emails );
138 }
139  
140 3 ▶sub get_spops_files {
Subroutine get_spops_files: called CC=3 SCAR=11.0
Pod undocumented
141 3 my ( $self ) = @_;
142 3 my $spops_files = $self->spops_file;
143 3 my $dir = $self->package_dir;
144 3 ▶ unless ( ref $spops_files eq 'ARRAY' and scalar @{ $spops_files } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $spops_files eq "ARRAY" and scalar @{$spops_files;}) 2 1 3
Condition100%2/2
ref $spops_files eq "ARRAY" and scalar @{$spops_files;}
!ll
21
MC/DC100%1/1
ref $spops_files eq "ARRAY" and scalar @{$spops_files;}
ref $spops_files eq "ARRAY"
Truth table100%2/2
A
A= ref $spops_files eq "ARRAY"
Aresult
00
11
145 2 return [];
146 }
147 1 return $spops_files;
148 }
149  
150 3 ▶sub get_action_files {
Subroutine get_action_files: called CC=3 SCAR=11.0
Pod undocumented
151 3 my ( $self ) = @_;
152 3 my $action_files = $self->action_file;
153 3 my $dir = $self->package_dir;
154 3 ▶ unless ( ref $action_files eq 'ARRAY' and scalar @{ $action_files } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $action_files eq "ARRAY" and scalar @{$action_files;}) 2 1 3
Condition100%2/2
ref $action_files eq "ARRAY" and scalar @{$action_files;}
!ll
21
MC/DC100%1/1
ref $action_files eq "ARRAY" and scalar @{$action_files;}
ref $action_files eq "ARRAY"
Truth table100%2/2
A
A= ref $action_files eq "ARRAY"
Aresult
00
11
155 2 return [];
156 }
157 1 return $action_files;
158 }
159  
160 6 ▶sub get_message_files {
Subroutine get_message_files: called CC=3 SCAR=12.3
Pod undocumented
161 6 my ( $self ) = @_;
162 6 my $message_files = $self->message_file;
163 6 my $dir = $self->package_dir;
164 0 ▶ unless ( ref $message_files eq 'ARRAY' and scalar @{ $message_files } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $message_files eq "ARRAY" and scalar @{$message_files;}) 6 0 6
Condition50%1/2
ref $message_files eq "ARRAY" and scalar @{$message_files;}
!ll
60
MC/DC0%0/1
ref $message_files eq "ARRAY" and scalar @{$message_files;}
ref $message_files eq "ARRAY"
Truth table50%1/2
A
A= ref $message_files eq "ARRAY"
Aresult
00
11
165 6 return [];
166 }
167 0 return $message_files;
168 }
169  
170 13 ▶sub check_required_fields {
Subroutine check_required_fields: called CC=10 SCAR=26.2
Pod undocumented
171 13 my ( $self, @check_fields ) = @_;
172  
173 13 my @all_check_fields = ( @check_fields, @REQUIRED_FIELDS );
174  
175 # First ensure that required fields are set
176  
177 13 my @empty_fields = ();
178 13 foreach my $field ( @all_check_fields ) {
179 32 my $value = $self->$field();
180 32 ▶ if ( $LIST_FIELDS{ $field } ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($LIST_FIELDS{$field}) { } 6 26 32
elsif ($HASH_FIELDS{$field}) { } 0 26 26
181 6 ▶ unless ( ref $value eq 'ARRAY' and scalar @{ $value } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "ARRAY" and scalar @{$value;}) 0 6 6
Condition50%1/2
ref $value eq "ARRAY" and scalar @{$value;}
!ll
06
MC/DC0%0/1
ref $value eq "ARRAY" and scalar @{$value;}
ref $value eq "ARRAY"
Truth table50%1/2
A
A= ref $value eq "ARRAY"
Aresult
00
11
182 0 push @empty_fields, $field;
183 }
184 }
185 elsif ( $HASH_FIELDS{ $field } ) {
186 0 ▶ unless ( ref $value eq 'HASH' and scalar keys %{ $value } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "HASH" and scalar keys %{$value;}) 0 0 0
Condition0%0/2
ref $value eq "HASH" and scalar keys %{$value;}
!ll
00
MC/DC0%0/1
ref $value eq "HASH" and scalar keys %{$value;}
ref $value eq "HASH"
Truth table0%0/2
A
A= ref $value eq "HASH"
Aresult
00
11
187 0 push @empty_fields, $field;
188 }
189 }
190 else {
191 26 ▶ unless ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 1 25 26
192 1 push @empty_fields, $field;
193 }
194 }
195 }
196  
197 13 ▶ if ( scalar @empty_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @empty_fields) 1 12 13
198 1 oi_error "Required fields check failed: the following fields ",
199 "must be defined: ", join( ", ", @empty_fields );
200 }
201 12 return 1;
202 }
203  
204  
205 1 ▶sub save_config {
Subroutine save_config: called CC=6 SCAR=18.1
Pod undocumented
206 1 my ( $self ) = @_;
207  
208 1 ▶ unless ( $self->filename() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->filename) 0 1 1
209 0 oi_error "Package configuration save failed: set filename first";
210 }
211 1 $self->check_required_fields;
212  
213 1 my $ini = OpenInteract2::Config::Ini->new();
214 1 foreach my $field ( @SERIAL_FIELDS ) {
215 13 ▶ if ( $HASH_FIELDS{ $field } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HASH_FIELDS{$field}) { } 2 11 13
216 2 ▶ my $values = $self->$field() || {};
Condition50%1/2
$self->$field || {}
l!l
20
MC/DC0%0/1
$self->$field || {}
$self->$field
Truth table50%1/2
A
A= $self->$field
Aresult
11
00
217 2 for ( keys %{ $values } ) {
218 2 $ini->set( 'package', $field, $_, $values->{ $_ } );
219 }
220 }
221 else {
222 11 $ini->set( 'package', $field, $self->$field() );
223 }
224 }
225 1 $ini->write_file( $self->filename() );
226  
227 1 return $self->filename;
228 }
229  
230 9 ▶sub _read_config {
Subroutine _read_config: called CC=9 SCAR=24.6
231 9 my ( $class, $type, $value ) = @_;
232 9 my %ini_conf = ();
233 9 ▶ if ( $type eq 'file' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'file') { } 9 0 9
elsif ($type eq 'content') { } 0 0 0
234 9 ▶ unless ( -f $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $value) 0 9 9
235 0 oi_error "Package configuration file '$value' does not exist.";
236 }
237 9 $ini_conf{filename} = $value;
238 }
239 elsif ( $type eq 'content' ) {
240 0 ▶ unless ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 0 0 0
241 0 oi_error "No content to use for package configuration";
242 }
243 0 $ini_conf{content} = $value;
244 }
245  
246 9 my $ini = OpenInteract2::Config::Ini->new( \%ini_conf );
247 9 ▶ return {} unless ( $ini->{package} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ini->{"package"} 0 9 9
248 9 my %params = ();
249 9 ▶ while ( my ( $key, $value ) = each %{ $ini->{package} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$ini{"package"};}) 69 9 78
250 69 ▶ if ( $LIST_FIELDS{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIST_FIELDS{$key}) { } 20 49 69
251 20 ▶ $params{ $key } = ( ref $value eq 'ARRAY' ) ? $value : [ $value ];
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 2 18 20
252 }
253 else {
254 49 $params{ $key } = $value;
255 }
256 }
257 9 return \%params;
258 }
259  
260  
261 87 1;
262  
263 __END__
« OpenInteract2/Setup/RegisterActionTypes.pm ↑ Summary OpenInteract2/Response/Apache2.pm »