Connector.pm

filter: Statementstmt 63.5% 101 / 159 Branchbran 51.7% 29 / 56 Conditioncond 45.0% 9 / 20 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 55.1% 16 / 29 Podpod 100% 10 / 10 totaltotal 59.0% 169 / 286 CC 38 SCAR 30.9
CC
38
Cov
59%
exists
110.0
__ANON__
12.4
get_keys
12.0
CRAP 21.9
« Connector/Proxy/Authentication/KeyNanny.pm ↑ Summary Connector/Builtin/File/Path.pm »
1 # Connector
2 #
3 # A generic abstraction for accessing information.
4 #
5 # Written by Scott Hardin, Martin Bartosch and Oliver Welter for the OpenXPKI project 2012
6 #
7 package Connector;
8  
9 # Feature::Compat::Try requires 5.14; below that it falls back to
10 # Syntax::Keyword::Try, which also requires 5.14. Sub-modules additionally
11 # use the '//' operator, which needs 5.10.
12 23 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 23 our $VERSION = '1.56';
15  
16 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 23 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 23 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 23 ▶use Connector::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 23 has LOCATION => (
27 is => 'ro',
28 isa => 'Connector::Types::Location',
29 required => 1,
30 );
31  
32 # In order to clear the prefix, call the accessor with undef as argument
33 has PREFIX => (
34 is => 'rw',
35 isa => 'Connector::Types::Key|ArrayRef|Undef',
36 # build and store an array of the prefix in _prefix_path
37 18 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
38 18 my ($self, $prefix, $old_prefix) = @_;
39 18 ▶ if (defined $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix) { } 16 2 18
40 16 my @path = $self->_build_path($prefix);
41 16 $self->__prefix_path(\@path);
42 } else {
43 2 $self->__prefix_path([]);
44 }
45 }
46 23 );
47  
48 23 has DELIMITER => (
49 is => 'rw',
50 isa => 'Connector::Types::Char',
51 default => '.',
52 );
53  
54 23 has RECURSEPATH => (
55 is => 'rw',
56 isa => 'Bool',
57 default => '0',
58 );
59  
60 # internal representation of the instance configuration
61 # NB: this should be a private variable and not accessible from outside
62 # an instance.
63 # TODO: figure out how to protect it.
64 23 has _config => (
65 is => 'rw',
66 lazy => 1,
67 init_arg => undef, # not settable via constructor
68 builder => '_build_config',
69 );
70  
71 23 has log => (
72 is => 'rw',
73 lazy => 1,
74 init_arg => undef, # not settable via constructor
75 builder => '_build_logger',
76 );
77  
78  
79 # this instance variable is set in the trigger function of PREFIX.
80 # it contains an array representation of PREFIX (assumed to be delimited
81 # by the DELIMITER character)
82 has _prefix_path => (
83 is => 'rw',
84 isa => 'ArrayRef',
85 init_arg => undef,
86 21 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
87 23 writer => '__prefix_path',
88 lazy => 1
89 );
90  
91 # weather to die on undef or just fail silently
92 # implemented in _node_not_exists
93 23 has 'die_on_undef' => (
94 is => 'rw',
95 isa => 'Bool',
96 default => 0,
97 );
98  
99  
100 # This is the foo that allows us to just milk the connector config from
101 # the settings fetched from another connector.
102  
103 52 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=12 SCAR=25.2
104 52 my $orig = shift;
105 52 my $class = shift;
106  
107 52 my $args = $_[0];
108  
109 52 ▶ if ( ref($args) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH" and defined $args->{"CONNECTOR"} and defined $args->{"TARGET"}) 7 45 52
Condition83%5/6
ref $args eq "HASH" and defined $args->{"CONNECTOR"} and defined $args->{"TARGET"}
!ll&&!rl&&r
4507
ref $args eq "HASH" and defined $args->{"CONNECTOR"}
!ll&&!rl&&r
3427
MC/DC66%2/3
ref $args eq "HASH" and defined $args->{"CONNECTOR"} and defined $args->{"TARGET"}
ref $args eq "HASH" defined $args->{"CONNECTOR"} defined $args->{"TARGET"}
Truth table75%3/4
A and B and C
A= ref $args eq "HASH"B= defined $args->{"CONNECTOR"}C= defined $args->{"TARGET"}
ABCresult
0XX0
10X0
1100
1111
110 && defined($args->{CONNECTOR})
111 && defined($args->{TARGET}) ) {
112  
113 7 my $conn = $args->{CONNECTOR};
114 7 delete $args->{CONNECTOR};
115  
116 7 my @targ = $conn->_build_path( $args->{TARGET} );
117 7 delete $args->{TARGET};
118  
119 7 my $meta = $class->meta;
120  
121 7 my $log = $conn->log(); # Logs to the parent that is initialising us
122 7 $log->trace( 'Wrapping connector - config at ' . join ".", @targ ) ;
123  
124 7 for my $attr ( $meta->get_all_attributes ) {
125 74 my $attrname = $attr->name();
126 74 ▶ next if $attrname =~ m/^_/; # skip apparently internal params
Branch: 1 branch
BranchTrueFalseTotal
if $attrname =~ /^_/u 14 60 74
127 # allow caller to override params in CONNECTOR
128 60 ▶ if ( not exists($args->{$attrname}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{$attrname}) 59 1 60
129 59 my $meta = $conn->get_meta( [ @targ , $attrname ] );
130 59 $log->trace( ' Check for ' . $attrname . ' - meta is ' . Dumper $meta );
131 59 ▶ next unless($meta && $meta->{TYPE});
Branch: 1 branch
BranchTrueFalseTotal
unless $meta and $meta->{"TYPE"} 47 12 59
Condition66%2/3
$meta and $meta->{"TYPE"}
!ll&&!rl&&r
47012
MC/DC50%1/2
$meta and $meta->{"TYPE"}
$meta $meta->{"TYPE"}
Truth table66%2/3
A and B
A= $metaB= $meta->{"TYPE"}
ABresult
0X0
100
111
132 12 ▶ if ($meta->{TYPE} eq 'scalar') {
Branch: 3 branches
BranchTrueFalseTotal
if ($meta->{'TYPE'} eq 'scalar') { } 11 1 12
elsif ($meta->{'TYPE'} eq 'list') { } 1 0 1
elsif ($meta->{'TYPE'} eq 'hash') { } 0 0 0
133 11 $args->{$attrname} = $conn->get( [ @targ , $attrname ] );
134 } elsif ($meta->{TYPE} eq 'list') {
135 1 my @tmp = $conn->get_list( [ @targ , $attrname ] );
136 1 $args->{$attrname} = \@tmp;
137 } elsif ($meta->{TYPE} eq 'hash') {
138 0 $args->{$attrname} = $conn->get_hash( [ @targ , $attrname ] );
139 } else {
140 0 $log->warn( ' Unexpected type '.$meta->{TYPE}.' for attribute ' . $attrname );
141 }
142 }
143 }
144  
145 7 $log->trace( 'Wrapping connector - arglist ' .Dumper \@_ );
146 }
147 52 return $class->$orig(@_);
148 23 };
149  
150  
151 # subclasses must implement this to initialize _config
152 0 ▶sub _build_config { return; };
Subroutine _build_config: not called CC=1 SCAR=6.9
153  
154 45 ▶sub _build_logger {
Subroutine _build_logger: called CC=1 SCAR=0.0
155  
156 45 return Log::Log4perl->get_logger("connector");
157  
158 };
159  
160  
161 # helper function: build a path from the given input. does not take PREFIX
162 # into account
163 729 ▶sub _build_path {
Subroutine _build_path: called CC=9 SCAR=22.4
164  
165 729 my $self = shift;
166 729 my @arg = @_;
167  
168 729 my @path;
169  
170  
171 # Catch old call format
172 729 ▶ if (scalar @arg > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arg > 1) 0 729 729
173 0 die "Sorry, we changed the API (pass scalar or array ref but not array)";
174 }
175  
176 729 my $location = shift @arg;
177  
178 729 ▶ if (not $location) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $location) { } 17 712 729
elsif (ref $location eq '') { } 371 341 712
elsif (ref $location ne 'ARRAY') { } 0 341 341
elsif ($self->RECURSEPATH) { } 4 337 341
179 17 @path = ();
180 } elsif (ref $location eq '') {
181 # String path - split at delimiter
182 371 my $delimiter = $self->DELIMITER();
183 371 @path = split(/[$delimiter]/, $location);
184 } elsif (ref $location ne "ARRAY") {
185 # Nothing else than arrays allowed beyond this point
186 0 die "Invalid data type passed in argument to _build_path";
187 } elsif ($self->RECURSEPATH()) {
188 4 foreach my $item (@{$location}) {
189 8 push @path, $self->_build_path( $item );
190 }
191 } else {
192 # Atomic path, the array is the result
193 337 @path = @{$location};
194 }
195  
196 729 $self->log()->trace( 'path created ' . Dumper \@path );
197  
198 729 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 728 1 729
elsif ($self->RECURSEPATH) { } 1 0 1
199 728 return @path;
200 } elsif ($self->RECURSEPATH()) {
201 1 return join $self->DELIMITER(), @path;
202 } else {
203 0 die "Sorry, we changed the API, request a list and join yourself or set RECURSEPATH in constructor";
204 }
205  
206 }
207  
208 # same as _build_config, but prepends PREFIX
209 589 ▶sub _build_path_with_prefix {
Subroutine _build_path_with_prefix: called CC=2 SCAR=6.9
210 589 my $self = shift;
211 589 my $location = shift;
212  
213 589 ▶ if (not $location) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $location) { } 20 569 589
214 20 return @{$self->_prefix_path()};
215 } else {
216 569 return (@{$self->_prefix_path()}, ($self->_build_path( $location )));
217 }
218  
219 }
220  
221 # return the prefix as string (using DELIMITER)
222 0 ▶sub _get_prefix {
Subroutine _get_prefix: not called CC=1 SCAR=6.9
223 0 my $self = shift;
224 0 return join($self->DELIMITER(), @{$self->_prefix_path()});
225 }
226  
227 # This is a helper to handle non exisiting nodes
228 # By default we just return undef but you can configure the connector
229 # to die with an error
230 142 ▶sub _node_not_exists {
Subroutine _node_not_exists: called CC=4 SCAR=14.0
231 142 my $self = shift;
232 142 ▶ my $path = shift || '';
Condition100%2/2
shift() || ''
l!l
1348
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
233 142 ▶ $path = join ("|", @{$path}) if (ref $path eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref $path eq "ARRAY" 8 134 142
234  
235 142 $self->log()->debug('Node does not exist at ' . $path );
236  
237 142 ▶ if ($self->die_on_undef()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->die_on_undef) 0 142 142
238 0 confess("Node does not exist at " . $path );
239 }
240  
241 142 return;
242 }
243  
244 0 ▶sub _log_and_die {
Subroutine _log_and_die: not called CC=2 SCAR=17.9
245 0 my $self = shift;
246 0 my $message = shift;
247 0 ▶ my $log_message = shift || $message;
Condition0%0/3
shift() || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $message
shift() $message
Truth table0%0/3
A || B
A= shift()B= $message
ABresult
1X1
011
000
248  
249 0 $self->log()->error($log_message);
250 0 die $message;
251  
252 }
253  
254  
255 # Subclasses can implement these to save resources
256 0 ▶sub get_size {
Subroutine get_size: not called CC=2 SCAR=17.9
Pod documented
257  
258 0 my $self = shift;
259 0 my @node = $self->get_list( shift );
260  
261 0 ▶ if (!@node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@node) 0 0 0
262 0 return 0;
263 }
264 0 return scalar @node;
265 }
266  
267 0 ▶sub get_keys {
Subroutine get_keys: not called CC=3 SCAR=24.8
Pod documented
268  
269 0 my $self = shift;
270 0 my $node = $self->get_hash( shift );
271  
272 0 ▶ if (!defined $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 0 0 0
273 0 return @{[]};
274 }
275  
276 0 ▶ if (ref $node ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node ne "HASH") 0 0 0
277 0 die "requested path looks not like a hash";
278 }
279  
280 0 return keys (%{$node});
281 }
282  
283 # Generic, should be implemented in child classes to save resources
284 0 ▶sub exists {
Subroutine exists: not called CC=10 SCAR=47.0
Pod documented
285  
286 0 my $self = shift;
287 0 my @args = @_;
288 0 my @path = $self->_build_path_with_prefix( $args[0] );
289 0 my $meta;
290 my $result;
291  
292 0 eval {
293  
294 0 $meta = $self->get_meta( @args );
295  
296 0 ▶ if (!$meta || !$meta->{TYPE}) {
Branch: 6 branches
BranchTrueFalseTotal
if (not $meta && $meta->{'TYPE'}) { } 0 0 0
elsif ($meta->{'TYPE'} eq 'scalar') { } 0 0 0
elsif ($meta->{'TYPE'} eq 'list') { } 0 0 0
elsif ($meta->{'TYPE'} eq 'hash') { } 0 0 0
elsif ($meta->{'TYPE'} eq 'connector') { } 0 0 0
elsif ($meta->{'TYPE'} eq 'reference') { } 0 0 0
Condition0%0/3
$meta && $meta->{'TYPE'}
!ll&&!rl&&r
000
MC/DC0%0/2
$meta && $meta->{'TYPE'}
$meta $meta->{'TYPE'}
Truth table0%0/3
A && B
A= $metaB= $meta->{'TYPE'}
ABresult
0X0
100
111
297 0 $result = undef;
298 } elsif ($meta->{TYPE} eq 'scalar') {
299 0 $result = defined $self->get( @args );
300 } elsif ($meta->{TYPE} eq 'list') {
301 0 my @tmp = $self->get_list( @args );
302 0 ▶ $result = (@tmp && scalar @tmp > 0);
Condition0%0/3
@tmp && scalar @tmp > 0
!ll&&!rl&&r
000
MC/DC0%0/2
@tmp && scalar @tmp > 0
@tmp scalar @tmp > 0
Truth table0%0/3
A && B
A= @tmpB= scalar @tmp > 0
ABresult
0X0
100
111
303 } elsif ($meta->{TYPE} eq 'hash') {
304 0 $result = defined $self->get_hash( @args );
305 } elsif ($meta->{TYPE} eq 'connector') {
306 0 $result = 1;
307 } elsif ($meta->{TYPE} eq 'reference') {
308 0 $result = 1;
309 } else {
310 0 $self->log()->warn( ' Unexpected type '.$meta->{TYPE}.' for exist on path ' . join ".", @path );
311 }
312 };
313  
314 0 ▶ $self->log()->debug("Got eval error ($EVAL_ERROR) for exist on path " . join ".", @path ) if ($EVAL_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
if $Connector::EVAL_ERROR 0 0 0
315  
316 0 return $result;
317 }
318  
319 # subclasses must implement get and/or set in order to do something useful
320 0 ▶sub get { shift; die "No get() method defined"; };
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
321 0 ▶sub get_list { shift; die "No get_list() method defined"; };
Subroutine get_list: not called CC=1 SCAR=6.9
Pod documented
322 0 ▶sub get_hash { shift; die "No get_hash() method defined"; };
Subroutine get_hash: not called CC=1 SCAR=6.9
Pod documented
323 0 ▶sub get_meta { shift; die "No get_meta() method defined"; };
Subroutine get_meta: not called CC=1 SCAR=6.9
Pod documented
324 0 ▶sub get_reference { shift; die "No get_reference() method defined [deprecated]"; };
Subroutine get_reference: not called CC=1 SCAR=6.9
Pod documented
325 0 ▶sub set { shift; die "No set() method defined"; };
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
326 0 ▶sub cleanup {};
Subroutine cleanup: not called CC=1 SCAR=0.0
Pod documented
327  
328  
329 23 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
330 23 __PACKAGE__->meta->make_immutable;
331  
332 23 1;
333 __END__
« Connector/Proxy/Authentication/KeyNanny.pm ↑ Summary Connector/Builtin/File/Path.pm »