Complex classes like OrderStep 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 OrderStep, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class OrderStep extends DataObject implements EditableEcommerceObject |
||
|
|||
13 | { |
||
14 | |||
15 | |||
16 | /** |
||
17 | * standard SS variable. |
||
18 | * |
||
19 | * @return array |
||
20 | */ |
||
21 | private static $db = array( |
||
22 | 'Name' => 'Varchar(50)', |
||
23 | 'Code' => 'Varchar(50)', |
||
24 | 'Description' => 'Text', |
||
25 | 'EmailSubject' => 'Varchar(200)', |
||
26 | 'CustomerMessage' => 'HTMLText', |
||
27 | //customer privileges |
||
28 | 'CustomerCanEdit' => 'Boolean', |
||
29 | 'CustomerCanCancel' => 'Boolean', |
||
30 | 'CustomerCanPay' => 'Boolean', |
||
31 | //What to show the customer... |
||
32 | 'ShowAsUncompletedOrder' => 'Boolean', |
||
33 | 'ShowAsInProcessOrder' => 'Boolean', |
||
34 | 'ShowAsCompletedOrder' => 'Boolean', |
||
35 | 'HideStepFromCustomer' => 'Boolean', |
||
36 | //sorting index |
||
37 | 'Sort' => 'Int', |
||
38 | 'DeferTimeInSeconds' => 'Int', |
||
39 | 'DeferFromSubmitTime' => 'Boolean' |
||
40 | ); |
||
41 | |||
42 | |||
43 | |||
44 | /** |
||
45 | * standard SS variable. |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | private static $indexes = array( |
||
50 | 'Code' => true, |
||
51 | 'Sort' => true, |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * standard SS variable. |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | private static $has_many = array( |
||
60 | 'Orders' => 'Order', |
||
61 | 'OrderEmailRecords' => 'OrderEmailRecord', |
||
62 | ); |
||
63 | |||
64 | /** |
||
65 | * standard SS variable. |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | private static $field_labels = array( |
||
70 | 'Sort' => 'Sorting Index', |
||
71 | 'CustomerCanEdit' => 'Customer can edit order', |
||
72 | 'CustomerCanPay' => 'Customer can pay order', |
||
73 | 'CustomerCanCancel' => 'Customer can cancel order', |
||
74 | ); |
||
75 | |||
76 | /** |
||
77 | * standard SS variable. |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | private static $summary_fields = array( |
||
82 | 'NameAndDescription' => 'Step', |
||
83 | 'ShowAsSummary' => 'Phase', |
||
84 | ); |
||
85 | |||
86 | /** |
||
87 | * standard SS variable. |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | private static $casting = array( |
||
92 | 'Title' => 'Varchar', |
||
93 | 'CustomerCanEditNice' => 'Varchar', |
||
94 | 'CustomerCanPayNice' => 'Varchar', |
||
95 | 'CustomerCanCancelNice' => 'Varchar', |
||
96 | 'ShowAsUncompletedOrderNice' => 'Varchar', |
||
97 | 'ShowAsInProcessOrderNice' => 'Varchar', |
||
98 | 'ShowAsCompletedOrderNice' => 'Varchar', |
||
99 | 'HideStepFromCustomerNice' => 'Varchar', |
||
100 | 'HasCustomerMessageNice' => 'Varchar', |
||
101 | 'ShowAsSummary' => 'HTMLText', |
||
102 | 'NameAndDescription' => 'HTMLText' |
||
103 | ); |
||
104 | |||
105 | /** |
||
106 | * standard SS variable. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | private static $searchable_fields = array( |
||
111 | 'Name' => array( |
||
112 | 'title' => 'Name', |
||
113 | 'filter' => 'PartialMatchFilter', |
||
114 | ), |
||
115 | 'Code' => array( |
||
116 | 'title' => 'Code', |
||
117 | 'filter' => 'PartialMatchFilter', |
||
118 | ), |
||
119 | ); |
||
120 | |||
121 | |||
122 | /** |
||
123 | * casted variable. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function Title() |
||
135 | |||
136 | /** |
||
137 | * casted variable. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function CustomerCanEditNice() |
||
153 | |||
154 | /** |
||
155 | * casted variable. |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | public function CustomerCanPayNice() |
||
171 | |||
172 | /** |
||
173 | * casted variable. |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function CustomerCanCancelNice() |
||
189 | |||
190 | public function ShowAsUncompletedOrderNice() |
||
202 | |||
203 | /** |
||
204 | * casted variable. |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function ShowAsInProcessOrderNice() |
||
220 | |||
221 | /** |
||
222 | * casted variable. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | public function ShowAsCompletedOrderNice() |
||
238 | |||
239 | /** |
||
240 | * do not show in steps at all. |
||
241 | * @return boolean |
||
242 | */ |
||
243 | public function HideFromEveryone() |
||
247 | |||
248 | /** |
||
249 | * casted variable. |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | public function HideStepFromCustomerNice() |
||
257 | |||
258 | public function getHideStepFromCustomerNice() |
||
266 | |||
267 | /** |
||
268 | * standard SS variable. |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | private static $singular_name = 'Order Step'; |
||
273 | public function i18n_singular_name() |
||
277 | |||
278 | /** |
||
279 | * standard SS variable. |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | private static $plural_name = 'Order Steps'; |
||
284 | public function i18n_plural_name() |
||
288 | |||
289 | /** |
||
290 | * Standard SS variable. |
||
291 | * |
||
292 | * @var string |
||
293 | */ |
||
294 | private static $description = 'A step that any order goes through.'; |
||
295 | |||
296 | /** |
||
297 | * SUPER IMPORTANT TO KEEP ORDER! |
||
298 | * standard SS variable. |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | private static $default_sort = '"Sort" ASC'; |
||
303 | |||
304 | /** |
||
305 | * returns all the order steps |
||
306 | * that the admin should / can edit.... |
||
307 | * |
||
308 | * @return DataList |
||
309 | */ |
||
310 | public static function admin_manageable_steps() |
||
315 | |||
316 | /** |
||
317 | * return StatusIDs (orderstep IDs) from orders that are bad.... |
||
318 | * (basically StatusID values that do not exist) |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | public static function bad_order_step_ids() |
||
334 | |||
335 | /** |
||
336 | * turns code into ID. |
||
337 | * |
||
338 | * @param string $code |
||
339 | * @param int |
||
340 | */ |
||
341 | public static function get_status_id_from_code($code) |
||
352 | |||
353 | /** |
||
354 | *@return array |
||
355 | **/ |
||
356 | public static function get_codes_for_order_steps_to_include() |
||
369 | |||
370 | /** |
||
371 | * returns a list of ordersteps that have not been created yet. |
||
372 | * |
||
373 | * @return array |
||
374 | **/ |
||
375 | public static function get_not_created_codes_for_order_steps_to_include() |
||
389 | |||
390 | /** |
||
391 | *@return string |
||
392 | **/ |
||
393 | public function getMyCode() |
||
402 | |||
403 | /** |
||
404 | * IMPORTANT:: MUST HAVE Code must be defined!!! |
||
405 | * standard SS variable. |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | private static $defaults = array( |
||
410 | 'CustomerCanEdit' => 0, |
||
411 | 'CustomerCanCancel' => 0, |
||
412 | 'CustomerCanPay' => 1, |
||
413 | 'ShowAsUncompletedOrder' => 0, |
||
414 | 'ShowAsInProcessOrder' => 0, |
||
415 | 'ShowAsCompletedOrder' => 0, |
||
416 | 'Code' => 'ORDERSTEP', |
||
417 | ); |
||
418 | |||
419 | /** |
||
420 | * standard SS method. |
||
421 | */ |
||
422 | public function populateDefaults() |
||
427 | |||
428 | /** |
||
429 | *@return FieldList |
||
430 | **/ |
||
431 | public function getCMSFields() |
||
515 | |||
516 | /** |
||
517 | * link to edit the record. |
||
518 | * |
||
519 | * @param string | Null $action - e.g. edit |
||
520 | * |
||
521 | * @return string |
||
522 | */ |
||
523 | public function CMSEditLink($action = null) |
||
531 | |||
532 | /** |
||
533 | * tells the order to display itself with an alternative display page. |
||
534 | * in that way, orders can be displayed differently for certain steps |
||
535 | * for example, in a print step, the order can be displayed in a |
||
536 | * PRINT ONLY format. |
||
537 | * |
||
538 | * When the method return null, the order is displayed using the standard display page |
||
539 | * |
||
540 | * @see Order::DisplayPage |
||
541 | * |
||
542 | * @return null|object (Page) |
||
543 | **/ |
||
544 | public function AlternativeDisplayPage() |
||
548 | |||
549 | /** |
||
550 | * Allows the opportunity for the Order Step to add any fields to Order::getCMSFields |
||
551 | * Usually this is added before ActionNextStepManually. |
||
552 | * |
||
553 | * @param FieldList $fields |
||
554 | * @param Order $order |
||
555 | * |
||
556 | * @return FieldList |
||
557 | **/ |
||
558 | public function addOrderStepFields(FieldList $fields, Order $order) |
||
562 | |||
563 | /** |
||
564 | *@return ValidationResult |
||
565 | **/ |
||
566 | public function validate() |
||
584 | |||
585 | /************************************************** |
||
586 | * moving between statusses... |
||
587 | **************************************************/ |
||
588 | /** |
||
589 | *initStep: |
||
590 | * makes sure the step is ready to run.... (e.g. check if the order is ready to be emailed as receipt). |
||
591 | * should be able to run this function many times to check if the step is ready. |
||
592 | * |
||
593 | * @see Order::doNextStatus |
||
594 | * |
||
595 | * @param Order object |
||
596 | * |
||
597 | * @return bool - true if the current step is ready to be run... |
||
598 | **/ |
||
599 | public function initStep(Order $order) |
||
605 | |||
606 | /** |
||
607 | *doStep: |
||
608 | * should only be able to run this function once |
||
609 | * (init stops you from running it twice - in theory....) |
||
610 | * runs the actual step. |
||
611 | * |
||
612 | * @see Order::doNextStatus |
||
613 | * |
||
614 | * @param Order object |
||
615 | * |
||
616 | * @return bool - true if run correctly. |
||
617 | **/ |
||
618 | public function doStep(Order $order) |
||
624 | |||
625 | /** |
||
626 | * nextStep: |
||
627 | * returns the next step (after it checks if everything is in place for the next step to run...). |
||
628 | * |
||
629 | * @see Order::doNextStatus |
||
630 | * |
||
631 | * @param Order $order |
||
632 | * |
||
633 | * @return OrderStep | Null (next step OrderStep object) |
||
634 | **/ |
||
635 | public function nextStep(Order $order) |
||
646 | |||
647 | /************************************************** |
||
648 | * Boolean checks |
||
649 | **************************************************/ |
||
650 | |||
651 | /** |
||
652 | * Checks if a step has passed (been completed) in comparison to the current step. |
||
653 | * |
||
654 | * @param string $code: the name of the step to check |
||
655 | * @param bool $orIsEqualTo if set to true, this method will return TRUE if the step being checked is the current one |
||
656 | * |
||
657 | * @return bool |
||
658 | **/ |
||
659 | public function hasPassed($code, $orIsEqualTo = false) |
||
677 | |||
678 | /** |
||
679 | * @param string $code |
||
680 | * |
||
681 | * @return bool |
||
682 | **/ |
||
683 | public function hasPassedOrIsEqualTo($code) |
||
687 | |||
688 | /** |
||
689 | * @param string $code |
||
690 | * |
||
691 | * @return bool |
||
692 | **/ |
||
693 | public function hasNotPassed($code) |
||
697 | |||
698 | /** |
||
699 | * Opposite of hasPassed. |
||
700 | * |
||
701 | * @param string $code |
||
702 | * |
||
703 | * @return bool |
||
704 | **/ |
||
705 | public function isBefore($code) |
||
709 | |||
710 | /** |
||
711 | *@return bool |
||
712 | **/ |
||
713 | protected function isDefaultStatusOption() |
||
717 | |||
718 | /************************************************** |
||
719 | |||
720 | **************************************************/ |
||
721 | |||
722 | /** |
||
723 | * @var string |
||
724 | */ |
||
725 | protected $emailClassName = ''; |
||
726 | |||
727 | /** |
||
728 | * returns the email class used for emailing the |
||
729 | * customer during a specific step (IF ANY!). |
||
730 | * |
||
731 | * @return string |
||
732 | */ |
||
733 | public function getEmailClassName() |
||
737 | |||
738 | /** |
||
739 | * return true if done already or mailed successfully now. |
||
740 | * |
||
741 | * @param order $order |
||
742 | * @param string $subject |
||
743 | * @param string $message |
||
744 | * @param bool $resend |
||
745 | * @param bool | string $adminOnlyOrToEmail you can set to false = send to customer, true: send to admin, or email = send to email |
||
746 | * @param string $emailClassName |
||
747 | * |
||
748 | * @return boolean; |
||
749 | */ |
||
750 | protected function sendEmailForStep( |
||
796 | |||
797 | /** |
||
798 | * sets the email class used for emailing the |
||
799 | * customer during a specific step (IF ANY!). |
||
800 | * |
||
801 | * @param string |
||
802 | */ |
||
803 | public function setEmailClassName($s) |
||
807 | |||
808 | /** |
||
809 | * returns a link that can be used to test |
||
810 | * the email being sent during this step |
||
811 | * this method returns NULL if no email |
||
812 | * is being sent OR if there is no suitable Order |
||
813 | * to test with... |
||
814 | * |
||
815 | * @return string |
||
816 | */ |
||
817 | protected function testEmailLink() |
||
831 | |||
832 | /** |
||
833 | * Has an email been sent to the customer for this |
||
834 | * order step. |
||
835 | *"-10 days". |
||
836 | * |
||
837 | * @param Order $order |
||
838 | * @param bool $checkDateOfOrder |
||
839 | * |
||
840 | * @return bool |
||
841 | **/ |
||
842 | public function hasBeenSent(Order $order, $checkDateOfOrder = true) |
||
866 | |||
867 | /** |
||
868 | * For some ordersteps this returns true... |
||
869 | * |
||
870 | * @return bool |
||
871 | **/ |
||
872 | protected function hasCustomerMessage() |
||
876 | |||
877 | |||
878 | /** |
||
879 | * Formatted answer for "hasCustomerMessage". |
||
880 | * |
||
881 | * @return string |
||
882 | */ |
||
883 | public function HasCustomerMessageNice() |
||
891 | |||
892 | /** |
||
893 | * Formatted answer for "hasCustomerMessage". |
||
894 | * |
||
895 | * @return string |
||
896 | */ |
||
897 | public function ShowAsSummary() |
||
901 | |||
902 | /** |
||
903 | * |
||
904 | * |
||
905 | * @return string |
||
906 | */ |
||
907 | public function getShowAsSummary() |
||
940 | |||
941 | /** |
||
942 | * @return string |
||
943 | */ |
||
944 | protected function humanReadeableDeferTimeInSeconds() |
||
961 | |||
962 | /** |
||
963 | * Formatted answer for "hasCustomerMessage". |
||
964 | * |
||
965 | * @return string |
||
966 | */ |
||
967 | public function NameAndDescription() |
||
971 | |||
972 | public function getNameAndDescription() |
||
978 | |||
979 | /** |
||
980 | * This allows you to set the time to something other than the standard DeferTimeInSeconds |
||
981 | * value based on the order provided. |
||
982 | * |
||
983 | * @param Order |
||
984 | * |
||
985 | * @return int |
||
986 | */ |
||
987 | public function CalculatedDeferTimeInSeconds($order) |
||
991 | |||
992 | /** |
||
993 | * can this order step be delayed? |
||
994 | * in general, if there is a customer message |
||
995 | * we should be able to delay it |
||
996 | * |
||
997 | * This method can be overridden in any orderstep |
||
998 | * @return bool |
||
999 | **/ |
||
1000 | protected function canBeDefered() |
||
1004 | |||
1005 | |||
1006 | /************************************************** |
||
1007 | * Order Status Logs |
||
1008 | **************************************************/ |
||
1009 | |||
1010 | /** |
||
1011 | * The OrderStatusLog that is relevant to the particular step. |
||
1012 | * |
||
1013 | * @var string |
||
1014 | */ |
||
1015 | protected $relevantLogEntryClassName = ''; |
||
1016 | |||
1017 | /** |
||
1018 | * @return string |
||
1019 | */ |
||
1020 | public function getRelevantLogEntryClassName() |
||
1024 | |||
1025 | /** |
||
1026 | * @param string |
||
1027 | */ |
||
1028 | public function setRelevantLogEntryClassName($s) |
||
1032 | |||
1033 | /** |
||
1034 | * returns the OrderStatusLog that is relevant to this step. |
||
1035 | * |
||
1036 | * @param Order $order |
||
1037 | * |
||
1038 | * @return OrderStatusLog | null |
||
1039 | */ |
||
1040 | public function RelevantLogEntry(Order $order) |
||
1046 | |||
1047 | /** |
||
1048 | * returns the OrderStatusLogs that are relevant to this step. |
||
1049 | * |
||
1050 | * @param Order $order |
||
1051 | * |
||
1052 | * @return DataObjectSet | null |
||
1053 | */ |
||
1054 | public function RelevantLogEntries(Order $order) |
||
1060 | |||
1061 | /************************************************** |
||
1062 | * Silverstripe Standard Data Object Methods |
||
1063 | **************************************************/ |
||
1064 | |||
1065 | /** |
||
1066 | * Standard SS method |
||
1067 | * These are only created programmatically. |
||
1068 | * |
||
1069 | * @param Member $member |
||
1070 | * |
||
1071 | * @return bool |
||
1072 | */ |
||
1073 | public function canCreate($member = null) |
||
1077 | |||
1078 | /** |
||
1079 | * Standard SS method. |
||
1080 | * |
||
1081 | * @param Member $member |
||
1082 | * |
||
1083 | * @return bool |
||
1084 | */ |
||
1085 | public function canView($member = null) |
||
1100 | |||
1101 | /** |
||
1102 | * the default for this is TRUE, but for completed order steps |
||
1103 | * |
||
1104 | * we do not allow this. |
||
1105 | * |
||
1106 | * @param Order $order |
||
1107 | * @param Member $member optional |
||
1108 | * @return bool |
||
1109 | */ |
||
1110 | public function canOverrideCanViewForOrder($order, $member = null) |
||
1119 | |||
1120 | /** |
||
1121 | * standard SS method. |
||
1122 | * |
||
1123 | * @param Member | NULL |
||
1124 | * |
||
1125 | * @return bool |
||
1126 | */ |
||
1127 | public function canEdit($member = null) |
||
1142 | |||
1143 | /** |
||
1144 | * Standard SS method. |
||
1145 | * |
||
1146 | * @param Member $member |
||
1147 | * |
||
1148 | * @return bool |
||
1149 | */ |
||
1150 | public function canDelete($member = null) |
||
1180 | |||
1181 | /** |
||
1182 | * standard SS method. |
||
1183 | */ |
||
1184 | public function onBeforeWrite() |
||
1213 | |||
1214 | /** |
||
1215 | * move linked orders to the next status |
||
1216 | * standard SS method. |
||
1217 | */ |
||
1218 | public function onBeforeDelete() |
||
1239 | |||
1240 | /** |
||
1241 | * standard SS method. |
||
1242 | */ |
||
1243 | public function onAfterDelete() |
||
1248 | |||
1249 | protected function NextOrderStep() |
||
1255 | |||
1256 | protected function PreviousOrderStep() |
||
1262 | |||
1263 | /** |
||
1264 | * standard SS method |
||
1265 | * USED TO BE: Unpaid,Query,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart. |
||
1266 | */ |
||
1267 | public function requireDefaultRecords() |
||
1320 | |||
1321 | /** |
||
1322 | * returns the standard EcommerceDBConfig for use within OrderSteps. |
||
1323 | * |
||
1324 | * @return EcommerceDBConfig |
||
1325 | */ |
||
1326 | protected function EcomConfig() |
||
1330 | |||
1331 | /** |
||
1332 | * Explains the current order step. |
||
1333 | * |
||
1334 | * @return string |
||
1335 | */ |
||
1336 | protected function myDescription() |
||
1340 | } |
||
1341 |
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.