Interchange6/Routes/Cart.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 122 CC 14 SCAR 49.9
CC
14
Cov
0%
cart_route
210.0
BEGIN
2.0
BEGIN
2.0
CRAP 147.6
1 package Dancer::Plugin::Interchange6::Routes::Cart;
2  
3 use Data::Dumper::Concise;
4 use Dancer ':syntax';
5 use Dancer::Plugin;
6 use Dancer::Plugin::Interchange6;
7 use Dancer::Plugin::Auth::Extensible;
8 use Try::Tiny;
9  
10 =head1 NAME
11  
12 Dancer::Plugin::Interchange6::Routes::Cart - Cart routes for Interchange6 Shop Machine
13  
14 =cut
15  
16 register_hook 'before_cart_display';
17  
18 =head1 FUNCTIONS
19  
20 =head2 cart_route
21  
22 Returns the cart route based on the passed routes configuration.
23  
24 =cut
25  
26 sub cart_route {
27 my $routes_config = shift;
28  
29 return sub {
30 my %values;
31 my ($input, $product, $cart, $cart_name, $cart_input,
32 $cart_product, $roles, @errors);
33  
34 if ($cart_name = param('cart') && scalar($cart_name)) {
35 $cart = cart($cart_name);
36 }
37 else {
38 $cart = cart;
39 }
40  
41 debug "cart_route cart name: " . $cart->name;
42  
43 if ( param('remove') ) {
44  
45 # removing item from cart
46  
47 try {
48 $cart->remove( param('remove') );
49 # if GET then URL now contains ugly query params so redirect
50 return redirect '/cart' if request->is_get;
51 }
52 catch {
53 warning "Cart remove error: $_";
54 push @errors, "Failed to remove product from cart: $_";
55 };
56 }
57 elsif ( param('update') && param('quantity') ) {
58  
59 # update existing cart product
60  
61 debug "Update "
62 . param('update')
63 . " with quantity "
64 . param('quantity');
65  
66 try {
67 $cart->update( param('update') => param('quantity') );
68 }
69 catch {
70 warning "Update cart product error: $_";
71 push @errors, "Failed to update product in cart: $_";
72 };
73 }
74  
75 if ( $input = param('sku') ) {
76  
77 # add new product
78  
79 # we currently only support one product at a time so check that
80 # the param is a scalar
81  
82 if ( ref($input) eq '' ) {
83  
84 $product = shop_product($input);
85  
86 unless ( defined $product ) {
87 warning "sku not found in POST /cart: $input";
88 session shop_cart_error =>
89 { message => "Product not found with sku: $input" };
90 return redirect '/';
91 }
92  
93 # store params in hash
94 my %params = params;
95  
96 if ( defined $product->canonical_sku ) {
97  
98 # this is a variant so we need to add in variant info
99 # into %params if missing
100  
101 my $rset = $product->product_attributes->search(
102 {
103 'attribute.type' => 'variant',
104 },
105 {
106 prefetch => [
107 'attribute',
108 {
109 product_attribute_values =>
110 'attribute_value'
111 }
112 ],
113 }
114 );
115 while ( my $result = $rset->next ) {
116 my $name = $result->attribute->name;
117 # WTF! why do we get a resultset of pavs? Surely there
118 # should be only one related pav for pa?
119 my $value = $result->product_attribute_values->first
120 ->attribute_value->value;
121 $params{$name} = $value unless defined $params{$name};
122  
123 }
124 }
125 # retrieve product attributes for possible variants
126 my $attr_ref = $product->attribute_iterator( hashref => 1 );
127 my %user_input;
128  
129 if ( keys %$attr_ref ) {
130  
131 for my $name ( keys %$attr_ref ) {
132 $user_input{$name} = $params{$name};
133 }
134  
135 debug "Attributes for $input: ", $attr_ref,
136 ", user input: ", \%user_input;
137 my %match_info;
138  
139 unless ( $cart_product =
140 $product->find_variant( \%user_input, \%match_info ) )
141 {
142 warning "Variant not found for ", $product->sku;
143 session shop_cart_error => {
144 message => 'Variant not found.',
145 info => \%match_info
146 };
147 return redirect $product->uri;
148 }
149 }
150 else {
151 # product without variants
152 $cart_product = $product;
153 }
154  
155 my $quantity = 1;
156 if ( param('quantity') ) {
157 $quantity = param('quantity');
158 }
159  
160 try {
161 $cart->add(
162 {
163 sku => $cart_product->sku,
164 quantity => $quantity
165 }
166 );
167 }
168 catch {
169 warning "Cart add error: $_";
170 push @errors, "Failed to add product to cart: $_";
171 };
172 }
173 }
174  
175 # add stuff useful for cart display
176 $values{cart_subtotal} = $cart->subtotal;
177 $values{cart_total} = $cart->total;
178 $values{cart} = $cart->products;
179 $values{cart_error} = join(". ", @errors) if scalar @errors;
180  
181 # call before_cart_display route so template tokens
182 # can be injected
183 execute_hook('before_cart_display', \%values);
184  
185 template $routes_config->{cart}->{template}, \%values;
186 }
187 }
188  
189 1;