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',// no need to set to => 'ADMIN', |
||
99 | 'debug', // no need to set to => 'ADMIN', |
||
100 | 'ajaxtest', // no need to set to => 'ADMIN', |
||
101 | ); |
||
102 | |||
103 | public function index() |
||
114 | |||
115 | /******************************************************* |
||
116 | * CONTROLLER LINKS |
||
117 | *******************************************************/ |
||
118 | |||
119 | /** |
||
120 | * @param string $action |
||
121 | * |
||
122 | * @return string (Link) |
||
123 | */ |
||
124 | public function Link($action = null) |
||
128 | |||
129 | /** |
||
130 | * returns ABSOLUTE link to the shopping cart controller. |
||
131 | * @param null | array | string $actionAndOtherLinkVariables |
||
132 | * @return string |
||
133 | */ |
||
134 | protected static function create_link($actionAndOtherLinkVariables = null) |
||
142 | |||
143 | /** |
||
144 | * @param int $buyableID |
||
145 | * @param string $classNameForBuyable |
||
146 | * @param array $parameters |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public static function add_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
154 | |||
155 | /** |
||
156 | * @param int $buyableID |
||
157 | * @param string $classNameForBuyable |
||
158 | * @param array $parameters |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public static function remove_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
166 | |||
167 | /** |
||
168 | * @param int $buyableID |
||
169 | * @param string $classNameForBuyable |
||
170 | * @param array $parameters |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | public static function remove_all_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
178 | |||
179 | /** |
||
180 | * @param int $buyableID |
||
181 | * @param string $classNameForBuyable |
||
182 | * @param array $parameters |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public static function remove_all_item_and_edit_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
190 | |||
191 | /** |
||
192 | * @param int $buyableID |
||
193 | * @param string $classNameForBuyable |
||
194 | * @param array $parameters |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public static function set_quantity_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) |
||
202 | |||
203 | /** |
||
204 | * @param int $modifierID |
||
205 | * @param array $parameters |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public static function remove_modifier_link($modifierID, array $parameters = array()) |
||
213 | |||
214 | /** |
||
215 | * @param int $modifierID |
||
216 | * @param array $parameters |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | public static function add_modifier_link($modifierID, array $parameters = array()) |
||
224 | |||
225 | /** |
||
226 | * @param int $addressID |
||
227 | * @param string $addressClassName |
||
228 | * @param array $parameters |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | public static function remove_address_link($addressID, $addressClassName, array $parameters = array()) |
||
236 | |||
237 | /** |
||
238 | * @param array $parameters |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | public static function clear_cart_link($parameters = array()) |
||
246 | |||
247 | /** |
||
248 | * @param array $parameters |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | public static function save_cart_link(array $parameters = array()) |
||
256 | |||
257 | /** |
||
258 | * @param array $parameters |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | public static function clear_cart_and_logout_link(array $parameters = array()) |
||
266 | |||
267 | /** |
||
268 | * @param array $parameters |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | public static function delete_order_link($orderID, array $parameters = array()) |
||
276 | |||
277 | /** |
||
278 | * |
||
279 | * @return null | string |
||
280 | */ |
||
281 | public static function copy_order_link($orderID, $parameters = array()) |
||
288 | |||
289 | /** |
||
290 | * returns a link that allows you to set a currency... |
||
291 | * dont be fooled by the set_ part... |
||
292 | * |
||
293 | * @param string $code |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | public static function set_currency_link($code, array $parameters = array()) |
||
301 | |||
302 | /** |
||
303 | * |
||
304 | * |
||
305 | * @param int $id |
||
306 | * @param string $className |
||
307 | * @return string |
||
308 | */ |
||
309 | public static function remove_from_sale_link($id, $className) |
||
313 | |||
314 | /** |
||
315 | * return json for cart... no further actions. |
||
316 | * |
||
317 | * @param SS_HTTPRequest |
||
318 | * |
||
319 | * @return JSON |
||
320 | */ |
||
321 | public function json(SS_HTTPRequest $request) |
||
325 | |||
326 | /** |
||
327 | * Adds item to cart via controller action; one by default. |
||
328 | * |
||
329 | * @param HTTPRequest |
||
330 | * |
||
331 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
332 | * If it is not AJAX it redirects back to requesting page. |
||
333 | */ |
||
334 | public function additem(SS_HTTPRequest $request) |
||
344 | |||
345 | /** |
||
346 | * Sets the exact passed quantity. |
||
347 | * Note: If no ?quantity=x is specified in URL, then quantity will be set to 1. |
||
348 | * |
||
349 | * @param HTTPRequest |
||
350 | * |
||
351 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
352 | * If it is not AJAX it redirects back to requesting page. |
||
353 | */ |
||
354 | public function setquantityitem(SS_HTTPRequest $request) |
||
365 | |||
366 | /** |
||
367 | * Removes item from cart via controller action; one by default. |
||
368 | * |
||
369 | * @param HTTPRequest |
||
370 | * |
||
371 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
372 | * If it is not AJAX it redirects back to requesting page. |
||
373 | */ |
||
374 | public function removeitem(SS_HTTPRequest $request) |
||
385 | |||
386 | /** |
||
387 | * Removes all of a specific item. |
||
388 | * |
||
389 | * @param HTTPRequest |
||
390 | * |
||
391 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
392 | * If it is not AJAX it redirects back to requesting page. |
||
393 | */ |
||
394 | public function removeallitem(SS_HTTPRequest $request) |
||
408 | |||
409 | /** |
||
410 | * Removes all of a specific item AND return back. |
||
411 | * |
||
412 | * @param HTTPRequest |
||
413 | * |
||
414 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
415 | * If it is not AJAX it redirects back to requesting page. |
||
416 | */ |
||
417 | public function removeallitemandedit(SS_HTTPRequest $request) |
||
428 | |||
429 | /** |
||
430 | * Removes a specified modifier from the cart;. |
||
431 | * |
||
432 | * @param HTTPRequest |
||
433 | * |
||
434 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
435 | * If it is not AJAX it redirects back to requesting page. |
||
436 | */ |
||
437 | public function removemodifier(SS_HTTPRequest $request) |
||
446 | |||
447 | /** |
||
448 | * Adds a specified modifier to the cart;. |
||
449 | * |
||
450 | * @param HTTPRequest |
||
451 | * |
||
452 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
453 | * If it is not AJAX it redirects back to requesting page. |
||
454 | */ |
||
455 | public function addmodifier(SS_HTTPRequest $request) |
||
464 | |||
465 | /** |
||
466 | * sets the country. |
||
467 | * |
||
468 | * @param SS_HTTPRequest |
||
469 | * |
||
470 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
471 | * If it is not AJAX it redirects back to requesting page. |
||
472 | **/ |
||
473 | public function setcountry(SS_HTTPRequest $request) |
||
481 | |||
482 | /** |
||
483 | * @param SS_HTTPRequest |
||
484 | * |
||
485 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
486 | * If it is not AJAX it redirects back to requesting page. |
||
487 | **/ |
||
488 | public function setregion(SS_HTTPRequest $request) |
||
495 | |||
496 | /** |
||
497 | * @param SS_HTTPRequest |
||
498 | * |
||
499 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
500 | * If it is not AJAX it redirects back to requesting page. |
||
501 | **/ |
||
502 | public function setcurrency(SS_HTTPRequest $request) |
||
509 | |||
510 | /** |
||
511 | * @param SS_HTTPRequest |
||
512 | * |
||
513 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
514 | * If it is not AJAX it redirects back to requesting page. |
||
515 | **/ |
||
516 | public function removefromsale(SS_HTTPRequest $request) |
||
537 | |||
538 | /** |
||
539 | * @param SS_HTTPRequest |
||
540 | * |
||
541 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); |
||
542 | * If it is not AJAX it redirects back to requesting page. |
||
543 | **/ |
||
544 | public function save(SS_HTTPRequest $request) |
||
550 | |||
551 | /** |
||
552 | * @param SS_HTTPRequest |
||
553 | * |
||
554 | * @return REDIRECT |
||
555 | **/ |
||
556 | public function clear(SS_HTTPRequest $request) |
||
563 | |||
564 | /** |
||
565 | * @param SS_HTTPRequest |
||
566 | * |
||
567 | * @return REDIRECT |
||
568 | **/ |
||
569 | public function clearandlogout(SS_HTTPRequest $request) |
||
579 | |||
580 | /** |
||
581 | * @param SS_HTTPRequest |
||
582 | * |
||
583 | * @return REDIRECT |
||
584 | **/ |
||
585 | public function deleteorder(SS_HTTPRequest $request) |
||
595 | |||
596 | public function copyorder($request) |
||
605 | |||
606 | /** |
||
607 | * return number of items in cart. |
||
608 | * |
||
609 | * @param SS_HTTPRequest |
||
610 | * |
||
611 | * @return int |
||
612 | **/ |
||
613 | public function numberofitemsincart(SS_HTTPRequest $request) |
||
619 | |||
620 | /** |
||
621 | * return cart for ajax call. |
||
622 | * |
||
623 | * @param SS_HTTPRequest |
||
624 | * |
||
625 | * @return HTML |
||
626 | */ |
||
627 | public function showcart(SS_HTTPRequest $request) |
||
631 | |||
632 | /** |
||
633 | * loads an order. |
||
634 | * |
||
635 | * @param SS_HTTPRequest |
||
636 | * |
||
637 | * @return REDIRECT |
||
638 | */ |
||
639 | public function loadorder(SS_HTTPRequest $request) |
||
649 | |||
650 | /** |
||
651 | * remove address from list of available addresses in checkout. |
||
652 | * |
||
653 | * @param SS_HTTPRequest |
||
654 | * |
||
655 | * @return string | REDIRECT |
||
656 | * @TODO: add non-ajax version of this request. |
||
657 | */ |
||
658 | public function removeaddress(SS_HTTPRequest $request) |
||
684 | |||
685 | /** |
||
686 | * allows us to view out-dated buyables that have been deleted |
||
687 | * where only old versions exist. |
||
688 | * this method should redirect. |
||
689 | * |
||
690 | * @param SS_HTTPRequest |
||
691 | * |
||
692 | * @return REDIRECT |
||
693 | */ |
||
694 | public function submittedbuyable(SS_HTTPRequest $request) |
||
727 | |||
728 | /** |
||
729 | * This can be used by admins to log in as customers |
||
730 | * to place orders on their behalf... |
||
731 | * |
||
732 | * @param SS_HTTPRequest |
||
733 | * |
||
734 | * @return REDIRECT |
||
735 | */ |
||
736 | public function placeorderformember(SS_HTTPRequest $request) |
||
757 | |||
758 | /** |
||
759 | * This can be used by admins to log in as customers |
||
760 | * to place orders on their behalf... |
||
761 | * |
||
762 | * @param SS_HTTPRequest |
||
763 | * |
||
764 | * @return REDIRECT |
||
765 | */ |
||
766 | public function loginas(SS_HTTPRequest $request) |
||
785 | |||
786 | /** |
||
787 | * Helper function used by link functions |
||
788 | * Creates the appropriate url-encoded string parameters for links from array. |
||
789 | * |
||
790 | * Produces string such as: MyParam%3D11%26OtherParam%3D1 |
||
791 | * ...which decodes to: MyParam=11&OtherParam=1 |
||
792 | * |
||
793 | * you will need to decode the url with javascript before using it. |
||
794 | * |
||
795 | * @todo: check that comment description actually matches what it does |
||
796 | * |
||
797 | * @return string (URLSegment) |
||
798 | */ |
||
799 | protected static function params_to_get_string(array $array) |
||
808 | |||
809 | /** |
||
810 | * Gets a buyable object based on URL actions. |
||
811 | * |
||
812 | * @return DataObject | Null - returns buyable |
||
813 | */ |
||
814 | protected function buyable() |
||
835 | |||
836 | /** |
||
837 | * Gets the requested quantity. |
||
838 | * |
||
839 | * @return float |
||
840 | */ |
||
841 | protected function quantity() |
||
850 | |||
851 | /** |
||
852 | * Gets the request parameters. |
||
853 | * |
||
854 | * @param $getpost - choose between obtaining the chosen parameters from GET or POST |
||
855 | * |
||
856 | * @return array |
||
857 | */ |
||
858 | protected function parameters($getpost = 'GET') |
||
862 | |||
863 | protected function goToErrorPage() |
||
874 | |||
875 | /** |
||
876 | * Handy debugging action visit. |
||
877 | * Log in as an administrator and visit mysite/shoppingcart/debug. |
||
878 | */ |
||
879 | public function debug() |
||
888 | |||
889 | /** |
||
890 | * test the ajax response |
||
891 | * for developers only. |
||
892 | * |
||
893 | * @return output to buffer |
||
894 | */ |
||
895 | public function ajaxtest(SS_HTTPRequest $request) |
||
915 | } |
||
916 |