| Total Complexity | 52 |
| Total Lines | 450 |
| Duplicated Lines | 0 % |
| Changes | 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) |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Remove one of an item from a cart (Cart Page) |
||
| 111 | * |
||
| 112 | * @see 'remove' function of ShoppingCart_Controller ($this->owner) |
||
| 113 | * @param SS_HTTPRequest $request |
||
| 114 | * @param AjaxHTTPResponse $response |
||
| 115 | * @param Buyable $product [optional] |
||
| 116 | * @param int $quantity [optional] |
||
| 117 | */ |
||
| 118 | public function updateRemoveResponse(&$request, &$response, $product = null, $quantity = 1) |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Remove all of an item from a cart (Cart Page) |
||
| 150 | * Quantity is NIL |
||
| 151 | * |
||
| 152 | * @see 'removeall' function of ShoppingCart_Controller ($this->owner) |
||
| 153 | * @param SS_HTTPRequest $request |
||
| 154 | * @param AjaxHTTPResponse $response |
||
| 155 | * @param Buyable $product [optional] |
||
| 156 | */ |
||
| 157 | public function updateRemoveAllResponse(&$request, &$response, $product = null) |
||
| 158 | { |
||
| 159 | if ($request->isAjax()) { |
||
| 160 | if (!$response) { |
||
| 161 | $response = $this->owner->getResponse(); |
||
| 162 | } |
||
| 163 | $response->removeHeader('Content-Type'); |
||
| 164 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 165 | $shoppingcart = ShoppingCart::curr(); |
||
| 166 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 167 | |||
| 168 | $data = array( |
||
| 169 | 'id' => (string) $product->ID, |
||
| 170 | 'message' => array( |
||
| 171 | 'content' => $this->owner->cart->getMessage(), |
||
| 172 | 'type' => $this->owner->cart->getMessageType(), |
||
| 173 | ), |
||
| 174 | ); |
||
| 175 | $this->owner->cart->clearMessage(); |
||
| 176 | |||
| 177 | if ($shoppingcart) { |
||
| 178 | $data['subTotal'] = $shoppingcart->SubTotal(); |
||
| 179 | $data['grandTotal'] = $shoppingcart->GrandTotal(); |
||
| 180 | } |
||
| 181 | |||
| 182 | $this->owner->extend('updateRemoveAllResponseShopJsonResponse', $data, $request, $response, $product); |
||
| 183 | $response->setBody(json_encode($data)); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Update the quantity of an item in a cart (Cart Page) |
||
| 189 | * |
||
| 190 | * @see 'setquantity' function of ShoppingCart_Controller ($this->owner) |
||
| 191 | * @param SS_HTTPRequest $request |
||
| 192 | * @param AjaxHTTPResponse $response |
||
| 193 | * @param Buyable $product [optional] |
||
| 194 | * @param int $quantity [optional] |
||
| 195 | */ |
||
| 196 | public function updateSetQuantityResponse(&$request, &$response, $product = null, $quantity = 1) |
||
| 197 | { |
||
| 198 | if ($request->isAjax()) { |
||
| 199 | if (!$response) { |
||
| 200 | $response = $this->owner->getResponse(); |
||
| 201 | } |
||
| 202 | $response->removeHeader('Content-Type'); |
||
| 203 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 204 | $shoppingcart = ShoppingCart::curr(); |
||
| 205 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 206 | |||
| 207 | $currentquantity = (int) $product->Item()->Quantity; // quantity of the order item left now in the cart |
||
| 208 | |||
| 209 | $data = array( |
||
| 210 | 'id' => (string) $product->ID, |
||
| 211 | 'quantity' => $currentquantity, |
||
| 212 | 'message' => array( |
||
| 213 | 'content' => $this->owner->cart->getMessage(), |
||
| 214 | 'type' => $this->owner->cart->getMessageType(), |
||
| 215 | ), |
||
| 216 | ); |
||
| 217 | $this->owner->cart->clearMessage(); |
||
| 218 | |||
| 219 | // include totals if required |
||
| 220 | if ($shoppingcart) { |
||
| 221 | $data['subTotal'] = $shoppingcart->SubTotal(); |
||
| 222 | $data['grandTotal'] = $shoppingcart->GrandTotal(); |
||
| 223 | } |
||
| 224 | |||
| 225 | $this->owner->extend('updateSetQuantityResponseShopJsonResponse', $data, $request, $response, $product, $currentquantity); |
||
| 226 | $response->setBody(json_encode($data)); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Clear all items from the cart (Cart Page) |
||
| 232 | * |
||
| 233 | * @see 'clear' function of ShoppingCart_Controller ($this->owner) |
||
| 234 | * @param SS_HTTPRequest $request |
||
| 235 | * @param AjaxHTTPResponse $response |
||
| 236 | */ |
||
| 237 | public function updateClearResponse(&$request, &$response) |
||
| 238 | { |
||
| 239 | if ($request->isAjax()) { |
||
| 240 | if (!$response) { |
||
| 241 | $response = $this->owner->getResponse(); |
||
| 242 | } |
||
| 243 | $response->removeHeader('Content-Type'); |
||
| 244 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 245 | |||
| 246 | $data = array( |
||
| 247 | 'message' => array( |
||
| 248 | 'content' => $this->owner->cart->getMessage(), |
||
| 249 | 'type' => $this->owner->cart->getMessageType(), |
||
| 250 | ), |
||
| 251 | ); |
||
| 252 | $this->owner->cart->clearMessage(); |
||
| 253 | |||
| 254 | $this->owner->extend('updateClearResponseShopJsonResponse', $data, $request, $response); |
||
| 255 | $response->setBody(json_encode($data)); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Update the variations of a product (Cart Page) |
||
| 261 | * |
||
| 262 | * @see 'addtocart' function of VariationForm ($this->owner) |
||
| 263 | * @param SS_HTTPRequest $request |
||
| 264 | * @param AjaxHTTPResponse $response |
||
| 265 | * @param Buyable $variation [optional] |
||
| 266 | * @param int $quantity [optional] |
||
| 267 | * @param VariationForm $form [optional] |
||
| 268 | */ |
||
| 269 | public function updateVariationFormResponse(&$request, &$response, $variation = null, $quantity = 1, $form = null) |
||
| 270 | { |
||
| 271 | if ($request->isAjax()) { |
||
| 272 | if (!$response) { |
||
| 273 | $response = $this->owner->getResponse(); |
||
| 274 | } |
||
| 275 | $response->removeHeader('Content-Type'); |
||
| 276 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 277 | $shoppingcart = ShoppingCart::curr(); |
||
| 278 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 279 | |||
| 280 | $data = array( |
||
| 281 | 'id' => (string) $variation->ID, |
||
| 282 | 'message' => array( |
||
| 283 | 'content' => $form->Message(), |
||
| 284 | 'type' => $form->MessageType(), |
||
| 285 | ), |
||
| 286 | ); |
||
| 287 | $form->clearMessage(); |
||
| 288 | |||
| 289 | // include totals if required |
||
| 290 | if ($shoppingcart) { |
||
| 291 | $data['subTotal'] = $shoppingcart->SubTotal(); |
||
| 292 | $data['grandTotal'] = $shoppingcart->GrandTotal(); |
||
| 293 | } |
||
| 294 | |||
| 295 | $this->owner->extend('updateVariationFormResponseShopJsonResponse', $data, $request, $response, $variation, $quantity, $form); |
||
| 296 | $response->setBody(json_encode($data)); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Add one of an item to a cart (Product Page) |
||
| 302 | * |
||
| 303 | * @see the addtocart function within AddProductForm class |
||
| 304 | * @param SS_HTTPRequest $request |
||
| 305 | * @param AjaxHTTPResponse $response |
||
| 306 | * @param Buyable $buyable [optional] |
||
| 307 | * @param int $quantity [optional] |
||
| 308 | * @param AddProductForm $form [optional] |
||
| 309 | */ |
||
| 310 | public function updateAddProductFormResponse(&$request, &$response, $buyable, $quantity, $form) |
||
| 311 | { |
||
| 312 | if ($request->isAjax()) { |
||
| 313 | if (!$response) { |
||
| 314 | $response = $this->owner->getController()->getResponse(); |
||
| 315 | } |
||
| 316 | $response->removeHeader('Content-Type'); |
||
| 317 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 318 | $shoppingcart = ShoppingCart::curr(); |
||
| 319 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 320 | |||
| 321 | $data = array( |
||
| 322 | 'id' => (string) $buyable->ID, |
||
| 323 | 'internalItemID' => $buyable->InternalItemID, |
||
| 324 | 'title' => $buyable->Title, |
||
| 325 | 'url' => $buyable->URLSegment, |
||
| 326 | 'categories' => $buyable->getCategories()->column('Title'), |
||
| 327 | 'addLink' => $buyable->addLink(), |
||
| 328 | 'removeLink' => $buyable->removeLink(), |
||
| 329 | 'removeallLink' => $buyable->removeallLink(), |
||
| 330 | 'setquantityLink' => $buyable->Item()->setquantityLink(), |
||
| 331 | 'message' => array( |
||
| 332 | 'content' => $form->Message(), |
||
| 333 | 'type' => $form->MessageType(), |
||
| 334 | ), |
||
| 335 | ); |
||
| 336 | $form->clearMessage(); |
||
| 337 | |||
| 338 | // include totals if required |
||
| 339 | if ($shoppingcart) { |
||
| 340 | $data['subTotal'] = $shoppingcart->SubTotal(); |
||
| 341 | $data['grandTotal'] = $shoppingcart->GrandTotal(); |
||
| 342 | } |
||
| 343 | |||
| 344 | $this->owner->extend('updateAddProductFormResponseShopJsonResponse', $data, $request, $response, $buyable, $quantity, $form); |
||
| 345 | $response->setBody(json_encode($data)); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Provide a copy of the current order in the required format |
||
| 351 | * Note the id is the cart's id |
||
| 352 | * @return array of product id, subTotal, grandTotal, and items & modifiers |
||
| 353 | */ |
||
| 354 | public function getCurrentShoppingCart() |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Provide a copy of the current order's items, including image details and variations |
||
| 377 | * @todo what about subTitles? i.e the variation choosen (I think) |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | protected function getCurrentShoppingCartItems() |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Provide a copy of the current order's modifiers |
||
| 436 | * @todo Only FlatTaxModifier tested |
||
| 437 | * @return array of modifiers (note: this excludes subtotal and grandtotal) |
||
| 438 | */ |
||
| 439 | protected function getCurrentShoppingCartModifiers() |
||
| 469 | } |
||
| 470 | } |
||
| 471 |