| Total Complexity | 58 |
| Total Lines | 474 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 19 | class SilvershopJsonResponse extends Extension |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Allow get action to obtain a copy of the shopping cart |
||
| 23 | */ |
||
| 24 | private static $allowed_actions = array( |
||
|
|
|||
| 25 | 'get' |
||
| 26 | ); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * get the shopping cart |
||
| 30 | * |
||
| 31 | * @param SS_HTTPRequest $request |
||
| 32 | * @return SS_HTTPResponse $response with JSON body |
||
| 33 | */ |
||
| 34 | public function get(HTTPRequest $request) |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Add one of an item to a cart (Category Page) |
||
| 51 | * |
||
| 52 | * @see 'add' function of ShoppingCart_Controller ($this->owner) |
||
| 53 | * @param SS_HTTPRequest $request |
||
| 54 | * @param AjaxHTTPResponse $response |
||
| 55 | * @param Buyable $product [optional] |
||
| 56 | * @param int $quantity [optional] |
||
| 57 | */ |
||
| 58 | public function updateAddResponse(&$request, &$response, $product = null, $quantity = 1) |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Remove one of an item from a cart (Cart Page) |
||
| 115 | * |
||
| 116 | * @see 'remove' function of ShoppingCart_Controller ($this->owner) |
||
| 117 | * @param SS_HTTPRequest $request |
||
| 118 | * @param AjaxHTTPResponse $response |
||
| 119 | * @param Buyable $product [optional] |
||
| 120 | * @param int $quantity [optional] |
||
| 121 | */ |
||
| 122 | public function updateRemoveResponse(&$request, &$response, $product = null, $quantity = 1) |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Remove all of an item from a cart (Cart Page) |
||
| 158 | * Quantity is NIL |
||
| 159 | * |
||
| 160 | * @see 'removeall' function of ShoppingCart_Controller ($this->owner) |
||
| 161 | * @param SS_HTTPRequest $request |
||
| 162 | * @param AjaxHTTPResponse $response |
||
| 163 | * @param Buyable $product [optional] |
||
| 164 | */ |
||
| 165 | public function updateRemoveAllResponse(&$request, &$response, $product = null) |
||
| 166 | { |
||
| 167 | if ($request->isAjax()) { |
||
| 168 | if (!$response) { |
||
| 169 | $response = $this->owner->getResponse(); |
||
| 170 | } |
||
| 171 | $response->removeHeader('Content-Type'); |
||
| 172 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 173 | $shoppingcart = ShoppingCart::curr(); |
||
| 174 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 175 | |||
| 176 | $data = array( |
||
| 177 | 'id' => (string) $product->ID, |
||
| 178 | 'message' => array( |
||
| 179 | 'content' => $this->owner->cart->getMessage(), |
||
| 180 | 'type' => $this->owner->cart->getMessageType(), |
||
| 181 | ), |
||
| 182 | ); |
||
| 183 | $this->owner->cart->clearMessage(); |
||
| 184 | |||
| 185 | if ($shoppingcart) { |
||
| 186 | $data['subTotal'] = $shoppingcart->SubTotal(); |
||
| 187 | $data['grandTotal'] = $shoppingcart->GrandTotal(); |
||
| 188 | } |
||
| 189 | |||
| 190 | if ($modifiers = $this->getCurrentShoppingCartModifiers()) { |
||
| 191 | $data['modifiers'] = $modifiers; |
||
| 192 | } |
||
| 193 | |||
| 194 | $this->owner->extend('updateRemoveAllResponseShopJsonResponse', $data, $request, $response, $product); |
||
| 195 | $response->setBody(json_encode($data)); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Update the quantity of an item in a cart (Cart Page) |
||
| 201 | * |
||
| 202 | * @see 'setquantity' function of ShoppingCart_Controller ($this->owner) |
||
| 203 | * @param SS_HTTPRequest $request |
||
| 204 | * @param AjaxHTTPResponse $response |
||
| 205 | * @param Buyable $product [optional] |
||
| 206 | * @param int $quantity [optional] |
||
| 207 | */ |
||
| 208 | public function updateSetQuantityResponse(&$request, &$response, $product = null, $quantity = 1) |
||
| 209 | { |
||
| 210 | if ($request->isAjax()) { |
||
| 211 | if (!$response) { |
||
| 212 | $response = $this->owner->getResponse(); |
||
| 213 | } |
||
| 214 | $response->removeHeader('Content-Type'); |
||
| 215 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 216 | $shoppingcart = ShoppingCart::curr(); |
||
| 217 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 218 | |||
| 219 | $currentquantity = (int) $product->Item()->Quantity; // quantity of the order item left now in the cart |
||
| 220 | |||
| 221 | $data = array( |
||
| 222 | 'id' => (string) $product->ID, |
||
| 223 | 'quantity' => $currentquantity, |
||
| 224 | 'message' => array( |
||
| 225 | 'content' => $this->owner->cart->getMessage(), |
||
| 226 | 'type' => $this->owner->cart->getMessageType(), |
||
| 227 | ), |
||
| 228 | ); |
||
| 229 | $this->owner->cart->clearMessage(); |
||
| 230 | |||
| 231 | // include totals if required |
||
| 232 | if ($shoppingcart) { |
||
| 233 | $data['subTotal'] = $shoppingcart->SubTotal(); |
||
| 234 | $data['grandTotal'] = $shoppingcart->GrandTotal(); |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($modifiers = $this->getCurrentShoppingCartModifiers()) { |
||
| 238 | $data['modifiers'] = $modifiers; |
||
| 239 | } |
||
| 240 | |||
| 241 | $this->owner->extend('updateSetQuantityResponseShopJsonResponse', $data, $request, $response, $product, $currentquantity); |
||
| 242 | $response->setBody(json_encode($data)); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Clear all items from the cart (Cart Page) |
||
| 248 | * |
||
| 249 | * @see 'clear' function of ShoppingCart_Controller ($this->owner) |
||
| 250 | * @param SS_HTTPRequest $request |
||
| 251 | * @param AjaxHTTPResponse $response |
||
| 252 | */ |
||
| 253 | public function updateClearResponse(&$request, &$response) |
||
| 254 | { |
||
| 255 | if ($request->isAjax()) { |
||
| 256 | if (!$response) { |
||
| 257 | $response = $this->owner->getResponse(); |
||
| 258 | } |
||
| 259 | $response->removeHeader('Content-Type'); |
||
| 260 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 261 | |||
| 262 | $data = array( |
||
| 263 | 'message' => array( |
||
| 264 | 'content' => $this->owner->cart->getMessage(), |
||
| 265 | 'type' => $this->owner->cart->getMessageType(), |
||
| 266 | ), |
||
| 267 | ); |
||
| 268 | $this->owner->cart->clearMessage(); |
||
| 269 | |||
| 270 | $this->owner->extend('updateClearResponseShopJsonResponse', $data, $request, $response); |
||
| 271 | $response->setBody(json_encode($data)); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Update the variations of a product (Cart Page) |
||
| 277 | * |
||
| 278 | * @see 'addtocart' function of VariationForm ($this->owner) |
||
| 279 | * @param SS_HTTPRequest $request |
||
| 280 | * @param AjaxHTTPResponse $response |
||
| 281 | * @param Buyable $variation [optional] |
||
| 282 | * @param int $quantity [optional] |
||
| 283 | * @param VariationForm $form [optional] |
||
| 284 | */ |
||
| 285 | public function updateVariationFormResponse(&$request, &$response, $variation = null, $quantity = 1, $form = null) |
||
| 286 | { |
||
| 287 | if ($request->isAjax()) { |
||
| 288 | if (!$response) { |
||
| 289 | $response = $this->owner->getResponse(); |
||
| 290 | } |
||
| 291 | $response->removeHeader('Content-Type'); |
||
| 292 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 293 | $shoppingcart = ShoppingCart::curr(); |
||
| 294 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 295 | |||
| 296 | $data = array( |
||
| 297 | 'id' => (string) $variation->ID, |
||
| 298 | 'message' => array( |
||
| 299 | 'content' => $form->getMessage(), |
||
| 300 | 'type' => $form->getMessageType(), |
||
| 301 | ), |
||
| 302 | ); |
||
| 303 | $form->clearMessage(); |
||
| 304 | |||
| 305 | // include totals if required |
||
| 306 | if ($shoppingcart) { |
||
| 307 | $data['subTotal'] = $shoppingcart->SubTotal(); |
||
| 308 | $data['grandTotal'] = $shoppingcart->GrandTotal(); |
||
| 309 | } |
||
| 310 | |||
| 311 | if ($modifiers = $this->getCurrentShoppingCartModifiers()) { |
||
| 312 | $data['modifiers'] = $modifiers; |
||
| 313 | } |
||
| 314 | |||
| 315 | $this->owner->extend('updateVariationFormResponseShopJsonResponse', $data, $request, $response, $variation, $quantity, $form); |
||
| 316 | $response->setBody(json_encode($data)); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Add one of an item to a cart (Product Page) |
||
| 322 | * |
||
| 323 | * @see the addtocart function within AddProductForm class |
||
| 324 | * @param SS_HTTPRequest $request |
||
| 325 | * @param AjaxHTTPResponse $response |
||
| 326 | * @param Buyable $buyable [optional] |
||
| 327 | * @param int $quantity [optional] |
||
| 328 | * @param AddProductForm $form [optional] |
||
| 329 | */ |
||
| 330 | public function updateAddProductFormResponse(&$request, &$response, $buyable, $quantity, $form) |
||
| 331 | { |
||
| 332 | if ($request->isAjax()) { |
||
| 333 | if (!$response) { |
||
| 334 | $response = $this->owner->getController()->getResponse(); |
||
| 335 | } |
||
| 336 | $response->removeHeader('Content-Type'); |
||
| 337 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 338 | $shoppingcart = ShoppingCart::curr(); |
||
| 339 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 340 | |||
| 341 | $data = array( |
||
| 342 | 'id' => (string) $buyable->ID, |
||
| 343 | 'internalItemID' => $buyable->InternalItemID, |
||
| 344 | 'title' => $buyable->getTitle(), |
||
| 345 | 'url' => $buyable->URLSegment, |
||
| 346 | 'categories' => $buyable->getCategories()->column('Title'), |
||
| 347 | 'addLink' => $buyable->addLink(), |
||
| 348 | 'removeLink' => $buyable->removeLink(), |
||
| 349 | 'removeallLink' => $buyable->removeallLink(), |
||
| 350 | 'setquantityLink' => $buyable->Item()->setquantityLink(), |
||
| 351 | 'message' => array( |
||
| 352 | 'content' => $form->getMessage(), |
||
| 353 | 'type' => $form->getMessageType(), |
||
| 354 | ), |
||
| 355 | ); |
||
| 356 | $form->clearMessage(); |
||
| 357 | |||
| 358 | // include totals if required |
||
| 359 | if ($shoppingcart) { |
||
| 360 | $data['subTotal'] = $shoppingcart->SubTotal(); |
||
| 361 | $data['grandTotal'] = $shoppingcart->GrandTotal(); |
||
| 362 | } |
||
| 363 | |||
| 364 | if ($modifiers = $this->getCurrentShoppingCartModifiers()) { |
||
| 365 | $data['modifiers'] = $modifiers; |
||
| 366 | } |
||
| 367 | |||
| 368 | $this->owner->extend('updateAddProductFormResponseShopJsonResponse', $data, $request, $response, $buyable, $quantity, $form); |
||
| 369 | $response->setBody(json_encode($data)); |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Provide a copy of the current order in the required format |
||
| 375 | * Note the id is the cart's id |
||
| 376 | * @return array of product id, subTotal, grandTotal, and items & modifiers |
||
| 377 | */ |
||
| 378 | public function getCurrentShoppingCart() |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Provide a copy of the current order's items, including image details and variations |
||
| 401 | * @todo what about subTitles? i.e the variation choosen (I think) |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | protected function getCurrentShoppingCartItems() |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Provide a copy of the current order's modifiers |
||
| 460 | * @todo Only FlatTaxModifier tested |
||
| 461 | * @return array of modifiers (note: this excludes subtotal and grandtotal) |
||
| 462 | */ |
||
| 463 | protected function getCurrentShoppingCartModifiers() |
||
| 493 | } |
||
| 494 | } |
||
| 495 |