v1/Role/ObjectMethods.pm

filter: Statementstmt 52.5% 31 / 59 Branchbran 34.6% 9 / 26 Conditioncond 30.0% 9 / 30 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 53.8% 7 / 13 Podpod n/a totaltotal 39.8% 59 / 148 SCAR 53.6
CC
31
Cov
43%
__ANON__
132.0
__ANON__
71.5
__ANON__
56.0
CRAP 212.7
« v1.pm ↑ Summary v1/Role/REST/Client.pm »
1 package Net::Checkpoint::Management::v1::Role::ObjectMethods;
2 3 $Net::Checkpoint::Management::v1::Role::ObjectMethods::VERSION = '0.002000';
3 # ABSTRACT: Role for Checkpoint Management API version 1.x method generation
4  
5 3 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use MooX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp::Clan qw(^Net::Checkpoint::Management::v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Moo::Role; # last for cleanup
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶no warnings "experimental::signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 requires qw( _create _list _get _update _delete );
14  
15  
16  
17  
18  
19  
20  
21  
22 role {
23 32 ▶ my $params = shift;
Subroutine __ANON__: called CC=13 SCAR=42.7
24 32 my $mop = shift;
25  
26 32 ▶ if (exists $params->{singular} && defined $params->{singular}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"singular"} and defined $params->{"singular"}) 32 0 32
Condition33%1/3
exists $params->{"singular"} and defined $params->{"singular"}
!ll&&!rl&&r
0032
MC/DC0%0/2
exists $params->{"singular"} and defined $params->{"singular"}
exists $params->{"singular"} defined $params->{"singular"}
Truth table33%1/3
A and B
A= exists $params->{"singular"}B= defined $params->{"singular"}
ABresult
0X0
100
111
27 ▶ $mop->method('create_' . $params->{singular} => sub ($self, $object_data) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 return $self->_create(join('/',
29 '/web_api',
30 'v' . $self->api_version,
31 $params->{create}
32 0 ), $object_data);
33 })
34 32 ▶ if exists $params->{create} && defined $params->{create};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{"create"} and defined $params->{"create"} 28 4 32
Condition66%2/3
exists $params->{"create"} and defined $params->{"create"}
!ll&&!rl&&r
4028
MC/DC50%1/2
exists $params->{"create"} and defined $params->{"create"}
exists $params->{"create"} defined $params->{"create"}
Truth table66%2/3
A and B
A= exists $params->{"create"}B= defined $params->{"create"}
ABresult
0X0
100
111
35  
36 0 ▶ $mop->method('get_' . $params->{singular} => sub ($self, $query_params = {}) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
37 return $self->_get(join('/',
38 '/web_api',
39 'v' . $self->api_version,
40 $params->{get}
41 0 ), $query_params);
42 })
43 32 ▶ if exists $params->{get} && defined $params->{get};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{"get"} and defined $params->{"get"} 32 0 32
Condition33%1/3
exists $params->{"get"} and defined $params->{"get"}
!ll&&!rl&&r
0032
MC/DC0%0/2
exists $params->{"get"} and defined $params->{"get"}
exists $params->{"get"} defined $params->{"get"}
Truth table33%1/3
A and B
A= exists $params->{"get"}B= defined $params->{"get"}
ABresult
0X0
100
111
44  
45 ▶ $mop->method('update_' . $params->{singular} => sub ($self, $object, $object_data) {
Subroutine __ANON__: not called CC=7 SCAR=40.3
46 0 my $updated_data = { %$object, %$object_data };
47 0 ▶ if (exists $params->{id_keys} && ref $params->{id_keys} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"id_keys"} and ref $params->{"id_keys"} eq "ARRAY") 0 0 0
Condition0%0/3
exists $params->{"id_keys"} and ref $params->{"id_keys"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params->{"id_keys"} and ref $params->{"id_keys"} eq "ARRAY"
exists $params->{"id_keys"} ref $params->{"id_keys"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $params->{"id_keys"}B= ref $params->{"id_keys"} eq "ARRAY"
ABresult
0X0
100
111
48 # ensure that only a single key is passed to the update call
49 # the order of keys is the priority
50 0 my @id_keys = $params->{id_keys}->@*;
51 0 ▶ while (my $key = shift @id_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = shift @id_keys) 0 0 0
52 last
53 if exists $updated_data->{$key}
54 0 ▶ && defined $updated_data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $updated_data->{$key} and defined $updated_data->{$key} 0 0 0
Condition0%0/3
exists $updated_data->{$key} and defined $updated_data->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $updated_data->{$key} and defined $updated_data->{$key}
exists $updated_data->{$key} defined $updated_data->{$key}
Truth table0%0/3
A and B
A= exists $updated_data->{$key}B= defined $updated_data->{$key}
ABresult
0X0
100
111
55 }
56 delete $updated_data->{$_}
57 0 for @id_keys;
58 }
59  
60 return $self->_update(join('/',
61 '/web_api',
62 'v' . $self->api_version,
63 $params->{update}
64 0 ), $updated_data);
65 })
66 32 ▶ if exists $params->{update} && defined $params->{update};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{"update"} and defined $params->{"update"} 30 2 32
Condition66%2/3
exists $params->{"update"} and defined $params->{"update"}
!ll&&!rl&&r
2030
MC/DC50%1/2
exists $params->{"update"} and defined $params->{"update"}
exists $params->{"update"} defined $params->{"update"}
Truth table66%2/3
A and B
A= exists $params->{"update"}B= defined $params->{"update"}
ABresult
0X0
100
111
67  
68 ▶ $mop->method('delete_' . $params->{singular} => sub ($self, $object) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 return $self->_delete(join('/',
70 '/web_api',
71 'v' . $self->api_version,
72 $params->{delete}
73 0 ), $object);
74 })
75 32 ▶ if exists $params->{delete} && defined $params->{delete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{"delete"} and defined $params->{"delete"} 28 4 32
Condition66%2/3
exists $params->{"delete"} and defined $params->{"delete"}
!ll&&!rl&&r
4028
MC/DC50%1/2
exists $params->{"delete"} and defined $params->{"delete"}
exists $params->{"delete"} defined $params->{"delete"}
Truth table66%2/3
A and B
A= exists $params->{"delete"}B= defined $params->{"delete"}
ABresult
0X0
100
111
76  
77 0 ▶ $mop->method('find_' . $params->{singular} => sub ($self, $search_params = {}, $query_params = {}) {
Subroutine __ANON__: not called CC=11 SCAR=48.8
78 0 my $listname = 'list_' . $params->{object};
79 0 my $list_key = $params->{list_key};
80 0 for my $object ($self->$listname({ 'details-level' => 'full', %$query_params })->{$list_key}->@*) {
81 0 my $identical = 0;
82 0 for my $key (keys $search_params->%*) {
83 0 ▶ if ( ref $search_params->{$key} eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $search_params->{$key} eq 'Regexp') { } 0 0 0
84 0 ▶ if ( exists $object->{$key}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $object->{$key} and $object->{$key} =~ /$search_params->{$key}/u) 0 0 0
Condition0%0/3
exists $object->{$key} and $object->{$key} =~ /$search_params->{$key}/u
!ll&&!rl&&r
000
MC/DC0%0/2
exists $object->{$key} and $object->{$key} =~ /$search_params->{$key}/u
exists $object->{$key} $object->{$key} =~ /$search_params->{$key}/u
Truth table0%0/3
A and B
A= exists $object->{$key}B= $object->{$key} =~ /$search_params->{$key}/u
ABresult
0X0
100
111
85 && $object->{$key} =~ $search_params->{$key}) {
86 0 $identical++;
87 }
88 }
89 else {
90 0 ▶ if ( exists $object->{$key}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $object->{$key} and $object->{$key} eq $search_params->{$key}) 0 0 0
Condition0%0/3
exists $object->{$key} and $object->{$key} eq $search_params->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $object->{$key} and $object->{$key} eq $search_params->{$key}
exists $object->{$key} $object->{$key} eq $search_params->{$key}
Truth table0%0/3
A and B
A= exists $object->{$key}B= $object->{$key} eq $search_params->{$key}
ABresult
0X0
100
111
91 && $object->{$key} eq $search_params->{$key}) {
92 0 $identical++;
93 }
94 }
95 }
96 0 ▶ if ($identical == scalar keys $search_params->%*) {
Branch: 1 branch
BranchTrueFalseTotal
if ($identical == scalar keys %$search_params) 0 0 0
97 0 return $object;
98 }
99 }
100 0 croak "object not found";
101 32 });
102 }
103  
104 32 ▶ if (exists $params->{object} && defined $params->{object}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"object"} and defined $params->{"object"}) 32 0 32
Condition33%1/3
exists $params->{"object"} and defined $params->{"object"}
!ll&&!rl&&r
0032
MC/DC0%0/2
exists $params->{"object"} and defined $params->{"object"}
exists $params->{"object"} defined $params->{"object"}
Truth table33%1/3
A and B
A= exists $params->{"object"}B= defined $params->{"object"}
ABresult
0X0
100
111
105 0 ▶ $mop->method('list_' . $params->{object} => sub ($self, $query_params = {}) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
106 return $self->_list(join('/',
107 '/web_api',
108 'v' . $self->api_version,
109 $params->{list}
110 0 ), $params->{list_key}, $query_params);
111 32 });
112 }
113 3 };
114  
115 3 1;
116  
117 __END__
118  
119 =pod
120  
121 =encoding UTF-8
122  
123 =head1 NAME
124  
125 Net::Checkpoint::Management::v1::Role::ObjectMethods - Role for Checkpoint Management API version 1.x method generation
126  
127 =head1 VERSION
128  
129 version 0.002000
130  
131 =head1 SYNOPSIS
132  
133 package Net::Checkpoint::Management::v1;
134 use Moo;
135 use Net::Checkpoint::Management::v1::Role::ObjectMethods;
136  
137 Net::Checkpoint::Management::v1::Role::ObjectMethods->apply([
138 {
139 object => 'packages',
140 singular => 'package',
141 create => 'add-package',
142 list => 'show-packages',
143 get => 'show-package',
144 update => 'set-package',
145 delete => 'delete-package',
146 list_key => 'packages',
147 id_keys => [qw( uid name )],
148 },
149 {
150 object => 'accessrules',
151 singular => 'accessrule',
152 create => 'add-access-rule',
153 list => 'show-access-rulebase',
154 get => 'show-access-rule',
155 update => 'set-access-rule',
156 delete => 'delete-access-rule',
157 list_key => 'rulebase',
158 id_keys => ['uid', 'name', 'rule-number'],
159 },
160 ]);
161  
162 1;
163  
164 =head1 DESCRIPTION
165  
166 This role adds methods for the commands of a specific object.
167  
168 =head1 METHODS
169  
170 =head2 create_$singular
171  
172 Takes a hashref of attributes.
173  
174 Returns the created object as hashref.
175  
176 Throws an exception on error.
177  
178 =head2 list_$object
179  
180 Takes optional query parameters.
181  
182 Returns a hashref similar to the Checkpoint Management API but without the
183 'from' and 'to' keys.
184  
185 Throws an exception on error.
186  
187 As the API only allows fetching 500 objects at a time it works around that by
188 making multiple API calls.
189  
190 =head2 get_$singular
191  
192 Takes an object id and optional query parameters.
193  
194 Returns the object as hashref.
195  
196 Throws an exception on error.
197  
198 =head2 update_$singular
199  
200 Takes an object and a hashref of attributes.
201  
202 Returns the updated object as hashref.
203  
204 Throws an exception on error.
205  
206 =head2 delete_$singular
207  
208 Takes a hashref of attributes uniquely identifying the object.
209 For most objects the uid is sufficient, accessrule requires the layer uid too.
210  
211 Returns true on success.
212  
213 Throws an exception on error.
214  
215 =head2 find_$singular
216  
217 Takes search and optional query parameters.
218  
219 Returns the object as hashref on success.
220  
221 Throws an exception on error.
222  
223 As there is no API for searching by all attributes this method emulates this
224 by fetching all objects using the L</list_$object> method and performing the
225 search on the client.
226  
227 =head1 AUTHOR
228  
229 Alexander Hartmaier <abraxxa@cpan.org>
230  
231 =head1 COPYRIGHT AND LICENSE
232  
233 This software is copyright (c) 2022 by Alexander Hartmaier.
234  
235 This is free software; you can redistribute it and/or modify it under
236 the same terms as the Perl 5 programming language system itself.
237  
238 =cut
« v1.pm ↑ Summary v1/Role/REST/Client.pm »