Connector/Builtin/Memory.pm

filter: Statementstmt 91.1% 113 / 124 Branchbran 76.6% 46 / 60 Conditioncond 66.6% 18 / 27 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 83.2% 208 / 250 CC 40 SCAR 19.7
CC
40
Cov
84%
_get_node
13.7
get_meta
8.1
get
7.6
CRAP 7.2
« Connector/Role/LocalPath.pm ↑ Summary Connector/Builtin/Authentication/Password.pm »
1 # Connector::Builtin::Memory
2 #
3 # Proxy class for reading YAML configuration
4 #
5 # Written by Scott Hardin, Martin Bartosch and Oliver Welter
6 # for the OpenXPKI project 2012
7 #
8 # THIS IS NOT WORKING IN A FORKING ENVIRONMENT!
9  
10  
11 package Connector::Builtin::Memory;
12  
13 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 extends 'Connector::Builtin';
20  
21 9 has '+LOCATION' => ( required => 0 );
22  
23 9 has 'primary_attribute' => (
24 is => 'ro',
25 isa => 'Str',
26 predicate => 'has_primary_attribute',
27 );
28  
29 3 ▶sub _build_config {
Subroutine _build_config: called CC=1 SCAR=0.0
30 3 my $self = shift;
31 3 $self->_config( {} );
32 }
33  
34 303 ▶sub _get_node {
Subroutine _get_node: called CC=13 SCAR=26.2
35  
36 303 my $self = shift;
37 303 my @path = $self->_build_path_with_prefix( shift );
38  
39 303 $self->log()->trace('get node for path'. Dumper \@path);
40  
41 303 my $ptr = $self->_config();
42  
43 303 ▶ while ( scalar @path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path) 795 187 982
44 795 my $entry = shift @path;
45 795 ▶ if ( ref $ptr eq 'HASH' && exists $ptr->{$entry} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ptr eq 'HASH' and exists $ptr->{$entry}) { } 681 114 795
elsif (ref $ptr eq 'ARRAY' and $entry =~ /\A\d+\z/ and exists $ptr->[$entry]) { } 2 112 114
Condition100%3/3
ref $ptr eq "HASH" and exists $ptr->{$entry}
!ll&&!rl&&r
3111681
MC/DC100%2/2
ref $ptr eq "HASH" and exists $ptr->{$entry}
ref $ptr eq "HASH" exists $ptr->{$entry}
Truth table100%3/3
A and B
A= ref $ptr eq "HASH"B= exists $ptr->{$entry}
ABresult
0X0
100
111
46 681 my $type = ref $ptr->{$entry};
47 681 ▶ if ( $type eq 'HASH' || $type eq 'ARRAY' || scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'HASH' or $type eq 'ARRAY' or scalar @path == 0) { } 677 4 681
Condition100%6/6
$type eq "HASH" or $type eq "ARRAY" or scalar @path == 0
l!l&&r!l&&!r
5651124
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
54916116
MC/DC100%3/3
$type eq "HASH" or $type eq "ARRAY" or scalar @path == 0
$type eq "HASH" $type eq "ARRAY" scalar @path == 0
Truth table100%4/4
A or B or C
A= $type eq "HASH"B= $type eq "ARRAY"C= scalar @path == 0
ABCresult
1XX1
01X1
0011
0000
48 677 $ptr = $ptr->{$entry};
49 }
50 else {
51 4 $self->log()->debug("tried to walk over unexpected node type: $type");
52 4 return $self->_node_not_exists( $entry );
53 }
54 }
55 ▶ elsif ( ref $ptr eq 'ARRAY' && $entry =~ m{\A\d+\z} && exists $ptr->[$entry] ) {
Condition83%5/6
ref $ptr eq "ARRAY" and $entry =~ /\A\d+\z/ and exists $ptr->[$entry]
!ll&&!rl&&r
11112
ref $ptr eq "ARRAY" and $entry =~ /\A\d+\z/
!ll&&!rl&&r
11103
MC/DC66%2/3
ref $ptr eq "ARRAY" and $entry =~ /\A\d+\z/ and exists $ptr->[$entry]
ref $ptr eq "ARRAY" $entry =~ /\A\d+\z/ exists $ptr->[$entry]
Truth table75%3/4
A and B and C
A= ref $ptr eq "ARRAY"B= $entry =~ /\A\d+\z/C= exists $ptr->[$entry]
ABCresult
0XX0
10X0
1100
1111
56 2 my $type = ref $ptr->[$entry];
57 2 ▶ if ( $type eq 'HASH' || $type eq 'ARRAY' || scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'HASH' or $type eq 'ARRAY' or scalar @path == 0) { } 2 0 2
Condition33%2/6
$type eq "HASH" or $type eq "ARRAY" or scalar @path == 0
l!l&&r!l&&!r
020
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
002
MC/DC0%0/3
$type eq "HASH" or $type eq "ARRAY" or scalar @path == 0
$type eq "HASH" $type eq "ARRAY" scalar @path == 0
Truth table25%1/4
A or B or C
A= $type eq "HASH"B= $type eq "ARRAY"C= scalar @path == 0
ABCresult
1XX1
01X1
0011
0000
58 2 $ptr = $ptr->[$entry];
59 }
60 else {
61 0 $self->log()->debug("tried to walk over unexpected node type: $type");
62 0 return $self->_node_not_exists( $entry );
63 }
64 } else {
65 112 return $self->_node_not_exists($entry);
66 }
67 }
68  
69 187 return $ptr;
70  
71 }
72  
73 64 ▶sub get {
Subroutine get: called CC=7 SCAR=20.2
Pod documented
74  
75 64 my $self = shift;
76 64 my $value = $self->_get_node( shift );
77  
78 64 ▶ return $self->_node_not_exists() unless (defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 8 56 64
79  
80 56 ▶ if (ref $value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "") 1 55 56
81 1 ▶ die "requested value is not a scalar"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_primary_attribute and ref $value eq "HASH" 0 1 1
Condition33%1/3
$self->has_primary_attribute and ref $value eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
$self->has_primary_attribute and ref $value eq "HASH"
$self->has_primary_attribute ref $value eq "HASH"
Truth table33%1/3
A and B
A= $self->has_primary_attributeB= ref $value eq "HASH"
ABresult
0X0
100
111
82 unless ($self->has_primary_attribute() && ref $value eq 'HASH');
83  
84 return $self->_node_not_exists()
85 1 ▶ unless (defined $value->{$self->primary_attribute});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$value{$self->primary_attribute} 0 1 1
86  
87 die "primary_attribute is not a scalar"
88 1 ▶ unless (ref $value->{$self->primary_attribute} eq '');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$value{$self->primary_attribute} eq "" 0 1 1
89  
90 1 return $value->{$self->primary_attribute};
91 }
92  
93 55 return $value;
94  
95 }
96  
97 4 ▶sub get_size {
Subroutine get_size: called CC=3 SCAR=11.2
Pod documented
98  
99 4 my $self = shift;
100 4 my $node = $self->_get_node( shift );
101  
102 4 ▶ return 0 unless(defined $node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 1 3 4
103  
104 3 ▶ if ( ref $node ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node ne "ARRAY") 0 3 3
105 0 die "requested value is not a list"
106 }
107  
108 3 return scalar @{$node};
109 }
110  
111 6 ▶sub get_list {
Subroutine get_list: called CC=3 SCAR=11.1
Pod documented
112  
113 6 my $self = shift;
114 6 my $path = shift;
115  
116 6 my $node = $self->_get_node( $path );
117  
118 6 ▶ return $self->_node_not_exists( $path ) unless(defined $node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 1 5 6
119  
120 5 ▶ if ( ref $node ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node ne "ARRAY") 0 5 5
121 0 die "requested value is not a list"
122 }
123  
124 5 return @{$node};
125 }
126  
127 13 ▶sub get_keys {
Subroutine get_keys: called CC=3 SCAR=11.8
Pod documented
128  
129 13 my $self = shift;
130 13 my $path = shift;
131  
132 13 my $node = $self->_get_node( $path );
133  
134 0 ▶ return @{[]} unless(defined $node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 13 13
135  
136 13 ▶ if ( ref $node ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node ne "HASH") 0 13 13
137 0 die "requested value is not a hash"
138 }
139  
140 13 return keys %{$node};
141 }
142  
143 7 ▶sub get_hash {
Subroutine get_hash: called CC=3 SCAR=11.2
Pod documented
144  
145 7 my $self = shift;
146 7 my $path = shift;
147  
148 7 my $node = $self->_get_node( $path );
149  
150 7 ▶ return $self->_node_not_exists( $path ) unless(defined $node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 1 6 7
151  
152 6 ▶ if ( ref $node ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node ne "HASH") 0 6 6
153 0 die "requested value is not a hash"
154 }
155  
156 6 return { %$node };
157 }
158  
159 190 ▶sub get_meta {
Subroutine get_meta: called CC=8 SCAR=21.0
Pod documented
160  
161 190 my $self = shift;
162  
163 190 my $node = $self->_get_node( shift );
164  
165 190 $self->log()->trace('get_node returned '. Dumper $node);
166  
167 190 ▶ if (!defined $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 100 90 190
168 # die_on_undef already handled by get_node
169 100 return;
170 }
171  
172 90 my $meta = {};
173  
174 90 ▶ if (ref $node eq '') {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $node eq '') { } 33 57 90
elsif (ref $node eq 'SCALAR') { } 20 37 57
elsif (ref $node eq 'ARRAY') { } 5 32 37
elsif (ref $node eq 'HASH') { } 29 3 32
elsif (builtin::blessed($node) and $node->isa('Connector')) { } 3 0 3
175 33 $meta = {TYPE => "scalar", VALUE => $node };
176 } elsif (ref $node eq "SCALAR") {
177 20 $meta = {TYPE => "reference", VALUE => $$node };
178 } elsif (ref $node eq "ARRAY") {
179 5 $meta = {TYPE => "list", VALUE => $node };
180 } elsif (ref $node eq "HASH") {
181 29 my @keys = keys(%{$node});
182 29 $meta = {TYPE => "hash", VALUE => \@keys };
183 ▶ } elsif (blessed($node) && $node->isa('Connector')) {
Condition33%1/3
builtin::blessed($node) and $node->isa("Connector")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($node) and $node->isa("Connector")
builtin::blessed($node) $node->isa("Connector")
Truth table33%1/3
A and B
A= builtin::blessed($node)B= $node->isa("Connector")
ABresult
0X0
100
111
184 3 $meta = {TYPE => "connector", VALUE => $node };
185 } else {
186 0 die "Unsupported node type: " . ref $node;
187 }
188 90 return $meta;
189 }
190  
191 19 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
192  
193 19 my $self = shift;
194  
195 19 my $value = 0;
196 19 eval {
197 19 $value = defined $self->_get_node( shift );
198 };
199 19 return $value;
200  
201 }
202  
203 10 ▶sub set {
Subroutine set: called CC=7 SCAR=20.2
Pod documented
204  
205 10 my $self = shift;
206 10 my @path = $self->_build_path_with_prefix( shift );
207  
208 10 my $value = shift;
209  
210 10 my $ptr = $self->_config();
211  
212 10 ▶ while (scalar @path > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path > 1) 6 10 16
213 6 my $entry = shift @path;
214 6 ▶ if (!exists $ptr->{$entry}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $ptr->{$entry}) { } 4 2 6
elsif (ref $ptr->{$entry} ne 'HASH') { } 0 2 2
215 4 $ptr->{$entry} = {};
216 } elsif (ref $ptr->{$entry} ne "HASH") {
217 0 confess('Try to step over a value node at ' . $entry);
218 }
219 6 $ptr = $ptr->{$entry};
220 }
221  
222 10 my $entry = shift @path;
223  
224 10 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 9 10
225 1 delete $ptr->{$entry};
226 1 return;
227 }
228  
229 9 ▶ if (exists $ptr->{$entry}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ptr->{$entry}) 0 9 9
230 0 ▶ if (ref $ptr->{$entry} ne ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ptr->{$entry} ne ref $value) 0 0 0
231 0 confess('Try to override data type at node ' . $entry);
232 }
233 }
234 9 $ptr->{$entry} = $value;
235 9 return 1;
236 }
237  
238  
239 9 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
240 9 __PACKAGE__->meta->make_immutable;
241  
242 9 1;
243 __END__
« Connector/Role/LocalPath.pm ↑ Summary Connector/Builtin/Authentication/Password.pm »