blib/lib/Webservice/OVH/Order/Cart/Item.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 169 SCAR 60.4
CC
20
Cov
0%
_new
42.0
configuration_add
30.0
configuration_delete
20.0
CRAP 420.0
1 package Webservice::OVH::Order::Cart::Item;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Webservice::OVH::Order::Cart::Item
8  
9 =head1 SYNOPSIS
10  
11 use Webservice::OVH;
12  
13 my $ovh = Webservice::OVH->new_from_json("credentials.json");
14  
15 my $cart = $ovh->order->new_cart(ovh_subsidiary => 'DE');
16  
17 my $items = $cart->items;
18  
19 =head1 DESCRIPTION
20  
21 Provides info for a specific cart item.
22  
23 =head1 METHODS
24  
25 =cut
26  
27 use strict;
28 use warnings;
29 use Carp qw{ carp croak };
30  
31 our $VERSION = 0.47;
32  
33 =head2 _new
34  
35 Internal Method to create the Item object.
36 This method is not ment to be called directly.
37  
38 =over
39  
40 =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $item_id - api id
41  
42 =item * Return: L<Webservice::OVH::Order::Cart>
43  
44 =item * Synopsis: Webservice::OVH::Order::Cart->_new($ovh_api_wrapper, $cart_id, $module);
45  
46 =back
47  
48 =cut
49  
50 sub _new {
51  
52 my ( $class, %params ) = @_;
53  
54 die "Missing module" unless $params{module};
55 die "Missing wrapper" unless $params{wrapper};
56 die "Missing id" unless $params{id};
57 die "Missing cart" unless $params{cart};
58  
59 my $module = $params{module};
60 my $api_wrapper = $params{wrapper};
61 my $item_id = $params{id};
62 my $cart = $params{cart};
63  
64 my $cart_id = $cart->id;
65 my $response = $api_wrapper->rawCall( method => 'get', path => "/order/cart/$cart_id/item/$item_id", noSignature => 0 );
66 croak $response->error if $response->error;
67  
68 my $porperties = $response->content;
69  
70 my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $item_id, _properties => $porperties, _cart => $cart }, $class;
71  
72 return $self;
73 }
74  
75 =head2 is_valid
76  
77 When the item is deleted on the api side, this method returns 0.
78  
79 =over
80  
81 =item * Return: VALUE
82  
83 =item * Synopsis: print "Valid" if $item->is_valid;
84  
85 =back
86  
87 =cut
88  
89 sub is_valid {
90  
91 my ($self) = @_;
92  
93 return $self->{_valid};
94 }
95  
96 =head2 _is_valid
97  
98 Intern method to check validity.
99 Difference is that this method carps an error.
100  
101 =over
102  
103 =item * Return: VALUE
104  
105 =item * Synopsis: $item->_is_valid;
106  
107 =back
108  
109 =cut
110  
111 sub _is_valid {
112  
113 my ($self) = @_;
114  
115 my $item_id = $self->id;
116 carp "Item $item_id is not valid anymore" unless $self->is_valid;
117 return $self->is_valid;
118 }
119  
120 =head2 _is_valid
121  
122 Gets the associated cart.
123  
124 =over
125  
126 =item * Return: L<Webservice::OVH::Order::Cart>
127  
128 =item * Synopsis: my $cart = $item->cart;
129  
130 =back
131  
132 =cut
133  
134 sub cart {
135  
136 my ($self) = @_;
137  
138 return $self->{_cart};
139 }
140  
141 =head2 id
142  
143 Returns the api id.
144  
145 =over
146  
147 =item * Return: VALUE
148  
149 =item * Synopsis: my $id = $item->id;
150  
151 =back
152  
153 =cut
154  
155 sub id {
156  
157 my ($self) = @_;
158  
159 return $self->{_id};
160 }
161  
162 =head2 properties
163  
164 Retrieves properties.
165 This method updates the intern property variable.
166  
167 =over
168  
169 =item * Return: HASH
170  
171 =item * Synopsis: my $properties = $item->properties;
172  
173 =back
174  
175 =cut
176  
177 sub properties {
178  
179 my ($self) = @_;
180  
181 return unless $self->_is_valid;
182  
183 my $api = $self->{_api_wrapper};
184 my $cart_id = $self->{_cart}->id;
185 my $item_id = $self->id;
186 my $response = $api->rawCall( method => 'get', path => "/order/cart/$cart_id/item/$item_id", noSignature => 0 );
187 croak $response->error if $response->error;
188  
189 $self->{_properties} = $response->content;
190 return $self->{_properties};
191 }
192  
193 =head2 configuration_add
194  
195 Setup configuration item for the product
196  
197 =over
198  
199 =item * Return: L<HASHREF>
200  
201 =item * Synopsis: my $configurations = $item->configuration_add;
202  
203 =back
204  
205 =cut
206  
207 sub configuration_add {
208  
209 my ($self, $label, $value) = @_;
210  
211 return unless $self->_is_valid;
212  
213 my $api = $self->{_api_wrapper};
214 my $cart_id = $self->{_cart}->id;
215 my $item_id = $self->id;
216  
217 croak "Missing label" unless $label;
218 croak "Missing value" unless $value;
219  
220 my $body = {label => $label, value => $value};
221 my $response = $api->rawCall( method => 'post', path => "/order/cart/$cart_id/item/$item_id/configuration", body => $body, noSignature => 0 );
222 croak $response->error if $response->error;
223  
224 return $response->content;
225 }
226  
227 =head2 configuration_delete
228  
229 Delete configuration item
230  
231 =over
232  
233 =item * Return: L<HASHREF>
234  
235 =item * Synopsis: my $configurations = $item->configuration_delete;
236  
237 =back
238  
239 =cut
240  
241 sub configuration_delete {
242  
243 my ($self, $configuration_id) = @_;
244  
245 return unless $self->_is_valid;
246  
247 my $api = $self->{_api_wrapper};
248 my $cart_id = $self->{_cart}->id;
249 my $item_id = $self->id;
250  
251 croak "Missing configuration_id" unless $configuration_id;
252  
253 my $response = $api->rawCall( method => 'delete', path => "/order/cart/$cart_id/item/$item_id/configuration/$configuration_id", body => {}, noSignature => 0 );
254 croak $response->error if $response->error;
255  
256 return $response->content;
257 }
258  
259 =head2 configurations
260  
261 Exposed property value.
262  
263 =over
264  
265 =item * Return: L<ARRAY>
266  
267 =item * Synopsis: my $configurations = $item->configurations;
268  
269 =back
270  
271 =cut
272  
273 sub configurations {
274  
275 my ($self) = @_;
276  
277 return $self->{_properties}->{configurations};
278 }
279  
280 =head2 duration
281  
282 Exposed property value.
283  
284 =over
285  
286 =item * Return: VALUE
287  
288 =item * Synopsis: my $duration = $item->duration;
289  
290 =back
291  
292 =cut
293  
294 sub duration {
295  
296 my ($self) = @_;
297  
298 return $self->{_properties}->{duration};
299 }
300  
301 =head2 offer_id
302  
303 Exposed property value.
304  
305 =over
306  
307 =item * Return: VALUE
308  
309 =item * Synopsis: my $offer_id = $item->offer_id;
310  
311 =back
312  
313 =cut
314  
315 sub offer_id {
316  
317 my ($self) = @_;
318  
319 return $self->{_properties}->{offerId};
320 }
321  
322 =head2 options
323  
324 Exposed property value.
325  
326 =over
327  
328 =item * Return: HASH
329  
330 =item * Synopsis: my $options = $item->options;
331  
332 =back
333  
334 =cut
335  
336 sub options {
337  
338 my ($self) = @_;
339  
340 return $self->{_properties}->{options};
341 }
342  
343 =head2 prices
344  
345 Exposed property value.
346  
347 =over
348  
349 =item * Return: HASH
350  
351 =item * Synopsis: my $prices = $item->prices;
352  
353 =back
354  
355 =cut
356  
357 sub prices {
358  
359 my ($self) = @_;
360  
361 return $self->{_properties}->{prices};
362 }
363  
364 =head2 product_id
365  
366 Exposed property value.
367  
368 =over
369  
370 =item * Return: VALUE
371  
372 =item * Synopsis: my $product_id = $item->product_id;
373  
374 =back
375  
376 =cut
377  
378 sub product_id {
379  
380 my ($self) = @_;
381  
382 return $self->{_properties}->{productId};
383 }
384  
385 =head2 settings
386  
387 Exposed property value.
388  
389 =over
390  
391 =item * Return: HASH
392  
393 =item * Synopsis: my $settings = $item->settings;
394  
395 =back
396  
397 =cut
398  
399 sub settings {
400  
401 my ($self) = @_;
402  
403 return $self->{_properties}->{settings};
404 }
405  
406 =head2 available_configuration
407  
408 Exposed property value.
409  
410 =over
411  
412 =item * Return: L<ARRAY>
413  
414 =item * Synopsis: my $available_configuration = $item->available_configuration;
415  
416 =back
417  
418 =cut
419  
420 sub available_configuration {
421  
422 my ($self) = @_;
423  
424 return unless $self->_is_valid;
425  
426 my $api = $self->{_api_wrapper};
427 my $cart_id = $self->{_cart}->id;
428 my $item_id = $self->id;
429  
430 my $response = $api->rawCall( method => 'get', path => "/order/cart/$cart_id/item/$item_id/configuration", noSignature => 0 );
431 croak $response->error if $response->error;
432  
433 return $response->content;
434 }
435  
436 =head2 delete
437  
438 Deletes the item and sets the object to invalid.
439  
440 =over
441  
442 =item * Synopsis: $item->delete;
443  
444 =back
445  
446 =cut
447  
448 sub delete {
449  
450 my ($self) = @_;
451  
452 return unless $self->_is_valid;
453  
454 my $api = $self->{_api_wrapper};
455 my $cart_id = $self->{_cart}->id;
456 my $item_id = $self->id;
457 my $response = $api->rawCall( method => 'delete', path => "/order/cart/$cart_id/item/$item_id", noSignature => 0 );
458 croak $response->error if $response->error;
459  
460 $self->{_valid} = 0;
461 }
462  
463 1;