vCloudDirector2/Object.pm

filter: Statementstmt 49.0% 81 / 165 Branchbran 9.4% 7 / 74 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 68.2% 28 / 41 Podpod n/a totaltotal 36.9% 116 / 314 CC 51 SCAR 35.1
CC
51
Cov
34%
find_links
160.9
_create_object
43.8
build_sub_objects
32.7
CRAP 33.5
« vCloudDirector2/Link.pm ↑ Summary vCloudDirector2/ObjectContent.pm »
1 package VMware::vCloudDirector2::Object;
2  
3 # ABSTRACT: Module to contain an object!
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.100'; # VERSION
9 4 our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY
10  
11 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Ref::Util qw(is_plain_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Lingua::EN::Inflexion;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use VMware::vCloudDirector2::ObjectContent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # ------------------------------------------------------------------------
18  
19  
20 4 has api => (
21 is => 'ro',
22 isa => 'VMware::vCloudDirector2::API',
23 required => 1,
24 weak_ref => 1,
25 documentation => 'API we use'
26 );
27  
28 4 has content => (
29 is => 'ro',
30 isa => 'VMware::vCloudDirector2::ObjectContent',
31 predicate => 'has_content',
32 writer => '_set_content',
33 documentation => 'The underlying content object',
34 handles => [qw( mime_type href type name )],
35 );
36  
37 4 has _partial_object => ( is => 'rw', isa => 'Bool', default => 0 );
38  
39 # delegates that force a full object to be pulled
40 4 ▶method hash () { return $self->inflate->content->hash; }
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine hash: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
41 4 ▶method links () { return $self->inflate->content->links; }
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine links: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
42 4 ▶method id () { return $self->inflate->content->id; }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine id: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
43 4 ▶method uuid () { return ( split( /\//, $self->href ) )[-1]; }
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine uuid: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
44  
45 # ------------------------------------------------------------------------
46 4 ▶method BUILD ($args) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BUILD: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 6 6
if scalar @_ > 1 0 6 6
47  
48 $self->_set_content(
49 6 VMware::vCloudDirector2::ObjectContent->new( object => $self, hash => $args->{hash} ) );
50 }
51  
52 # ------------------------------------------------------------------------
53  
54  
55 4 ▶method inflate () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine inflate: called CC=3 SCAR=11.2
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
56 1 ▶ $self->refetch if ( $self->_partial_object );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_partial_object 1 0 1
57 1 return $self;
58 }
59  
60 # ------------------------------------------------------------------------
61 4 ▶method refetch () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine refetch: called CC=4 SCAR=14.6
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
62 1 my $hash = $self->api->GET_hash( $self->href );
63 1 $self->_set_content(
64 VMware::vCloudDirector2::ObjectContent->new( object => $self, hash => $hash ) );
65 1 ▶ $self->api->_debug(
Branch: 2 branches
BranchTrueFalseTotal
if $self->api->debug 0 1 1
$self->_partial_object ? : 0 0 0
66 sprintf(
67 'Object: %s a [%s]',
68 ( $self->_partial_object ? 'Inflated' : 'Refetched' ),
69 $self->type
70 )
71 ) if ( $self->api->debug );
72 1 $self->_partial_object(0);
73 1 return $self;
74 }
75  
76 # ------------------------------------------------------------------------
77  
78  
79 4 ▶method find_links (:$name, :$type, :$rel) {
Subroutine BEGIN: called CC=1 SCAR=5.1
Subroutine find_links: not called CC=13 SCAR=50.8
Branch: 2 branches
BranchTrueFalseTotal
if keys %args 0 0 0
if scalar @_ > 6 0 0 0
80 0 my @matched_links;
81 0 my $links = $self->links;
82 0 foreach my $link ( @{$links} ) {
83 0 ▶ if ( not( defined($rel) ) or ( $rel eq ( $link->rel || '' ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rel or $rel eq ($link->rel || "")) 0 0 0
Condition0%0/5
not defined $rel or $rel eq ($link->rel || "")
l!l&&r!l&&!r
000
$link->rel || ''
l!l
00
MC/DC0%0/2
not defined $rel or $rel eq ($link->rel || "")
not defined $rel $rel eq ($link->rel || "")
MC/DC0%0/1
$link->rel || ''
$link->rel
Truth table0%0/3
not(A) or B
A= not defined $relB= $rel eq ($link->rel || "")
ABresult
1X1
011
000
Truth table0%0/2
A
A= $link->rel
Aresult
11
00
84 0 ▶ if ( not( defined($type) ) or ( $type eq ( $link->type || '' ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type or $type eq ($link->type || "")) 0 0 0
Condition0%0/5
not defined $type or $type eq ($link->type || "")
l!l&&r!l&&!r
000
$link->type || ''
l!l
00
MC/DC0%0/2
not defined $type or $type eq ($link->type || "")
not defined $type $type eq ($link->type || "")
MC/DC0%0/1
$link->type || ''
$link->type
Truth table0%0/3
not(A) or B
A= not defined $typeB= $type eq ($link->type || "")
ABresult
1X1
011
000
Truth table0%0/2
A
A= $link->type
Aresult
11
00
85 0 ▶ if ( not( defined($name) ) or ( $name eq ( $link->name || '' ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $name or $name eq ($link->name || "")) 0 0 0
Condition0%0/5
not defined $name or $name eq ($link->name || "")
l!l&&r!l&&!r
000
$link->name || ''
l!l
00
MC/DC0%0/2
not defined $name or $name eq ($link->name || "")
not defined $name $name eq ($link->name || "")
MC/DC0%0/1
$link->name || ''
$link->name
Truth table0%0/3
not(A) or B
A= not defined $nameB= $name eq ($link->name || "")
ABresult
1X1
011
000
Truth table0%0/2
A
A= $link->name
Aresult
11
00
86 0 push( @matched_links, $link );
87 }
88 }
89 }
90 }
91 0 return @matched_links;
92 }
93  
94 # ------------------------------------------------------------------------
95  
96  
97 4 ▶method fetch_links (@search_items) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine fetch_links: not called CC=2 SCAR=13.0
98 0 my @matched_objects;
99 0 foreach my $link ( $self->find_links(@search_items) ) {
100 0 push( @matched_objects, $link->GET() );
101 }
102 0 return @matched_objects;
103 }
104  
105 # ------------------------------------------------------------------------
106 4 ▶method _create_object ($hash, $type='Thing') {
Subroutine BEGIN: called CC=1 SCAR=4.7
Subroutine _create_object: not called CC=7 SCAR=37.8
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
scalar @_ > 1 ? : 0 0 0
if scalar @_ > 2 0 0 0
107  
108 # if thing has Link content within it then it is a full object, otherwise it
109 # is just a stub
110 my $object = VMware::vCloudDirector2::Object->new(
111 hash => { $type => $hash },
112 api => $self->api,
113 0 ▶ _partial_object => ( exists( $hash->{Link} ) ) ? 0 : 1,
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'Link'} ? : 0 0 0
114 );
115 0 ▶ $self->api->_debug(
Branch: 2 branches
BranchTrueFalseTotal
if $self->api->debug 0 0 0
$object->_partial_object ? : 0 0 0
116 sprintf(
117 'Object: [%s] instantiated %s for [%s]',
118 $self->type, ( $object->_partial_object ? 'a stub' : 'an object' ),
119 $object->type
120 )
121 ) if ( $self->api->debug );
122 0 return $object;
123 }
124  
125 # ------------------------------------------------------------------------
126  
127  
128 4 ▶method build_sub_objects ($type) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine build_sub_objects: not called CC=6 SCAR=34.9
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
129 0 my @objects;
130 0 my $container_type = noun($type)->plural;
131 return
132 unless ( exists( $self->hash->{$container_type} )
133 0 ▶ and is_plain_hashref( $self->hash->{$container_type} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->hash->{$container_type} and is_plain_hashref($self->hash->{$container_type}) 0 0 0
Condition0%0/3
exists $self->hash->{$container_type} and is_plain_hashref($self->hash->{$container_type})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->hash->{$container_type} and is_plain_hashref($self->hash->{$container_type})
exists $self->hash->{$container_type} is_plain_hashref($self->hash->{$container_type})
Truth table0%0/3
A and B
A= exists $self->hash->{$container_type}B= is_plain_hashref($self->hash->{$container_type})
ABresult
0X0
100
111
134 0 foreach my $thing ( $self->_listify( $self->hash->{$container_type}{$type} ) ) {
135 0 push( @objects, $self->_create_object( $thing, $type ) );
136 }
137 0 return @objects;
138 }
139  
140 4 ▶method build_children_objects () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine build_children_objects: not called CC=6 SCAR=34.6
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
141 0 my $hash = $self->hash;
142 0 ▶ return unless ( exists( $hash->{Children} ) and is_plain_hashref( $hash->{Children} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"Children"} and is_plain_hashref($hash->{"Children"}) 0 0 0
Condition0%0/3
exists $hash->{"Children"} and is_plain_hashref($hash->{"Children"})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $hash->{"Children"} and is_plain_hashref($hash->{"Children"})
exists $hash->{"Children"} is_plain_hashref($hash->{"Children"})
Truth table0%0/3
A and B
A= exists $hash->{"Children"}B= is_plain_hashref($hash->{"Children"})
ABresult
0X0
100
111
143 0 my @objects;
144 0 foreach my $key ( keys %{ $hash->{Children} } ) {
145 0 foreach my $thing ( $self->_listify( $self->hash->{Children}{$key} ) ) {
146 0 push( @objects, $self->_create_object( $thing, $key ) );
147 }
148 }
149 0 return @objects;
150 }
151  
152 # ------------------------------------------------------------------------
153  
154  
155 4 ▶method DELETE () { return $self->api->DELETE( $self->href ); }
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine DELETE: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
156  
157  
158 4 ▶method GET () { return $self->api->GET( $self->href ); }
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine GET: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
159  
160  
161 4 ▶method POST ($hash) { return $self->api->POST( $self->href, $hash, $self->mime_type ); }
Subroutine BEGIN: called CC=1 SCAR=4.4
Subroutine POST: not called CC=3 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
162  
163  
164 4 ▶method PUT ($hash) { return $self->api->PUT( $self->href, $hash, $self->mime_type ); }
Subroutine BEGIN: called CC=1 SCAR=4.4
Subroutine PUT: not called CC=3 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
165  
166 # ------------------------------------------------------------------------
167 4 ▶method _listify ($thing) { !defined $thing ? () : ( ( ref $thing eq 'ARRAY' ) ? @{$thing} : $thing ) }
Subroutine BEGIN: called CC=1 SCAR=5.1
Subroutine _listify: not called CC=5 SCAR=30.8
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
!defined($thing) ? : 0 0 0
ref $thing eq 'ARRAY' ? : 0 0 0
168  
169 # ------------------------------------------------------------------------
170  
171 4 __PACKAGE__->meta->make_immutable;
172  
173 4 1;
174  
175 __END__
176  
177 =pod
178  
179 =encoding UTF-8
180  
181 =head1 NAME
182  
183 VMware::vCloudDirector2::Object - Module to contain an object!
184  
185 =head1 VERSION
186  
187 version 0.100
188  
189 =head2 Attributes
190  
191 =head3 api
192  
193 A weak link to the API object to be used.
194  
195 =head3 content
196  
197 The object content. This is in a separate container so that partial objects
198 passed can be inflated at a later stage without having to replace the object
199 itself.
200  
201 =head3 hash
202  
203 A reference to the hash returned from the vCloud API. Forces object inflation.
204  
205 =head3 links
206  
207 An array references to the links contained in this object. Forces object
208 inflation.
209  
210 =head3 id
211  
212 The id attribute from the returned vCloud JSON. Forces object inflation.
213  
214 =head2 Methods
215  
216 =head3 inflate
217  
218 If this object is a partial object (ie taken from a link or partial chunk
219 within a containing object), then this forces a refetch of the content from
220 vCloud creating a fully populated object.
221  
222 =head3 refetch
223  
224 Forces a refetch of this object's content unconditionally.
225  
226 =head3 find_links
227  
228 Returns any links found that match the search criteria. The possible criteria
229 are:-
230  
231 =over 4
232  
233 =item name
234  
235 The name of the link
236  
237 =item type
238  
239 The type of the link (short type, not full MIME type)
240  
241 =item rel
242  
243 The rel of the link
244  
245 =back
246  
247 The return value is a list of link objects.
248  
249 =head3 fetch_links
250  
251 As per L</find_links> except that each link found is fetched and expanded up as
252 an object.
253  
254 =head3 build_sub_objects
255  
256 Given a type (specifically a key used within the current object hash), grabs
257 the descendants of that key and instantiates them as partial objects (they can
258 then be inflated into full objects).
259  
260 Due to the structure of the XML there will always be two layers, the inner
261 named singular thing, and the outer named as the plural of thing. Hence this
262 does magic with the language inflection module.
263  
264 =head3 DELETE
265  
266 Make a delete request to the URL in this link. Returns Objects. Failure will
267 generate an exception. See L<VMware::vCloudDirector2::API/DELETE>.
268  
269 =head3 GET
270  
271 Make a get request to the URL in this link. Returns Objects. Failure will
272 generate an exception. See L<VMware::vCloudDirector2::API/GET>.
273  
274 =head3 POST
275  
276 Make a post request with the specified payload to the URL in this link. Returns
277 Objects. Failure will generate an exception. See
278 L<VMware::vCloudDirector2::API/POST>.
279  
280 =head3 PUT
281  
282 Make a put request with the specified payload to the URL in this link. Returns
283 Objects. Failure will generate an exception. See
284 L<VMware::vCloudDirector2::API/PUT>.
285  
286 =head1 AUTHOR
287  
288 Nigel Metheringham <nigelm@cpan.org>
289  
290 =head1 COPYRIGHT AND LICENSE
291  
292 This software is copyright (c) 2019 by Nigel Metheringham.
293  
294 This is free software; you can redistribute it and/or modify it under
295 the same terms as the Perl 5 programming language system itself.
296  
297 =cut
« vCloudDirector2/Link.pm ↑ Summary vCloudDirector2/ObjectContent.pm »