FreeIPA/API/Magic.pm

filter: Statementstmt 98.5% 66 / 67 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 97.9% 96 / 98 CC 11 SCAR 14.1
CC
11
Cov
98%
cache
8.0
retrieve
4.0
BEGIN
1.0
CRAP 4.1
« FreeIPA/Base.pm ↑ Summary FreeIPA/Request.pm »
1 package Net::FreeIPA::API::Magic;
2 8 $Net::FreeIPA::API::Magic::VERSION = '3.0.3';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Types::Serialiser; # is used by JSON::XS
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use Net::FreeIPA::API::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 our @EXPORT_OK = qw(retrieve version);
16  
17 8 Readonly my $TRUE => Types::Serialiser::true;
18 8 Readonly my $FALSE => Types::Serialiser::false;
19  
20 # Cache these command keys
21 8 Readonly::Array our @CACHE_KEYS => qw(
22 name
23 takes_args takes_options
24 );
25  
26 # Cache these keys from the takes_ CACHE_KEYS
27 8 Readonly::Array our @CACHE_TAKES_KEYS => qw(
28 name type class
29 required autofill multivalue
30 );
31  
32 8 Readonly::Hash our %CACHE_TAKES_DEFAULT => {
33 autofill => $FALSE,
34 class => 'unknown_class',
35 multivalue => $FALSE,
36 name => 'unknown_name',
37 required => $FALSE,
38 type => 'unknown_type',
39 };
40  
41 # hashref to store cached command data
42 8 my $cmd_cache = {};
43  
44 =head2 Public functions
45  
46 =over
47  
48 =item flush_cache
49  
50 Reset the cache
51  
52 =cut
53  
54 sub flush_cache
55 1 ▶{
Subroutine flush_cache: called CC=1 SCAR=0.0
Pod documented
56 1 $cmd_cache = {};
57 1 return $cmd_cache;
58 }
59  
60 =item cache
61  
62 Given C<data> command hashref, cache (and return) the relevant
63 (filtered) command data.
64  
65 C<data> is typically the decoded JSON command response.
66  
67 =cut
68  
69 sub cache
70 5 ▶{
Subroutine cache: called CC=8 SCAR=20.8
Pod documented
71 5 my ($data) = @_;
72  
73 5 my $name = $data->{name};
74  
75 5 foreach my $key (sort keys %$data) {
76 21 ▶ if ($key =~ m/^takes_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^takes_/) { } 10 11 21
77 10 my $newdata = [];
78 10 foreach my $tdata (@{$data->{$key}}) {
79 101 ▶ if (ref($tdata) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tdata eq '') { } 1 100 101
80 1 my $value = $tdata;
81 1 $value =~ s/[*+?]$//;
82 1 $tdata = {%CACHE_TAKES_DEFAULT};
83 1 $tdata->{name} = $value;
84 } else {
85 # Arrayref of command hashrefs
86 100 foreach my $tkey (sort keys %$tdata) {
87 681 ▶ delete $tdata->{$tkey} if (! grep {$tkey eq $_} @CACHE_TAKES_KEYS);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$tkey eq $_;} @Net::FreeIPA::API::Magic::CACHE_TAKES_KEYS 81 600 681
88 }
89 }
90 101 push(@$newdata, $tdata);
91 }
92 10 $data->{$key} = $newdata;
93 } else {
94 11 ▶ delete $data->{$key} if (! grep {$key eq $_} @CACHE_KEYS);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$key eq $_;} @Net::FreeIPA::API::Magic::CACHE_KEYS 6 5 11
95 }
96 }
97  
98 5 $cmd_cache->{$name} = $data;
99  
100 5 return $data;
101 }
102  
103 =item version
104  
105 Return the API version from C<Net::FreeIPA::API::Data>
106  
107 =cut
108  
109 sub version
110 1 ▶{
Subroutine version: called CC=1 SCAR=0.0
Pod documented
111 1 return $Net::FreeIPA::API::Data::VERSION;
112 }
113  
114 =item retrieve
115  
116 Retrieve the command data for command C<name>.
117  
118 (For now, the data is loaded from the C<Net::FreeIPA::API::Data> that is
119 distributed with this package and is a fixed version only).
120  
121 Returns the cache command hashref and undef errormessage on SUCCESS,
122 an emptyhashref and actual errormessage otherwise.
123 If the command is already in cache, return the cached version
124 (and undef errormessage).
125  
126 =cut
127  
128 sub retrieve
129 4 ▶{
Subroutine retrieve: called CC=4 SCAR=13.9
Pod documented
130 4 my ($name) = @_;
131  
132 # Return already cached data
133 4 ▶ return ($cmd_cache->{$name}, undef) if defined($cmd_cache->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if defined $cmd_cache->{$name} 1 3 4
134  
135 # Get the JSON data from Net::FreeIPA::API::Data
136 # TODO: get the JSON data from the JSON api
137  
138 3 my $data;
139 3 my $json = $Net::FreeIPA::API::Data::API_DATA{$name};
140 3 ▶ if (! $json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json) 1 2 3
141 1 return {}, "retrieve name $name failed: no JSON data";
142 }
143  
144 2 local $@;
145 2 eval {
146 2 $data = decode_json($json);
147 };
148  
149 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 2 2
150 0 return {}, "retrieve name $name failed decode_json with $@";
151 } else {
152 2 return cache($data), undef;
153 };
154 }
155  
156 =item all_command_names
157  
158 Return all possible commandsnames sorted.
159  
160 Does not update cache.
161  
162 =cut
163  
164 # Used mainly to generate the API::Function exports
165  
166 sub all_command_names
167 1 ▶{
Subroutine all_command_names: called CC=1 SCAR=0.0
Pod documented
168  
169 # Get the JSON data from Net::FreeIPA::API::Data
170 # TODO: get the JSON data from the JSON api
171 # If the JSON API doesn't allow to just get the names,
172  
173 1 return sort keys %Net::FreeIPA::API::Data::API_DATA;
174 }
175  
176 =pod
177  
178 =back
179  
180 =cut
181  
182  
183 8 1;
« FreeIPA/Base.pm ↑ Summary FreeIPA/Request.pm »