Interchange6/Cart/Product.pm

filter: Statementstmt 100% 64 / 64 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 100% 94 / 94 CC 6 SCAR 3.6
CC
6
Cov
100%
_build_discount_percent
2.0
is_variant
2.0
is_canonical
2.0
CRAP 1.4
« Interchange6/Cart.pm ↑ Summary Interchange6/Cart/Cost.pm »
1 # Interchange6::Cart::Product - Interchange6 cart product class
2  
3 package Interchange6::Cart::Product;
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Types::Common::Numeric qw/PositiveInt PositiveOrZeroNum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Types::Common::String qw/NonEmptyStr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Types::Standard qw/Defined HashRef HasMethods InstanceOf Int Maybe Num Str Undef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use MooseX::CoverableModifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 with 'Interchange6::Role::Costs';
14 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Interchange6::Cart::Product - Cart product class for Interchange6 Shop Machine
19  
20 =head1 DESCRIPTION
21  
22 Cart product class for L<Interchange6>.
23  
24 See L<Interchange6::Role::Costs> for details of cost attributes and methods.
25  
26 =head1 ATTRIBUTES
27  
28 See also L<Interchange6::Role::Costs/ATTRIBUTES>.
29  
30 Each cart product has the following attributes:
31  
32 =head2 id
33  
34 Can be used by subclasses, e.g. primary key value for cart products in the database.
35  
36 =cut
37  
38 3 has id => (
39 is => 'ro',
40 isa => Int,
41 );
42  
43 =head2 cart
44  
45 A reference to the Cart object that this Cart::Product belongs to.
46  
47 =over
48  
49 =item Writer: C<set_cart>
50  
51 =back
52  
53 =cut
54  
55 3 has cart => (
56 is => 'ro',
57 isa => Maybe[InstanceOf ['Interchange6::Cart']],
58 default => undef,
59 writer => 'set_cart',
60 weak_ref => 1,
61 );
62  
63 =head2 name
64  
65 Product name is required.
66  
67 =cut
68  
69 3 has name => (
70 is => 'ro',
71 isa => NonEmptyStr,
72 required => 1,
73 );
74  
75 =head2 price
76  
77 Product price is required and a positive number or zero.
78  
79 Price is required, because you want to maintain the price that was valid at the time of adding to the cart. Should the price in the shop change in the meantime, it will maintain this price.
80  
81 =over
82  
83 =item Writer: C<set_price>
84  
85 =back
86  
87 =cut
88  
89 3 has price => (
90 is => 'ro',
91 isa => PositiveOrZeroNum,
92 required => 1,
93 writer => 'set_price',
94 );
95  
96 =head2 selling_price
97  
98 Selling price is the price after group pricing, tier pricing or promotional discounts have been applied. If it is not set then it defaults to L</price>.
99  
100 =over
101  
102 =item Writer: C<set_selling_price>
103  
104 =back
105  
106 =cut
107  
108 3 has selling_price => (
109 is => 'lazy',
110 isa => PositiveOrZeroNum,
111 writer => 'set_selling_price',
112 );
113  
114 sub _build_selling_price {
115 16 ▶ my $self = shift;
Subroutine _build_selling_price: called CC=1 SCAR=0.0
116 16 return $self->price;
117 }
118  
119 =head2 discount_percent
120  
121 This is the integer discount percentage calculated from the difference
122 between L</price> and L</selling_price>. This attribute should not normally
123 be set since as it is a calculated value.
124  
125 L</discount_percent> is cleared if either L</set_price> or
126 L<set_selling_price> methods are called.
127  
128 =cut
129  
130 3 has discount_percent => (
131 is => 'lazy',
132 clearer => 1
133 );
134  
135 sub _build_discount_percent {
136 3 ▶ my $self = shift;
Subroutine _build_discount_percent: called CC=2 SCAR=6.9
137 3 ▶ return 0 if $self->price == $self->selling_price;
Branch: 1 branch
BranchTrueFalseTotal
if $self->price == $self->selling_price 2 1 3
138 1 return int( ( $self->price - $self->selling_price ) / $self->price * 100 );
139 }
140  
141 after 'set_price', 'set_selling_price' => sub {
142 5 ▶ shift->clear_discount_percent;
Subroutine __ANON__: called CC=1 SCAR=0.0
143 3 };
144  
145 =head2 quantity
146  
147 Product quantity is optional and has to be a natural number greater
148 than zero. Default for quantity is 1.
149  
150 =cut
151  
152 3 has quantity => (
153 is => 'ro',
154 isa => PositiveInt,
155 default => 1,
156 writer => 'set_quantity',
157 );
158  
159 after quantity => sub {
160 97 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
161 97 $self->clear_subtotal;
162 97 $self->clear_total;
163 3 };
164  
165 =head2 sku
166  
167 Unique product identifier is required.
168  
169 =cut
170  
171 3 has sku => (
172 is => 'ro',
173 isa => NonEmptyStr,
174 required => 1,
175 );
176  
177 =head2 canonical_sku
178  
179 If this product is a variant of a "parent" product then C<canonical_sku>
180 is the sku of the parent product.
181  
182 =cut
183  
184 3 has canonical_sku => (
185 is => 'ro',
186 default => undef,
187 );
188  
189 =head2 subtotal
190  
191 Subtotal calculated as L</price> * L</quantity>. Lazy set via builder.
192  
193 =cut
194  
195 3 has subtotal => (
196 is => 'lazy',
197 isa => Num,
198 clearer => 1,
199 predicate => 1,
200 );
201  
202 sub _build_subtotal {
203 33 ▶ my $self = shift;
Subroutine _build_subtotal: called CC=1 SCAR=0.0
204 33 return sprintf( "%.2f", $self->selling_price * $self->quantity);
205 }
206  
207 =head2 uri
208  
209 Product uri
210  
211 =cut
212  
213 3 has uri => (
214 is => 'ro',
215 isa => Str,
216 );
217  
218 =head2 weight
219  
220 Weight of quantity 1 of this product.
221  
222 =cut
223  
224 3 has weight => (
225 is => 'ro',
226 isa => Num,
227 writer => 'set_weight',
228 );
229  
230 =head2 extra
231  
232 Hash reference of extra things the cart product might want to store such as:
233  
234 =over
235  
236 =item * variant attributes in order to be able to change variant within cart
237  
238 =item * simple attributes to allow display of them within cart
239  
240 =back
241  
242 =cut
243  
244 has extra => (
245 is => 'ro',
246 isa => HashRef,
247 37 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
248 3 handles_via => 'Hash',
249 handles => {
250 get_extra => 'get',
251 set_extra => 'set',
252 delete_extra => 'delete',
253 keys_extra => 'keys',
254 clear_extra => 'clear',
255 exists_extra => 'exists',
256 defined_extra => 'defined',
257 },
258 );
259  
260 =head1 METHODS
261  
262 See also L<Interchange6::Role::Costs/METHODS>.
263  
264 =head2 L</extra> methods
265  
266 =over
267  
268 =item * get_extra($key, $key2, $key3...)
269  
270 See L<Data::Perl::Role::Collection::Hash/get>
271  
272 =item * set_extra($key => $value, $key2 => $value2...)
273  
274 See L<Data::Perl::Role::Collection::Hash/set>
275  
276 =item * delete_extra($key, $key2, $key3...)
277  
278 See L<Data::Perl::Role::Collection::Hash/set>
279  
280 =item * keys_extra
281  
282 See L<Data::Perl::Role::Collection::Hash/keys>
283  
284 =item * clear_extra
285  
286 See L<Data::Perl::Role::Collection::Hash/clear>
287  
288 =item * exists_extra($key)
289  
290 See L<Data::Perl::Role::Collection::Hash/exists>
291  
292 =item * defined_extra($key)
293  
294 See L<Data::Perl::Role::Collection::Hash/defined>
295  
296 =back
297  
298 =head2 L</subtotal> methods
299  
300 =over
301  
302 =item * clear_subtotal
303  
304 Clears L</subtotal>.
305  
306 =item * has_subtotal
307  
308 predicate on L</subtotal>.
309  
310 =back
311  
312 =head2 is_variant
313  
314 Returns 1 if L</canonical_sku> is defined else 0.
315  
316 =cut
317  
318 sub is_variant {
319 2 ▶ return defined shift->canonical_sku ? 1 : 0;
Subroutine is_variant: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined((shift())->canonical_sku) ? : 1 1 2
320 }
321  
322 =head2 is_canonical
323  
324 Returns 0 if L</canonical_sku> is defined else 1.
325  
326 =cut
327  
328 sub is_canonical {
329 2 ▶ return defined shift->canonical_sku ? 0 : 1;
Subroutine is_canonical: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined((shift())->canonical_sku) ? : 1 1 2
330 }
331  
332 # after cost changes we need to clear the cart subtotal/total
333 # our own total is handled by the Costs role
334  
335 after 'clear_costs', 'cost_set', 'cost_push', 'set_quantity' => sub {
336 12 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
337 12 ▶ if ( $self->cart ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cart) 8 4 12
338 8 $self->cart->clear_subtotal;
339 8 $self->cart->clear_total;
340 }
341 3 };
342 after 'set_quantity', 'set_weight' => sub {
343 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
344 8 ▶ if ( $self->cart ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cart) 4 4 8
345 4 $self->cart->clear_weight;
346 }
347 3 };
348  
349 3 1;
« Interchange6/Cart.pm ↑ Summary Interchange6/Cart/Cost.pm »