Interchange6/Cart.pm

filter: Statementstmt 98.4% 127 / 129 Branchbran 94.4% 34 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 23 / 23 Podpod 100% 6 / 6 totaltotal 97.9% 190 / 194 CC 24 SCAR 13.3
CC
24
Cov
98%
update
8.0
add
7.0
remove
4.0
CRAP 3.8
« Interchange6/Role/Costs.pm ↑ Summary Interchange6/Cart/Product.pm »
1 # Interchange6::Cart - Interchange6 cart class
2  
3 package Interchange6::Cart;
4  
5 =head1 NAME
6  
7 Interchange6::Cart - Cart class for Interchange6 Shop Machine
8  
9 =cut
10  
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Interchange6::Cart::Product;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Types::Common::String qw/NonEmptyStr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Types::Standard qw/ArrayRef InstanceOf Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use MooseX::CoverableModifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 with 'Interchange6::Role::Costs';
24 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 DESCRIPTION
27  
28 Generic cart class for L<Interchange6>.
29  
30 =head1 SYNOPSIS
31  
32 my $cart = Interchange6::Cart->new();
33  
34 $cart->add( sku => 'ABC', name => 'Foo', price => 23.45 );
35  
36 $cart->update( sku => 'ABC', quantity => 3 );
37  
38 my $product = Interchange::Cart::Product->new( ... );
39  
40 $cart->add($product);
41  
42 $cart->apply_cost( ... );
43  
44 my $total = $cart->total;
45  
46 =head1 ATTRIBUTES
47  
48 See also L<Interchange6::Role::Costs/ATTRIBUTES>.
49  
50 =head2 id
51  
52 Cart id can be used for subclasses, e.g. primary key value for carts in the database.
53  
54 =over
55  
56 =item Writer: C<set_id>
57  
58 =back
59  
60 =cut
61  
62 2 has id => (
63 is => 'ro',
64 isa => Str,
65 writer => 'set_id',
66 );
67  
68 =head2 name
69  
70 The cart name. Default is 'main'.
71  
72 =over
73  
74 =item Writer: C<rename>
75  
76 =back
77  
78 =cut
79  
80 2 has name => (
81 is => 'ro',
82 isa => NonEmptyStr,
83 default => 'main',
84 writer => 'rename',
85 );
86  
87 =head2 products
88  
89 Called without args returns a hash reference of L<Interchange6::Cart::Product>.
90  
91 Anything passed in as a value on object instantiation is ignored. To load
92 products into a cart the preferred methods are L</add> and L</seed> which
93 make sure appropriate arguements are passed.
94  
95 =cut
96  
97 has products => (
98 # rwp allows us to clear out products in seed via _set_products
99 # without disturbing what subclasses might expect of clear
100 is => 'rwp',
101 isa => ArrayRef [ InstanceOf ['Interchange6::Cart::Product'] ],
102 13 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
103 2 handles_via => 'Array',
104 handles => {
105 clear => 'clear',
106 count => 'count',
107 is_empty => 'is_empty',
108 product_get => 'get',
109 product_index => 'first_index',
110 products_array => 'elements',
111 product_delete => 'delete',
112 product_push => 'push',
113 product_set => 'set',
114 },
115 init_arg => undef,
116 );
117  
118 =head2 sessions_id
119  
120 The session ID for the cart.
121  
122 =over
123  
124 =item Writer: C<set_sessions_id>
125  
126 =back
127  
128 =cut
129  
130 2 has sessions_id => (
131 is => 'ro',
132 isa => Str,
133 clearer => 1,
134 writer => 'set_sessions_id',
135 );
136  
137 =head2 subtotal
138  
139 Returns current cart subtotal excluding costs.
140  
141 =cut
142  
143 2 has subtotal => (
144 is => 'lazy',
145 clearer => 1,
146 predicate => 1,
147 );
148  
149 sub _build_subtotal {
150 25 ▶ my $self = shift;
Subroutine _build_subtotal: called CC=2 SCAR=6.9
151  
152 25 my $subtotal = 0;
153 25 foreach my $product ( $self->products_array ) {
154 40 $subtotal += $product->total;
155 }
156  
157 25 return sprintf( "%.2f", $subtotal );
158 }
159  
160 after 'add', 'clear', 'product_push', 'product_set', 'product_delete', 'remove',
161 'seed', 'update' => sub {
162  
163 my $self = shift;
164 32 ▶ $self->clear_subtotal;
Subroutine __ANON__: called CC=1 SCAR=0.0
165 32 $self->clear_weight;
166 32 };
167 2  
168 after 'clear_subtotal' => sub {
169 shift->clear_total;
170 40 ▶};
Subroutine __ANON__: called CC=1 SCAR=0.0
171 2  
172 =head2 users_id
173  
174 The user id of the logged in user.
175  
176 =over
177  
178 =item Writer: C<set_users_id>
179  
180 =back
181  
182 =cut
183  
184 has users_id => (
185 2 is => 'ro',
186 isa => Str,
187 writer => 'set_users_id',
188 );
189  
190 =head2 weight
191  
192 Returns total weight of all products in the cart. If all products have
193 unedfined weight then this returns undef.
194  
195 =cut
196  
197 has weight => (
198 2 is => 'lazy',
199 clearer => 1,
200 predicate => 1,
201 );
202  
203 sub _build_weight {
204 my $self = shift;
205 17 ▶
Subroutine _build_weight: called CC=2 SCAR=6.9
206 my $weight = 0;
207 17 foreach my $product ( grep { defined $_->weight } $self->products_array ) {
208 17 $weight += $product->weight * $product->quantity;
209 25 }
210  
211 return $weight;
212 17 }
213  
214 =head1 METHODS
215  
216 See also L<Interchange6::Role::Costs/METHODS>.
217  
218 =head2 clear
219  
220 Removes all products from the cart.
221  
222 =head2 count
223  
224 Returns the number of different products in the shopping cart. If you have 5 apples and 6 pears it will return 2 (2 different products).
225  
226 =head2 is_empty
227  
228 Return boolean 1 or 0 depending on whether the cart is empty or not.
229  
230 =head2 product_delete($index)
231  
232 Deletes the product at the specified index.
233  
234 =head2 product_get($index)
235  
236 Returns the product at the specified index.
237  
238 =head2 product_index( sub {...})
239  
240 This method returns the index of the first matching product in the cart. The matching is done with a subroutine reference you pass to this method. The subroutine will be called against each element in the array until one matches or all elements have been checked.
241  
242 This method requires a single argument.
243  
244 my $index = $cart->product_index( sub { $_->sku eq 'ABC' } );
245  
246 =head2 product_push($product)
247  
248 Like Perl's normal C<push> this adds the supplied L<Interchange::Cart::Product>
249 to L</products>.
250  
251 =head2 product_set($index, $product)
252  
253 Sets the product at the specified index in L</products> to the supplied
254 L<Interchange::Cart::Product>.
255  
256 =head2 products_array
257  
258 Returns an array of Interchange::Cart::Product(s)
259  
260 =head2 new
261  
262 Inherited method. Returns a new Cart object.
263  
264 =head2 add($product)
265  
266 Add product to the cart. Returns product in case of success.
267  
268 The product is an L<Interchange6::Cart::Product> or a hash (reference) of product attributes that would be passed to Interchange6::Cart::Product->new().
269  
270 =cut
271  
272 sub add {
273 my $self = shift;
274 10 ▶ my $product = $_[0];
Subroutine add: called CC=7 SCAR=19.5
Pod documented
275 10 my ( $index, $oldproduct, $update );
276 10  
277 if ( !defined $product ) {
278 10 ▶ die "undefined argument passed to add";
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $product) { } 2 8 10
elsif (builtin::blessed($product)) { } 3 5 8
279 2 }
280 elsif ( blessed($product) ) {
281 die "product argument is not an Interchange6::Cart::Product"
282 3 ▶ unless ( $product->isa('Interchange6::Cart::Product') );
Branch: 1 branch
BranchTrueFalseTotal
unless $product->isa("Interchange6::Cart::Product") 1 2 3
283 }
284 else {
285  
286 my %args;
287 5  
288 if ( @_ % 2 ) {
289 5 ▶ if ( ref($product) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) { } 1 4 5
290 1 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if (ref $product eq 'HASH') { } 0 1 1
291 # copy args
292 %args = %{$product};
293 0 }
294 else {
295 die "argument to add should be hash or hashref";
296 1 }
297 }
298 else {
299  
300 %args = @_;
301 4 }
302  
303 $product = 'Interchange6::Cart::Product'->new(%args);
304 4 }
305  
306 # cart may already contain an product with the same sku
307 # if so then we add quantity to existing product otherwise we add new product
308  
309 $index = $self->product_index( sub { $_->sku eq $product->sku } );
310 6  
311 if ( $index >= 0 ) {
312 6 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0) { } 1 5 6
313 # product already exists in cart so we need to add new quantity to old
314  
315 $oldproduct = $self->product_get($index);
316 1  
317 $product->set_quantity( $oldproduct->quantity + $product->quantity );
318 1  
319 $self->product_set( $index, $product );
320 1  
321 $update = 1;
322 1 }
323 else {
324  
325 # a new product for this cart
326  
327 $product->set_cart($self);
328 5 $self->product_push($product);
329 5 }
330  
331 return $product;
332 6 }
333  
334 =head2 find
335  
336 Searches for an cart product with the given SKU.
337 Returns cart product in case of sucess or undef on failure.
338  
339 if ($product = $cart->find(9780977920174)) {
340 print "Quantity: $product->{quantity}.\n";
341 }
342  
343 =cut
344  
345 sub find {
346 my ( $self, $sku ) = @_;
347 10 ▶ 
Subroutine find: called CC=3 SCAR=11.0
Pod documented
348 for my $cartproduct ( $self->products_array ) {
349 10 if ( $sku eq $cartproduct->sku ) {
350 15 ▶ return $cartproduct;
Branch: 1 branch
BranchTrueFalseTotal
if ($sku eq $cartproduct->sku) 9 6 15
351 9 }
352 }
353  
354 return undef;
355 1 }
356  
357 =head2 has_subtotal
358  
359 predicate on L</subtotal>.
360  
361 =head2 has_total
362  
363 predicate on L</total>.
364  
365 =head2 has_weight
366  
367 predicate on L</weight>.
368  
369 =head2 quantity
370  
371 Returns the sum of the quantity of all products in the shopping cart,
372 which is commonly used as number of products. If you have 5 apples and 6 pears it will return 11.
373  
374 print 'Products in your cart: ', $cart->quantity, "\n";
375  
376 =cut
377  
378 sub quantity {
379 my $self = shift;
380 19 ▶ 
Subroutine quantity: called CC=2 SCAR=6.9
Pod documented
381 my $qty = 0;
382 19 foreach my $product ( $self->products_array ) {
383 19 $qty += $product->quantity;
384 26 }
385  
386 return $qty;
387 19 }
388  
389 =head2 remove($sku)
390  
391 Remove product from the cart. Takes SKU of product to identify the product.
392  
393 =cut
394  
395 sub remove {
396 my ( $self, $arg ) = @_;
397 5 ▶ 
Subroutine remove: called CC=4 SCAR=13.9
Pod documented
398 die "no argument passed to remove" unless defined $arg;
399 5 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 2 3 5
400 my $index = $self->product_index( sub { $_->sku eq $arg } );
401 3  
402 die "sku $arg not found in cart" unless $index >= 0;
403 3 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless $index >= 0 1 2 3
404 # remove product from our array
405 my $ret = $self->product_delete($index);
406 2 die "remove sku $arg failed" unless defined $ret;
407 2 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 2 2
408 return $ret;
409 2 }
410  
411 =head2 seed $product_ref
412  
413 Seeds products within the cart from $product_ref.
414  
415 B<NOTE:> use with caution since any existing products in the cart will be lost.
416  
417 $cart->seed([
418 { sku => 'BMX2015', price => 20, quantity = 1 },
419 { sku => 'KTM2018', price => 400, quantity = 5 },
420 { sku => 'DBF2020', price => 200, quantity = 5 },
421 ]);
422  
423 If any product fails to be added (for example bad product args) then an
424 exception is thrown and no products will be added to cart.
425  
426 On success returns L</products>.
427  
428 =cut
429  
430 sub seed {
431 my ( $self, $product_ref ) = @_;
432 4 ▶ 
Subroutine seed: called CC=3 SCAR=11.0
Pod documented
433 die "argument to seed must be an array reference"
434 4 ▶ unless ref($product_ref) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $product_ref eq "ARRAY" 1 3 4
435  
436 # we can't use ->clear since subclasses might wrap that to do
437 # interesting things like remove products from cart in database
438 $self->_set_products([]);
439 3  
440 my @products;
441 3 for my $args ( @{$product_ref} ) {
442 3 push @products, Interchange6::Cart::Product->new($args);
443 6 }
444  
445 # if we got here then all products are good so put them in the cart
446 $self->product_push(@products);
447 2  
448 return $self->products;
449 2 }
450  
451 =head2 update
452  
453 Update quantity of products in the cart.
454  
455 Parameters are pairs of SKUs and quantities, e.g.
456  
457 $cart->update(9780977920174 => 5,
458 9780596004927 => 3);
459  
460 A quantity of zero is equivalent to removing this product.
461  
462 Returns an array of updated products that are still in the cart.
463 Products removed via quantity 0 or products for which quantity has not
464 changed will not be returned.
465  
466 =cut
467  
468 sub update {
469 my ( $self, @args ) = @_;
470 13 ▶ my ( $sku, $qty, $product, $update, @products );
Subroutine update: called CC=8 SCAR=20.8
Pod documented
471 13  
472 ARGS: while ( @args > 0 ) {
473 13 ▶ $sku = shift @args;
Branch: 1 branch
BranchTrueFalseTotal
if (@args > 0) 13 8 21
474 14 $qty = shift @args;
475 14  
476 die "sku not defined in arg to update" unless defined $sku;
477 14 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sku 1 12 13
478 die "quantity not supplied as arg to update for sku $sku"
479 13 ▶ unless defined $qty;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qty 1 11 12
480  
481 die "quantity must be a positive integer or zero"
482 12 ▶ unless $qty =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $qty =~ /^\d+$/ 1 10 11
483  
484 unless ( $product = $self->find($sku) ) {
485 11 ▶ die "Product for $sku not found in cart.";
Branch: 1 branch
BranchTrueFalseTotal
unless ($product = $self->find($sku)) 1 9 10
486 2 }
487  
488 if ( $qty == 0 ) {
489 10 ▶ $self->remove($sku);
Branch: 1 branch
BranchTrueFalseTotal
if ($qty == 0) 1 8 9
490 1 next;
491 1 }
492  
493 # jump to next product if quantity stays the same
494 next ARGS if $qty == $product->quantity;
495 8 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if $qty == $product->quantity 2 6 8
496 $product->set_quantity($qty);
497 6 push @products, $product;
498 6 }
499 return @products;
500 8 }
501  
502 =head1 AUTHORS
503  
504 Stefan Hornburg (Racke), <racke@linuxia.de>
505 Peter Mottram (SysPete), <peter@sysnix.com>
506  
507 =head1 LICENSE AND COPYRIGHT
508  
509 Copyright 2011-2016 Stefan Hornburg (Racke) <racke@linuxia.de>.
510  
511 This program is free software; you can redistribute it and/or modify it
512 under the terms of either: the GNU General Public License as published
513 by the Free Software Foundation; or the Artistic License.
514  
515 See http://dev.perl.org/licenses/ for more information.
516  
517 =cut
518  
519 1;
« Interchange6/Role/Costs.pm ↑ Summary Interchange6/Cart/Product.pm »