Complex classes like ShoppingCart_Controller 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ShoppingCart_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ShoppingCart_Controller extends Controller |
||
|
|
|||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Default URL handlers - (Action)/(ID)/(OtherID). |
||
| 21 | */ |
||
| 22 | private static $url_handlers = array( |
||
| 23 | '$Action//$ID/$OtherID/$Version' => 'handleAction', |
||
| 24 | ); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * We need to only use the Security ID on a few |
||
| 28 | * actions, these are listed here. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $methodsRequiringSecurityID = array( |
||
| 33 | 'additem', |
||
| 34 | 'removeitem', |
||
| 35 | 'removeallitem', |
||
| 36 | 'removeallitemandedit', |
||
| 37 | 'removemodifier', |
||
| 38 | 'addmodifier', |
||
| 39 | 'copyorder', |
||
| 40 | 'deleteorder', |
||
| 41 | 'save', |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var ShoppingCart |
||
| 46 | */ |
||
| 47 | protected $cart = null; |
||
| 48 | |||
| 49 | public function init() |
||
| 72 | |||
| 73 | private static $allowed_actions = array( |
||
| 74 | 'json', |
||
| 75 | 'index', |
||
| 76 | 'additem', |
||
| 77 | 'removeitem', |
||
| 78 | 'removeallitem', |
||
| 79 | 'removeallitemandedit', |
||
| 80 | 'removemodifier', |
||
| 81 | 'addmodifier', |
||
| 82 | 'setcountry', |
||
| 83 | 'setregion', |
||
| 84 | 'setcurrency', |
||
| 85 | 'removefromsale', |
||
| 86 | 'setquantityitem', |
||
| 87 | 'clear', |
||
| 88 | 'clearandlogout', |
||
| 89 | 'save', |
||
| 90 | 'deleteorder', |
||
| 91 | 'numberofitemsincart', |
||
| 92 | 'showcart', |
||
| 93 | 'loadorder', |
||
| 94 | 'copyorder', |
||
| 95 | 'removeaddress', |
||
| 96 | 'submittedbuyable', |
||
| 97 | 'placeorderformember', |
||
| 98 | 'loginas', |
||
| 99 | 'debug', // no need to set to => 'ADMIN', |
||
| 100 | 'ajaxtest', // no need to set to => 'ADMIN', |
||
| 101 | ); |
||
| 102 | |||
| 103 | public function index() |
||
| 121 | |||
| 122 | /******************************************************* |
||
| 123 | * CONTROLLER LINKS |
||
| 124 | *******************************************************/ |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $action |
||
| 128 | * |
||
| 129 | * @return string (Link) |
||
| 130 | */ |
||
| 131 | public function Link($action = null) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * returns ABSOLUTE link to the shopping cart controller. |
||
| 138 | * @param null | array | string $actionAndOtherLinkVariables |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | protected static function create_link($actionAndOtherLinkVariables = null) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param int $buyableID |
||
| 152 | * @param string $classNameForBuyable |
||
| 153 | * @param array $parameters |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public static function add_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param int $buyableID |
||
| 164 | * @param string $classNameForBuyable |
||
| 165 | * @param array $parameters |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public static function remove_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param int $buyableID |
||
| 176 | * @param string $classNameForBuyable |
||
| 177 | * @param array $parameters |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public static function remove_all_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param int $buyableID |
||
| 188 | * @param string $classNameForBuyable |
||
| 189 | * @param array $parameters |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public static function remove_all_item_and_edit_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param int $buyableID |
||
| 200 | * @param string $classNameForBuyable |
||
| 201 | * @param array $parameters |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public static function set_quantity_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param int $modifierID |
||
| 212 | * @param array $parameters |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public static function remove_modifier_link($modifierID, array $parameters = array()) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param int $modifierID |
||
| 223 | * @param array $parameters |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public static function add_modifier_link($modifierID, array $parameters = array()) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param int $addressID |
||
| 234 | * @param string $addressClassName |
||
| 235 | * @param array $parameters |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public static function remove_address_link($addressID, $addressClassName, array $parameters = array()) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param array $parameters |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public static function clear_cart_link($parameters = array()) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param array $parameters |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public static function save_cart_link(array $parameters = array()) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param array $parameters |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public static function clear_cart_and_logout_link(array $parameters = array()) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param array $parameters |
||
| 276 | * |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public static function delete_order_link($orderID, array $parameters = array()) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * |
||
| 286 | * @return null | string |
||
| 287 | */ |
||
| 288 | public static function copy_order_link($orderID, $parameters = array()) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * returns a link that allows you to set a currency... |
||
| 298 | * dont be fooled by the set_ part... |
||
| 299 | * |
||
| 300 | * @param string $code |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public static function set_currency_link($code, array $parameters = array()) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * |
||
| 311 | * |
||
| 312 | * @param int $id |
||
| 313 | * @param string $className |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public static function remove_from_sale_link($id, $className) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * return json for cart... no further actions. |
||
| 323 | * |
||
| 324 | * @param SS_HTTPRequest |
||
| 325 | * |
||
| 326 | * @return JSON |
||
| 327 | */ |
||
| 328 | public function json(SS_HTTPRequest $request) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Adds item to cart via controller action; one by default. |
||
| 335 | * |
||
| 336 | * @param HTTPRequest |
||
| 337 | * |
||
| 338 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 339 | * If it is not AJAX it redirects back to requesting page. |
||
| 340 | */ |
||
| 341 | public function additem(SS_HTTPRequest $request) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Sets the exact passed quantity. |
||
| 350 | * Note: If no ?quantity=x is specified in URL, then quantity will be set to 1. |
||
| 351 | * |
||
| 352 | * @param HTTPRequest |
||
| 353 | * |
||
| 354 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 355 | * If it is not AJAX it redirects back to requesting page. |
||
| 356 | */ |
||
| 357 | public function setquantityitem(SS_HTTPRequest $request) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Removes item from cart via controller action; one by default. |
||
| 366 | * |
||
| 367 | * @param HTTPRequest |
||
| 368 | * |
||
| 369 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 370 | * If it is not AJAX it redirects back to requesting page. |
||
| 371 | */ |
||
| 372 | public function removeitem(SS_HTTPRequest $request) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Removes all of a specific item. |
||
| 381 | * |
||
| 382 | * @param HTTPRequest |
||
| 383 | * |
||
| 384 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 385 | * If it is not AJAX it redirects back to requesting page. |
||
| 386 | */ |
||
| 387 | public function removeallitem(SS_HTTPRequest $request) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Removes all of a specific item AND return back. |
||
| 396 | * |
||
| 397 | * @param HTTPRequest |
||
| 398 | * |
||
| 399 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 400 | * If it is not AJAX it redirects back to requesting page. |
||
| 401 | */ |
||
| 402 | public function removeallitemandedit(SS_HTTPRequest $request) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Removes a specified modifier from the cart;. |
||
| 416 | * |
||
| 417 | * @param HTTPRequest |
||
| 418 | * |
||
| 419 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 420 | * If it is not AJAX it redirects back to requesting page. |
||
| 421 | */ |
||
| 422 | public function removemodifier(SS_HTTPRequest $request) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Adds a specified modifier to the cart;. |
||
| 432 | * |
||
| 433 | * @param HTTPRequest |
||
| 434 | * |
||
| 435 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 436 | * If it is not AJAX it redirects back to requesting page. |
||
| 437 | */ |
||
| 438 | public function addmodifier(SS_HTTPRequest $request) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * sets the country. |
||
| 448 | * |
||
| 449 | * @param SS_HTTPRequest |
||
| 450 | * |
||
| 451 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 452 | * If it is not AJAX it redirects back to requesting page. |
||
| 453 | **/ |
||
| 454 | public function setcountry(SS_HTTPRequest $request) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param SS_HTTPRequest |
||
| 465 | * |
||
| 466 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 467 | * If it is not AJAX it redirects back to requesting page. |
||
| 468 | **/ |
||
| 469 | public function setregion(SS_HTTPRequest $request) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param SS_HTTPRequest |
||
| 479 | * |
||
| 480 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 481 | * If it is not AJAX it redirects back to requesting page. |
||
| 482 | **/ |
||
| 483 | public function setcurrency(SS_HTTPRequest $request) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param SS_HTTPRequest |
||
| 493 | * |
||
| 494 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 495 | * If it is not AJAX it redirects back to requesting page. |
||
| 496 | **/ |
||
| 497 | public function removefromsale(SS_HTTPRequest $request) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param SS_HTTPRequest |
||
| 517 | * |
||
| 518 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
| 519 | * If it is not AJAX it redirects back to requesting page. |
||
| 520 | **/ |
||
| 521 | public function save(SS_HTTPRequest $request) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param SS_HTTPRequest |
||
| 530 | * |
||
| 531 | * @return REDIRECT |
||
| 532 | **/ |
||
| 533 | public function clear(SS_HTTPRequest $request) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param SS_HTTPRequest |
||
| 543 | * |
||
| 544 | * @return REDIRECT |
||
| 545 | **/ |
||
| 546 | public function clearandlogout(SS_HTTPRequest $request) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param SS_HTTPRequest |
||
| 559 | * |
||
| 560 | * @return REDIRECT |
||
| 561 | **/ |
||
| 562 | public function deleteorder(SS_HTTPRequest $request) |
||
| 572 | |||
| 573 | public function copyorder($request) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * return number of items in cart. |
||
| 584 | * |
||
| 585 | * @param SS_HTTPRequest |
||
| 586 | * |
||
| 587 | * @return int |
||
| 588 | **/ |
||
| 589 | public function numberofitemsincart(SS_HTTPRequest $request) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * return cart for ajax call. |
||
| 598 | * |
||
| 599 | * @param SS_HTTPRequest |
||
| 600 | * |
||
| 601 | * @return HTML |
||
| 602 | */ |
||
| 603 | public function showcart(SS_HTTPRequest $request) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * loads an order. |
||
| 610 | * |
||
| 611 | * @param SS_HTTPRequest |
||
| 612 | * |
||
| 613 | * @return REDIRECT |
||
| 614 | */ |
||
| 615 | public function loadorder(SS_HTTPRequest $request) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * remove address from list of available addresses in checkout. |
||
| 628 | * |
||
| 629 | * @param SS_HTTPRequest |
||
| 630 | * |
||
| 631 | * @return string | REDIRECT |
||
| 632 | * @TODO: add non-ajax version of this request. |
||
| 633 | */ |
||
| 634 | public function removeaddress(SS_HTTPRequest $request) |
||
| 660 | |||
| 661 | /** |
||
| 662 | * allows us to view out-dated buyables that have been deleted |
||
| 663 | * where only old versions exist. |
||
| 664 | * this method should redirect. |
||
| 665 | * |
||
| 666 | * @param SS_HTTPRequest |
||
| 667 | * |
||
| 668 | * @return REDIRECT |
||
| 669 | */ |
||
| 670 | public function submittedbuyable(SS_HTTPRequest $request) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * This can be used by admins to log in as customers |
||
| 705 | * to place orders on their behalf... |
||
| 706 | * |
||
| 707 | * @param SS_HTTPRequest |
||
| 708 | * |
||
| 709 | * @return REDIRECT |
||
| 710 | */ |
||
| 711 | public function placeorderformember(SS_HTTPRequest $request) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * This can be used by admins to log in as customers |
||
| 736 | * to place orders on their behalf... |
||
| 737 | * |
||
| 738 | * @param SS_HTTPRequest |
||
| 739 | * |
||
| 740 | * @return REDIRECT |
||
| 741 | */ |
||
| 742 | public function loginas(SS_HTTPRequest $request) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Helper function used by link functions |
||
| 769 | * Creates the appropriate url-encoded string parameters for links from array. |
||
| 770 | * |
||
| 771 | * Produces string such as: MyParam%3D11%26OtherParam%3D1 |
||
| 772 | * ...which decodes to: MyParam=11&OtherParam=1 |
||
| 773 | * |
||
| 774 | * you will need to decode the url with javascript before using it. |
||
| 775 | * |
||
| 776 | * @todo: check that comment description actually matches what it does |
||
| 777 | * |
||
| 778 | * @return string (URLSegment) |
||
| 779 | */ |
||
| 780 | protected static function params_to_get_string(array $array) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Gets a buyable object based on URL actions. |
||
| 792 | * |
||
| 793 | * @return DataObject | Null - returns buyable |
||
| 794 | */ |
||
| 795 | protected function buyable() |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Gets the requested quantity. |
||
| 819 | * |
||
| 820 | * @return float |
||
| 821 | */ |
||
| 822 | protected function quantity() |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Gets the request parameters. |
||
| 834 | * |
||
| 835 | * @param $getpost - choose between obtaining the chosen parameters from GET or POST |
||
| 836 | * |
||
| 837 | * @return array |
||
| 838 | */ |
||
| 839 | protected function parameters($getpost = 'GET') |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Handy debugging action visit. |
||
| 846 | * Log in as an administrator and visit mysite/shoppingcart/debug. |
||
| 847 | */ |
||
| 848 | public function debug() |
||
| 857 | |||
| 858 | /** |
||
| 859 | * test the ajax response |
||
| 860 | * for developers only. |
||
| 861 | * |
||
| 862 | * @return output to buffer |
||
| 863 | */ |
||
| 864 | public function ajaxtest(SS_HTTPRequest $request) |
||
| 884 | } |
||
| 885 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.