Interchange6/Cart/Product.pm

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