lib/Net/LDAP/SimpleServer/LDIFStore.pm

filter: Statementstmt 89.1% 115 / 129 Branchbran 65.0% 26 / 40 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 92.5% 25 / 27 Podpod 100% 8 / 8 totaltotal 80.9% 178 / 220 SCAR 33.8
CC
25
Cov
81%
_open_ldif
13.1
load
4.4
_make_entry
4.0
CRAP 29.3
« lib/Net/LDAP/SimpleServer/ProtocolHandler.pm ↑ Summary lib/Net/LDAP/SimpleServer.pm »
1 package Net::LDAP::SimpleServer::LDIFStore;
2  
3 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Data store to support Net::LDAP::SimpleServer
7  
8 41 our $VERSION = '0.0.20'; # VERSION
9  
10 41 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 41 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 41 ▶use UNIVERSAL::isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 41 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 41 ▶use Net::LDAP::LDIF;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 41 ▶use Net::LDAP::Util qw/canonical_dn/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 41 ▶use Net::LDAP::SimpleServer::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 sub new {
21 28 ▶ my ( $class, $param ) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
22 28 ▶ croak 'Must pass parameter!' unless defined($param);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 0 28 28
23  
24 # empty defaults
25 28 my $data = {
26 ldif_object => undef,
27 tree => {},
28 };
29  
30 28 my $self = bless( $data, $class );
31 28 $self->load($param);
32 26 return $self;
33 }
34  
35 sub load {
36 28 ▶ my ( $self, $param ) = @_;
Subroutine load: called CC=4 SCAR=14.8
Pod documented
37  
38 28 ▶ croak 'Must pass parameter!' unless $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $param 1 27 28
39  
40 27 ▶ if ( blessed($param) && $param->isa('Net::LDAP::LDIF') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($param) and $param->isa('Net::LDAP::LDIF')) { } 0 27 27
Condition33%1/3
builtin::blessed($param) and $param->isa("Net::LDAP::LDIF")
!ll&&!rl&&r
2700
MC/DC0%0/2
builtin::blessed($param) and $param->isa("Net::LDAP::LDIF")
builtin::blessed($param) $param->isa("Net::LDAP::LDIF")
Truth table33%1/3
A and B
A= builtin::blessed($param)B= $param->isa("Net::LDAP::LDIF")
ABresult
0X0
100
111
41 0 $self->{ldif_object} = $param;
42 }
43 else {
44 27 $self->_open_ldif($param);
45 }
46 26 $self->_load_ldif();
47 26 return;
48 }
49  
50 sub ldif {
51 0 ▶ my $self = shift;
Subroutine ldif: not called CC=1 SCAR=6.9
Pod documented
52 0 return $self->{ldif_object};
53 }
54  
55 #
56 # opens a filename, a file-handle, or a Net::LDAP::LDIF object
57 #
58 sub _open_ldif {
59 27 ▶ my $self = shift;
Subroutine _open_ldif: called CC=7 SCAR=25.7
60 27 ▶ my $param = shift // '';
Condition50%1/2
shift() // ""
l!l
270
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
61  
62 27 ▶ my $reftype = reftype($param) // '';
Condition50%1/2
builtin::reftype($param) // ""
l!l
027
MC/DC0%0/1
builtin::reftype($param) // ""
builtin::reftype($param)
Truth table50%1/2
A
A= builtin::reftype($param)
Aresult
11
00
63 27 ▶ if ( $reftype eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "HASH") 0 27 27
64 croak q{Hash parameter must contain a "ldif" parameter}
65 0 ▶ unless exists $param->{ldif};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{"ldif"} 0 0 0
66  
67 $self->{ldif_object} = Net::LDAP::LDIF->new(
68 $param->{ldif},
69 'r',
70 (
71 exists $param->{ldif_options}
72 0 ▶ ? %{ $param->{ldif_options} }
Branch: 1 branch
BranchTrueFalseTotal
exists $param->{'ldif_options'} ? : 0 0 0
73 : undef
74 )
75 );
76 0 return;
77 }
78  
79 # Then, it must be a filename
80 27 ▶ croak q{Cannot read file "} . $param . q{"} unless -r $param;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $param 1 26 27
81  
82 26 $self->{ldif_object} = Net::LDAP::LDIF->new($param);
83 }
84  
85 sub _make_entry_path {
86 39 ▶ my $dn = shift;
Subroutine _make_entry_path: called CC=2 SCAR=6.9
87  
88 39 ▶ $dn = $dn->dn() if $dn->isa('Net::LDAP::Entry');
Branch: 1 branch
BranchTrueFalseTotal
if $dn->isa("Net::LDAP::Entry") 26 13 39
89  
90 39 return [ reverse( split( ',', canonical_dn($dn) ) ) ];
91 }
92  
93 sub _make_entry {
94 78 ▶ my ( $entry, $tree, $current_dn, @path ) = @_;
Subroutine _make_entry: called CC=4 SCAR=14.0
95  
96 78 ▶ $tree = {} unless defined($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree 78 0 78
97 78 ▶ if ( scalar(@path) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path == 0) { } 26 52 78
98 26 $tree->{_object} = $entry;
99 }
100 else {
101 52 my $next = $path[0];
102 $tree->{_object} = Net::LDAP::Entry->new($current_dn)
103 52 ▶ unless exists $tree->{_object};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tree->{"_object"} 52 0 52
104 $tree->{$next} = _make_entry(
105 52 $entry, $tree->{$next},
106 join( q{,}, $next, $current_dn ),
107 @path[ 1 .. $#path ]
108 );
109 }
110  
111 78 return $tree;
112 }
113  
114 sub _add {
115 26 ▶ my ( $self, $entry ) = @_;
Subroutine _add: called CC=1 SCAR=0.0
116  
117 26 my @path = @{ _make_entry_path($entry) };
118 26 my $tree = $self->{tree};
119 26 my $next = $path[0];
120 26 $tree->{$next} = _make_entry( $entry, $tree->{$next}, @path );
121  
122 # line above is equivalent to
123 # _make_entry( $entry, $tree->{$next}, $next, @path[ 1 .. $#path ] );
124 }
125  
126 #
127 # loads a LDIF file
128 #
129 sub _load_ldif {
130 26 ▶ my $self = shift;
Subroutine _load_ldif: called CC=3 SCAR=11.7
131 26 my $ldif = $self->{ldif_object};
132  
133 26 ▶ while ( not $ldif->eof() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ldif->eof) 26 26 52
134 26 my $entry = $ldif->read_entry();
135 26 ▶ if ( $ldif->error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ldif->error) 0 26 26
136 0 print STDERR "Error msg: ", $ldif->error(), "\n";
137 0 print STDERR "Error lines:\n", $ldif->error_lines(), "\n";
138 0 next;
139 }
140  
141 26 $self->_add($entry);
142 }
143 26 $ldif->done();
144 }
145  
146 sub _find_subtree {
147 28 ▶ my ( $tree, $rdn, @path ) = @_;
Subroutine _find_subtree: called CC=3 SCAR=11.0
148  
149 28 ▶ return unless exists $tree->{$rdn};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tree->{$rdn} 1 27 28
150 27 ▶ return $tree->{$rdn} if scalar(@path) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @path == 0 12 15 27
151 15 return _find_subtree( $tree->{$rdn}, @path );
152 }
153  
154 sub find_tree {
155 13 ▶ my $self = shift;
Subroutine find_tree: called CC=2 SCAR=7.0
Pod documented
156 13 my $dn = shift;
157 13 ▶ $dn = $dn->dn() if $dn->isa('Net::LDAP::Entry');
Branch: 1 branch
BranchTrueFalseTotal
if $dn->isa("Net::LDAP::Entry") 0 13 13
158  
159 13 return _find_subtree( $self->{tree}, @{ _make_entry_path($dn) } );
160 }
161  
162 sub exists_dn {
163 0 ▶ my ( $self, $dn ) = @_;
Subroutine exists_dn: not called CC=1 SCAR=6.9
Pod documented
164  
165 0 my $tree = $self->find_tree($dn);
166 0 return defined($tree);
167 }
168  
169 sub find_entry {
170 4 ▶ my ( $self, $dn ) = @_;
Subroutine find_entry: called CC=2 SCAR=7.6
Pod documented
171  
172 4 my $tree = $self->find_tree($dn);
173 4 ▶ return $tree->{_object} if defined($tree);
Branch: 1 branch
BranchTrueFalseTotal
if defined $tree 4 0 4
174 0 return;
175 }
176  
177 sub _list {
178 24 ▶ my $tree = shift;
Subroutine _list: called CC=1 SCAR=0.0
179  
180 my @children_trees =
181 24 map { $tree->{$_} } ( grep { $_ ne '_object' } keys( %{$tree} ) );
182  
183 24 return ( $tree->{_object}, ( map { ( _list($_) ) } @children_trees ) );
184 }
185  
186 sub list {
187 4 ▶ my $self = shift;
Subroutine list: called CC=2 SCAR=7.4
Pod documented
188 4 ▶ my $tree = shift // $self->{tree}->{ ( keys( %{ $self->{tree} } ) )[0] };
Condition33%1/3
shift() // $self->{"tree"}{(keys %{$$self{"tree"};})[0]}
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() // $self->{"tree"}{(keys %{$$self{"tree"};})[0]}
shift() $self->{"tree"}{(keys %{$$self{"tree"};})[0]}
Truth table33%1/3
A // B
A= shift()B= $self->{"tree"}{(keys %{$$self{"tree"};})[0]}
ABresult
1X1
011
000
189  
190 4 return [ _list($tree) ];
191 }
192  
193 sub _list_baseobj {
194 3 ▶ my $self = shift;
Subroutine _list_baseobj: called CC=2 SCAR=7.0
195 3 my $dn = shift;
196 3 my $entry = $self->find_entry($dn);
197  
198 3 ▶ return unless defined($entry);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry 0 3 3
199  
200 3 return [$entry];
201 }
202  
203 sub _list_onelevel {
204 2 ▶ my $self = shift;
Subroutine _list_onelevel: called CC=2 SCAR=6.9
205 2 my $dn = shift;
206 2 my $tree = $self->find_tree($dn);
207  
208 2 ▶ return unless defined($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree 0 2 2
209 my @children =
210 1 map { $tree->{$_}->{_object} }
211 2 ( grep { $_ ne '_object' } keys( %{$tree} ) );
212  
213 2 return [ $tree->{_object}, @children ];
214 }
215  
216 sub _list_subtree {
217 6 ▶ my $self = shift;
Subroutine _list_subtree: called CC=2 SCAR=6.9
218 6 my $dn = shift;
219 6 my $tree = $self->find_tree($dn);
220  
221 6 ▶ return unless defined($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree 1 5 6
222 5 return [ _list($tree) ];
223 }
224  
225 sub list_with_dn_scope {
226 11 ▶ my ( $self, $dn, $scope ) = @_;
Subroutine list_with_dn_scope: called CC=1 SCAR=0.0
Pod documented
227  
228 11 my @funcs = ( \&_list_baseobj, \&_list_onelevel, \&_list_subtree );
229 11 return $funcs[$scope]->( $self, $dn );
230 }
231  
232 41 1; # Magic true value required at end of module
233  
234 __END__
« lib/Net/LDAP/SimpleServer/ProtocolHandler.pm ↑ Summary lib/Net/LDAP/SimpleServer.pm »