Interchange6/Cart.pm

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