Total Complexity | 43 |
Total Lines | 378 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like SilvershopJsonResponse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SilvershopJsonResponse, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class SilvershopJsonResponse extends Extension |
||
24 | { |
||
25 | /** |
||
26 | * Allow get action to obtain a copy of the shopping cart |
||
27 | */ |
||
28 | private static $allowed_actions = array( |
||
|
|||
29 | 'get' |
||
30 | ); |
||
31 | |||
32 | /** |
||
33 | * get the shopping cart |
||
34 | * |
||
35 | * @param SS_HTTPRequest $request |
||
36 | * @return SS_HTTPResponse $response with JSON body |
||
37 | */ |
||
38 | public function get(HTTPRequest $request) |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Add one of an item to a cart (Category Page) |
||
55 | * |
||
56 | * @see 'add' function of ShoppingCart_Controller ($this->owner) |
||
57 | * @param SS_HTTPRequest $request |
||
58 | * @param SS_HTTPResponse $response |
||
59 | * @param Buyable $product [optional] |
||
60 | * @param int $quantity [optional] |
||
61 | */ |
||
62 | public function updateAddResponse(&$request, &$response, $product = null, $quantity = 1) |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Remove one of an item from a cart (Cart Page) |
||
87 | * |
||
88 | * @see 'remove' function of ShoppingCart_Controller ($this->owner) |
||
89 | * @param SS_HTTPRequest $request |
||
90 | * @param SS_HTTPResponse $response |
||
91 | * @param Buyable $product [optional] |
||
92 | * @param int $quantity [optional] |
||
93 | */ |
||
94 | public function updateRemoveResponse(&$request, &$response, $product = null, $quantity = 1) |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Remove all of an item from a cart (Cart Page) |
||
119 | * Quantity is NIL |
||
120 | * |
||
121 | * @see 'removeall' function of ShoppingCart_Controller ($this->owner) |
||
122 | * @param SS_HTTPRequest $request |
||
123 | * @param SS_HTTPResponse $response |
||
124 | * @param Buyable $product [optional] |
||
125 | */ |
||
126 | public function updateRemoveAllResponse(&$request, &$response, $product = null) |
||
127 | { |
||
128 | if ($request->isAjax()) { |
||
129 | if (!$response) { |
||
130 | $response = $this->owner->getResponse(); |
||
131 | } |
||
132 | $response->removeHeader('Content-Type'); |
||
133 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
134 | $shoppingcart = ShoppingCart::curr(); |
||
135 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
136 | |||
137 | $data = $this->getCurrentShoppingCart(); |
||
138 | $data['message'] = [ |
||
139 | 'content' => $this->owner->cart->getMessage(), |
||
140 | 'type' => $this->owner->cart->getMessageType() |
||
141 | ]; |
||
142 | $this->owner->cart->clearMessage(); |
||
143 | |||
144 | $this->owner->extend('updateRemoveAllResponseShopJsonResponse', $data, $request, $response, $product); |
||
145 | $response->setBody(json_encode($data)); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Update the quantity of an item in a cart (Cart Page) |
||
151 | * |
||
152 | * @see 'setquantity' function of ShoppingCart_Controller ($this->owner) |
||
153 | * @param SS_HTTPRequest $request |
||
154 | * @param SS_HTTPResponse $response |
||
155 | * @param Buyable $product [optional] |
||
156 | * @param int $quantity [optional] |
||
157 | */ |
||
158 | public function updateSetQuantityResponse(&$request, &$response, $product = null, $quantity = 1) |
||
159 | { |
||
160 | if ($request->isAjax()) { |
||
161 | if (!$response) { |
||
162 | $response = $this->owner->getResponse(); |
||
163 | } |
||
164 | $response->removeHeader('Content-Type'); |
||
165 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
166 | $shoppingcart = ShoppingCart::curr(); |
||
167 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
168 | |||
169 | $currentquantity = (int) $product->Item()->Quantity; // quantity of the order item left now in the cart |
||
170 | |||
171 | $data = $this->getCurrentShoppingCart(); |
||
172 | $data['message'] = [ |
||
173 | 'content' => $this->owner->cart->getMessage(), |
||
174 | 'type' => $this->owner->cart->getMessageType() |
||
175 | ]; |
||
176 | $this->owner->cart->clearMessage(); |
||
177 | |||
178 | $this->owner->extend('updateSetQuantityResponseShopJsonResponse', $data, $request, $response, $product, $quantity); |
||
179 | $response->setBody(json_encode($data)); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Clear all items from the cart (Cart Page) |
||
185 | * |
||
186 | * @see 'clear' function of ShoppingCart_Controller ($this->owner) |
||
187 | * @param SS_HTTPRequest $request |
||
188 | * @param SS_HTTPResponse $response |
||
189 | */ |
||
190 | public function updateClearResponse(&$request, &$response) |
||
191 | { |
||
192 | if ($request->isAjax()) { |
||
193 | if (!$response) { |
||
194 | $response = $this->owner->getResponse(); |
||
195 | } |
||
196 | $response->removeHeader('Content-Type'); |
||
197 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
198 | |||
199 | $data = $this->getCurrentShoppingCart(); |
||
200 | $data['message'] = [ |
||
201 | 'content' => $this->owner->cart->getMessage(), |
||
202 | 'type' => $this->owner->cart->getMessageType() |
||
203 | ]; |
||
204 | $this->owner->cart->clearMessage(); |
||
205 | |||
206 | $this->owner->extend('updateClearResponseShopJsonResponse', $data, $request, $response); |
||
207 | $response->setBody(json_encode($data)); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Update the variations of a product (Cart Page) |
||
213 | * |
||
214 | * @see 'addtocart' function of VariationForm ($this->owner) |
||
215 | * @param SS_HTTPRequest $request |
||
216 | * @param SS_HTTPResponse $response |
||
217 | * @param Buyable $variation [optional] |
||
218 | * @param int $quantity [optional] |
||
219 | * @param VariationForm $form [optional] |
||
220 | */ |
||
221 | public function updateVariationFormResponse(&$request, &$response, $variation = null, $quantity = 1, $form = null) |
||
222 | { |
||
223 | if ($request->isAjax()) { |
||
224 | if (!$response) { |
||
225 | $response = $this->owner->getResponse(); |
||
226 | } |
||
227 | $response->removeHeader('Content-Type'); |
||
228 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
229 | $shoppingcart = ShoppingCart::curr(); |
||
230 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
231 | |||
232 | $data = $this->getCurrentShoppingCart(); |
||
233 | if ($form) { |
||
234 | $data['message'] = [ |
||
235 | 'content' => $form->getMessage(), |
||
236 | 'type' => $form->getMessageType() |
||
237 | ]; |
||
238 | $form->clearMessage(); |
||
239 | } |
||
240 | |||
241 | |||
242 | $this->owner->extend('updateVariationFormResponseShopJsonResponse', $data, $request, $response, $variation, $quantity, $form); |
||
243 | $response->setBody(json_encode($data)); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Add one of an item to a cart (Product Page) |
||
249 | * |
||
250 | * @see the addtocart function within AddProductForm class |
||
251 | * @param SS_HTTPRequest $request |
||
252 | * @param SS_HTTPResponse $response |
||
253 | * @param Buyable $buyable [optional] |
||
254 | * @param int $quantity [optional] |
||
255 | * @param AddProductForm $form [optional] |
||
256 | */ |
||
257 | public function updateAddProductFormResponse(&$request, &$response, $buyable, $quantity, $form) |
||
258 | { |
||
259 | if ($request->isAjax()) { |
||
260 | if (!$response) { |
||
261 | $response = $this->owner->getController()->getResponse(); |
||
262 | } |
||
263 | $response->removeHeader('Content-Type'); |
||
264 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
265 | $shoppingcart = ShoppingCart::curr(); |
||
266 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
267 | |||
268 | $data = $this->getCurrentShoppingCart(); |
||
269 | if ($form) { |
||
270 | $data['message'] = [ |
||
271 | 'content' => $form->getMessage(), |
||
272 | 'type' => $form->getMessageType() |
||
273 | ]; |
||
274 | $form->clearMessage(); |
||
275 | } |
||
276 | |||
277 | $this->owner->extend('updateAddProductFormResponseShopJsonResponse', $data, $request, $response, $buyable, $quantity, $form); |
||
278 | $response->setBody(json_encode($data)); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Provide a copy of the current order in the required format |
||
284 | * Note the id is the cart's id |
||
285 | * @return array of product id, subTotal, grandTotal, and items & modifiers |
||
286 | */ |
||
287 | public function getCurrentShoppingCart() |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Provide a copy of the current order's items, including image details and variations |
||
312 | * @return array |
||
313 | */ |
||
314 | protected function getCurrentShoppingCartItems() |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Provide a copy of the current order's modifiers |
||
369 | * @return array of modifiers (note: this excludes subtotal and grandtotal) |
||
370 | */ |
||
371 | protected function getCurrentShoppingCartModifiers() |
||
401 | } |
||
402 | } |
||
403 |