vCloudDirector/Object.pm

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