| Total Complexity | 41 |
| Total Lines | 368 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| 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 |
||
| 22 | class SilvershopJsonResponse extends Extension |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Allow get action to obtain a copy of the shopping cart |
||
| 26 | */ |
||
| 27 | private static $allowed_actions = array( |
||
|
|
|||
| 28 | 'get' |
||
| 29 | ); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * get the shopping cart |
||
| 33 | * |
||
| 34 | * @param HTTPRequest $request |
||
| 35 | * @return HTTPResponse $response with JSON body |
||
| 36 | */ |
||
| 37 | public function get(HTTPRequest $request) |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Add one of an item to a cart (Category Page) |
||
| 54 | * |
||
| 55 | * @see 'add' function of ShoppingCart_Controller ($this->owner) |
||
| 56 | * @param HTTPRequest $request |
||
| 57 | * @param HTTPResponse $response |
||
| 58 | * @param Buyable $product [optional] |
||
| 59 | * @param int $quantity [optional] |
||
| 60 | */ |
||
| 61 | public function updateAddResponse(&$request, &$response, $product = null, $quantity = 1) |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Remove one of an item from a cart (Cart Page) |
||
| 86 | * |
||
| 87 | * @see 'remove' function of ShoppingCart_Controller ($this->owner) |
||
| 88 | * @param HTTPRequest $request |
||
| 89 | * @param HTTPResponse $response |
||
| 90 | * @param Buyable $product [optional] |
||
| 91 | * @param int $quantity [optional] |
||
| 92 | */ |
||
| 93 | public function updateRemoveResponse(&$request, &$response, $product = null, $quantity = 1) |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Remove all of an item from a cart (Cart Page) |
||
| 118 | * Quantity is NIL |
||
| 119 | * |
||
| 120 | * @see 'removeall' function of ShoppingCart_Controller ($this->owner) |
||
| 121 | * @param HTTPRequest $request |
||
| 122 | * @param HTTPResponse $response |
||
| 123 | * @param Buyable $product [optional] |
||
| 124 | */ |
||
| 125 | public function updateRemoveAllResponse(&$request, &$response, $product = null) |
||
| 126 | { |
||
| 127 | if ($request->isAjax()) { |
||
| 128 | if (!$response) { |
||
| 129 | $response = $this->owner->getResponse(); |
||
| 130 | } |
||
| 131 | $response->removeHeader('Content-Type'); |
||
| 132 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 133 | $shoppingcart = ShoppingCart::curr(); |
||
| 134 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 135 | |||
| 136 | $data = $this->getCurrentShoppingCart(); |
||
| 137 | $data['message'] = [ |
||
| 138 | 'content' => $this->owner->cart->getMessage(), |
||
| 139 | 'type' => $this->owner->cart->getMessageType() |
||
| 140 | ]; |
||
| 141 | $this->owner->cart->clearMessage(); |
||
| 142 | |||
| 143 | $this->owner->extend('updateRemoveAllResponseShopJsonResponse', $data, $request, $response, $product); |
||
| 144 | $response->setBody(json_encode($data)); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Update the quantity of an item in a cart (Cart Page) |
||
| 150 | * |
||
| 151 | * @see 'setquantity' function of ShoppingCart_Controller ($this->owner) |
||
| 152 | * @param HTTPRequest $request |
||
| 153 | * @param HTTPResponse $response |
||
| 154 | * @param Buyable $product [optional] |
||
| 155 | * @param int $quantity [optional] |
||
| 156 | */ |
||
| 157 | public function updateSetQuantityResponse(&$request, &$response, $product = null, $quantity = 1) |
||
| 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 = $this->getCurrentShoppingCart(); |
||
| 169 | $data['message'] = [ |
||
| 170 | 'content' => $this->owner->cart->getMessage(), |
||
| 171 | 'type' => $this->owner->cart->getMessageType() |
||
| 172 | ]; |
||
| 173 | $this->owner->cart->clearMessage(); |
||
| 174 | |||
| 175 | $this->owner->extend('updateSetQuantityResponseShopJsonResponse', $data, $request, $response, $product, $quantity); |
||
| 176 | $response->setBody(json_encode($data)); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Clear all items from the cart (Cart Page) |
||
| 182 | * |
||
| 183 | * @see 'clear' function of ShoppingCart_Controller ($this->owner) |
||
| 184 | * @param HTTPRequest $request |
||
| 185 | * @param HTTPResponse $response |
||
| 186 | */ |
||
| 187 | public function updateClearResponse(&$request, &$response) |
||
| 188 | { |
||
| 189 | if ($request->isAjax()) { |
||
| 190 | if (!$response) { |
||
| 191 | $response = $this->owner->getResponse(); |
||
| 192 | } |
||
| 193 | $response->removeHeader('Content-Type'); |
||
| 194 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 195 | |||
| 196 | $data = $this->getCurrentShoppingCart(); |
||
| 197 | $data['message'] = [ |
||
| 198 | 'content' => $this->owner->cart->getMessage(), |
||
| 199 | 'type' => $this->owner->cart->getMessageType() |
||
| 200 | ]; |
||
| 201 | $this->owner->cart->clearMessage(); |
||
| 202 | |||
| 203 | $this->owner->extend('updateClearResponseShopJsonResponse', $data, $request, $response); |
||
| 204 | $response->setBody(json_encode($data)); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Update the variations of a product (Cart Page) |
||
| 210 | * |
||
| 211 | * @see 'addtocart' function of VariationForm ($this->owner) |
||
| 212 | * @param HTTPRequest $request |
||
| 213 | * @param HTTPResponse $response |
||
| 214 | * @param Buyable $variation [optional] |
||
| 215 | * @param int $quantity [optional] |
||
| 216 | * @param VariationForm $form [optional] |
||
| 217 | */ |
||
| 218 | public function updateVariationFormResponse(&$request, &$response, $variation = null, $quantity = 1, $form = null) |
||
| 219 | { |
||
| 220 | if ($request->isAjax()) { |
||
| 221 | if (!$response) { |
||
| 222 | $response = $this->owner->getResponse(); |
||
| 223 | } |
||
| 224 | $response->removeHeader('Content-Type'); |
||
| 225 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 226 | $shoppingcart = ShoppingCart::curr(); |
||
| 227 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 228 | |||
| 229 | $data = $this->getCurrentShoppingCart(); |
||
| 230 | if ($form) { |
||
| 231 | $data['message'] = [ |
||
| 232 | 'content' => $form->getMessage(), |
||
| 233 | 'type' => $form->getMessageType() |
||
| 234 | ]; |
||
| 235 | $form->clearMessage(); |
||
| 236 | } |
||
| 237 | |||
| 238 | |||
| 239 | $this->owner->extend('updateVariationFormResponseShopJsonResponse', $data, $request, $response, $variation, $quantity, $form); |
||
| 240 | $response->setBody(json_encode($data)); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Add one of an item to a cart (Product Page) |
||
| 246 | * |
||
| 247 | * @see the addtocart function within AddProductForm class |
||
| 248 | * @param HTTPRequest $request |
||
| 249 | * @param HTTPResponse $response |
||
| 250 | * @param Buyable $buyable [optional] |
||
| 251 | * @param int $quantity [optional] |
||
| 252 | * @param AddProductForm $form [optional] |
||
| 253 | */ |
||
| 254 | public function updateAddProductFormResponse(&$request, &$response, $buyable, $quantity, $form) |
||
| 255 | { |
||
| 256 | if ($request->isAjax()) { |
||
| 257 | if (!$response) { |
||
| 258 | $response = $this->owner->getController()->getResponse(); |
||
| 259 | } |
||
| 260 | $response->removeHeader('Content-Type'); |
||
| 261 | $response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
||
| 262 | $shoppingcart = ShoppingCart::curr(); |
||
| 263 | $shoppingcart->calculate(); // recalculate the shopping cart |
||
| 264 | |||
| 265 | $data = $this->getCurrentShoppingCart(); |
||
| 266 | if ($form) { |
||
| 267 | $data['message'] = [ |
||
| 268 | 'content' => $form->getMessage(), |
||
| 269 | 'type' => $form->getMessageType() |
||
| 270 | ]; |
||
| 271 | $form->clearMessage(); |
||
| 272 | } |
||
| 273 | |||
| 274 | $this->owner->extend('updateAddProductFormResponseShopJsonResponse', $data, $request, $response, $buyable, $quantity, $form); |
||
| 275 | $response->setBody(json_encode($data)); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Provide a copy of the current order in the required format |
||
| 281 | * Note the id is the cart's id |
||
| 282 | * @return array of product id, subTotal, grandTotal, and items & modifiers |
||
| 283 | */ |
||
| 284 | public function getCurrentShoppingCart() |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Provide a copy of the current order's items, including image details and variations |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | protected function getCurrentShoppingCartItems() |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Provide a copy of the current order's modifiers |
||
| 358 | * @return array of modifiers (note: this excludes subtotal and grandtotal) |
||
| 359 | */ |
||
| 360 | protected function getCurrentShoppingCartModifiers() |
||
| 390 | } |
||
| 391 | } |
||
| 392 |