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 | 'OrderProcessQueueEntries' => 'OrderProcessQueue' |
||
63 | ); |
||
64 | |||
65 | /** |
||
66 | * standard SS variable. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | private static $field_labels = array( |
||
71 | 'Sort' => 'Sorting Index', |
||
72 | 'CustomerCanEdit' => 'Customer can edit order', |
||
73 | 'CustomerCanPay' => 'Customer can pay order', |
||
74 | 'CustomerCanCancel' => 'Customer can cancel order', |
||
75 | ); |
||
76 | |||
77 | /** |
||
78 | * standard SS variable. |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | private static $summary_fields = array( |
||
83 | 'NameAndDescription' => 'Step', |
||
84 | 'ShowAsSummary' => 'Phase', |
||
85 | ); |
||
86 | |||
87 | /** |
||
88 | * standard SS variable. |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | private static $casting = array( |
||
93 | 'Title' => 'Varchar', |
||
94 | 'CustomerCanEditNice' => 'Varchar', |
||
95 | 'CustomerCanPayNice' => 'Varchar', |
||
96 | 'CustomerCanCancelNice' => 'Varchar', |
||
97 | 'ShowAsUncompletedOrderNice' => 'Varchar', |
||
98 | 'ShowAsInProcessOrderNice' => 'Varchar', |
||
99 | 'ShowAsCompletedOrderNice' => 'Varchar', |
||
100 | 'HideStepFromCustomerNice' => 'Varchar', |
||
101 | 'HasCustomerMessageNice' => 'Varchar', |
||
102 | 'ShowAsSummary' => 'HTMLText', |
||
103 | 'NameAndDescription' => 'HTMLText' |
||
104 | ); |
||
105 | |||
106 | /** |
||
107 | * standard SS variable. |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | private static $searchable_fields = array( |
||
112 | 'Name' => array( |
||
113 | 'title' => 'Name', |
||
114 | 'filter' => 'PartialMatchFilter', |
||
115 | ), |
||
116 | 'Code' => array( |
||
117 | 'title' => 'Code', |
||
118 | 'filter' => 'PartialMatchFilter', |
||
119 | ), |
||
120 | ); |
||
121 | |||
122 | |||
123 | /** |
||
124 | * casted variable. |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public function Title() |
||
136 | |||
137 | /** |
||
138 | * casted variable. |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | public function CustomerCanEditNice() |
||
154 | |||
155 | /** |
||
156 | * casted variable. |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | public function CustomerCanPayNice() |
||
172 | |||
173 | /** |
||
174 | * casted variable. |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | public function CustomerCanCancelNice() |
||
190 | |||
191 | public function ShowAsUncompletedOrderNice() |
||
203 | |||
204 | /** |
||
205 | * casted variable. |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function ShowAsInProcessOrderNice() |
||
221 | |||
222 | /** |
||
223 | * casted variable. |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | public function ShowAsCompletedOrderNice() |
||
239 | |||
240 | /** |
||
241 | * do not show in steps at all. |
||
242 | * @return boolean |
||
243 | */ |
||
244 | public function HideFromEveryone() |
||
248 | |||
249 | /** |
||
250 | * casted variable. |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | public function HideStepFromCustomerNice() |
||
258 | |||
259 | public function getHideStepFromCustomerNice() |
||
267 | |||
268 | /** |
||
269 | * standard SS variable. |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | private static $singular_name = 'Order Step'; |
||
274 | public function i18n_singular_name() |
||
278 | |||
279 | /** |
||
280 | * standard SS variable. |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | private static $plural_name = 'Order Steps'; |
||
285 | public function i18n_plural_name() |
||
289 | |||
290 | /** |
||
291 | * Standard SS variable. |
||
292 | * |
||
293 | * @var string |
||
294 | */ |
||
295 | private static $description = 'A step that any order goes through.'; |
||
296 | |||
297 | /** |
||
298 | * SUPER IMPORTANT TO KEEP ORDER! |
||
299 | * standard SS variable. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | private static $default_sort = '"Sort" ASC'; |
||
304 | |||
305 | /** |
||
306 | * returns all the order steps |
||
307 | * that the admin should / can edit.... |
||
308 | * |
||
309 | * @return DataList |
||
310 | */ |
||
311 | public static function admin_manageable_steps() |
||
317 | /** |
||
318 | * returns all the order steps |
||
319 | * that the admin should / can edit.... |
||
320 | * |
||
321 | * @return DataList |
||
322 | */ |
||
323 | public static function non_admin_manageable_steps() |
||
329 | |||
330 | private static $_last_order_step_cache = null; |
||
331 | |||
332 | /** |
||
333 | * @param bool $noCacheValues |
||
334 | * @return OrderStep |
||
335 | */ |
||
336 | public static function last_order_step($noCacheValues = false) |
||
344 | |||
345 | /** |
||
346 | * return StatusIDs (orderstep IDs) from orders that are bad.... |
||
347 | * (basically StatusID values that do not exist) |
||
348 | * |
||
349 | * @return array |
||
350 | */ |
||
351 | public static function bad_order_step_ids() |
||
363 | |||
364 | /** |
||
365 | * turns code into ID. |
||
366 | * |
||
367 | * @param string $code |
||
368 | * @param int |
||
369 | */ |
||
370 | public static function get_status_id_from_code($code) |
||
382 | |||
383 | /** |
||
384 | *@return array |
||
385 | **/ |
||
386 | public static function get_codes_for_order_steps_to_include() |
||
399 | |||
400 | /** |
||
401 | * returns a list of ordersteps that have not been created yet. |
||
402 | * |
||
403 | * @return array |
||
404 | **/ |
||
405 | public static function get_not_created_codes_for_order_steps_to_include() |
||
419 | |||
420 | /** |
||
421 | *@return string |
||
422 | **/ |
||
423 | public function getMyCode() |
||
432 | |||
433 | /** |
||
434 | * IMPORTANT:: MUST HAVE Code must be defined!!! |
||
435 | * standard SS variable. |
||
436 | * |
||
437 | * @return array |
||
438 | */ |
||
439 | private static $defaults = array( |
||
440 | 'CustomerCanEdit' => 0, |
||
441 | 'CustomerCanCancel' => 0, |
||
442 | 'CustomerCanPay' => 1, |
||
443 | 'ShowAsUncompletedOrder' => 0, |
||
444 | 'ShowAsInProcessOrder' => 0, |
||
445 | 'ShowAsCompletedOrder' => 0, |
||
446 | 'Code' => 'ORDERSTEP', |
||
447 | ); |
||
448 | |||
449 | /** |
||
450 | * standard SS method. |
||
451 | */ |
||
452 | public function populateDefaults() |
||
457 | |||
458 | /** |
||
459 | *@return FieldList |
||
460 | **/ |
||
461 | public function getCMSFields() |
||
578 | |||
579 | /** |
||
580 | * link to edit the record. |
||
581 | * |
||
582 | * @param string | Null $action - e.g. edit |
||
583 | * |
||
584 | * @return string |
||
585 | */ |
||
586 | public function CMSEditLink($action = null) |
||
590 | |||
591 | /** |
||
592 | * tells the order to display itself with an alternative display page. |
||
593 | * in that way, orders can be displayed differently for certain steps |
||
594 | * for example, in a print step, the order can be displayed in a |
||
595 | * PRINT ONLY format. |
||
596 | * |
||
597 | * When the method return null, the order is displayed using the standard display page |
||
598 | * |
||
599 | * @see Order::DisplayPage |
||
600 | * |
||
601 | * @return null|object (Page) |
||
602 | **/ |
||
603 | public function AlternativeDisplayPage() |
||
607 | |||
608 | /** |
||
609 | * Allows the opportunity for the Order Step to add any fields to Order::getCMSFields |
||
610 | * Usually this is added before ActionNextStepManually. |
||
611 | * |
||
612 | * @param FieldList $fields |
||
613 | * @param Order $order |
||
614 | * |
||
615 | * @return FieldList |
||
616 | **/ |
||
617 | public function addOrderStepFields(FieldList $fields, Order $order) |
||
621 | |||
622 | /** |
||
623 | *@return ValidationResult |
||
624 | **/ |
||
625 | public function validate() |
||
643 | |||
644 | /************************************************** |
||
645 | * moving between statusses... |
||
646 | **************************************************/ |
||
647 | /** |
||
648 | *initStep: |
||
649 | * makes sure the step is ready to run.... (e.g. check if the order is ready to be emailed as receipt). |
||
650 | * should be able to run this function many times to check if the step is ready. |
||
651 | * |
||
652 | * @see Order::doNextStatus |
||
653 | * |
||
654 | * @param Order object |
||
655 | * |
||
656 | * @return bool - true if the current step is ready to be run... |
||
657 | **/ |
||
658 | public function initStep(Order $order) |
||
664 | |||
665 | /** |
||
666 | *doStep: |
||
667 | * should only be able to run this function once |
||
668 | * (init stops you from running it twice - in theory....) |
||
669 | * runs the actual step. |
||
670 | * |
||
671 | * @see Order::doNextStatus |
||
672 | * |
||
673 | * @param Order object |
||
674 | * |
||
675 | * @return bool - true if run correctly. |
||
676 | **/ |
||
677 | public function doStep(Order $order) |
||
683 | |||
684 | /** |
||
685 | * nextStep: |
||
686 | * returns the next step (after it checks if everything is in place for the next step to run...). |
||
687 | * |
||
688 | * @see Order::doNextStatus |
||
689 | * |
||
690 | * @param Order $order |
||
691 | * |
||
692 | * @return OrderStep | Null (next step OrderStep object) |
||
693 | **/ |
||
694 | public function nextStep(Order $order) |
||
707 | |||
708 | /************************************************** |
||
709 | * Boolean checks |
||
710 | **************************************************/ |
||
711 | |||
712 | /** |
||
713 | * Checks if a step has passed (been completed) in comparison to the current step. |
||
714 | * |
||
715 | * @param string $code: the name of the step to check |
||
716 | * @param bool $orIsEqualTo if set to true, this method will return TRUE if the step being checked is the current one |
||
717 | * |
||
718 | * @return bool |
||
719 | **/ |
||
720 | public function hasPassed($code, $orIsEqualTo = false) |
||
739 | |||
740 | /** |
||
741 | * @param string $code |
||
742 | * |
||
743 | * @return bool |
||
744 | **/ |
||
745 | public function hasPassedOrIsEqualTo($code) |
||
749 | |||
750 | /** |
||
751 | * @param string $code |
||
752 | * |
||
753 | * @return bool |
||
754 | **/ |
||
755 | public function hasNotPassed($code) |
||
759 | |||
760 | /** |
||
761 | * Opposite of hasPassed. |
||
762 | * |
||
763 | * @param string $code |
||
764 | * |
||
765 | * @return bool |
||
766 | **/ |
||
767 | public function isBefore($code) |
||
771 | |||
772 | /** |
||
773 | *@return bool |
||
774 | **/ |
||
775 | protected function isDefaultStatusOption() |
||
779 | |||
780 | /************************************************** |
||
781 | |||
782 | **************************************************/ |
||
783 | |||
784 | /** |
||
785 | * @var string |
||
786 | */ |
||
787 | protected $emailClassName = ''; |
||
788 | |||
789 | /** |
||
790 | * returns the email class used for emailing the |
||
791 | * customer during a specific step (IF ANY!). |
||
792 | * |
||
793 | * @return string |
||
794 | */ |
||
795 | public function getEmailClassName() |
||
799 | |||
800 | /** |
||
801 | * return true if done already or mailed successfully now. |
||
802 | * |
||
803 | * @param order $order |
||
804 | * @param string $subject |
||
805 | * @param string $message |
||
806 | * @param bool $resend |
||
807 | * @param bool | string $adminOnlyOrToEmail you can set to false = send to customer, true: send to admin, or email = send to email |
||
808 | * @param string $emailClassName |
||
809 | * |
||
810 | * @return boolean; |
||
811 | */ |
||
812 | protected function sendEmailForStep( |
||
858 | |||
859 | /** |
||
860 | * sets the email class used for emailing the |
||
861 | * customer during a specific step (IF ANY!). |
||
862 | * |
||
863 | * @param string |
||
864 | */ |
||
865 | public function setEmailClassName($s) |
||
869 | |||
870 | /** |
||
871 | * returns a link that can be used to test |
||
872 | * the email being sent during this step |
||
873 | * this method returns NULL if no email |
||
874 | * is being sent OR if there is no suitable Order |
||
875 | * to test with... |
||
876 | * |
||
877 | * @return string |
||
878 | */ |
||
879 | protected function testEmailLink() |
||
905 | |||
906 | /** |
||
907 | * Has an email been sent to the customer for this |
||
908 | * order step. |
||
909 | *"-10 days". |
||
910 | * |
||
911 | * @param Order $order |
||
912 | * @param bool $checkDateOfOrder |
||
913 | * |
||
914 | * @return bool |
||
915 | **/ |
||
916 | public function hasBeenSent(Order $order, $checkDateOfOrder = true) |
||
940 | |||
941 | /** |
||
942 | * For some ordersteps this returns true... |
||
943 | * |
||
944 | * @return bool |
||
945 | **/ |
||
946 | protected function hasCustomerMessage() |
||
950 | |||
951 | |||
952 | /** |
||
953 | * Formatted answer for "hasCustomerMessage". |
||
954 | * |
||
955 | * @return string |
||
956 | */ |
||
957 | public function HasCustomerMessageNice() |
||
965 | |||
966 | /** |
||
967 | * Formatted answer for "hasCustomerMessage". |
||
968 | * |
||
969 | * @return string |
||
970 | */ |
||
971 | public function ShowAsSummary() |
||
975 | |||
976 | /** |
||
977 | * |
||
978 | * |
||
979 | * @return string |
||
980 | */ |
||
981 | public function getShowAsSummary() |
||
1014 | |||
1015 | /** |
||
1016 | * @return string |
||
1017 | */ |
||
1018 | protected function humanReadeableDeferTimeInSeconds() |
||
1035 | |||
1036 | /** |
||
1037 | * Formatted answer for "hasCustomerMessage". |
||
1038 | * |
||
1039 | * @return string |
||
1040 | */ |
||
1041 | public function NameAndDescription() |
||
1045 | |||
1046 | public function getNameAndDescription() |
||
1052 | |||
1053 | /** |
||
1054 | * This allows you to set the time to something other than the standard DeferTimeInSeconds |
||
1055 | * value based on the order provided. |
||
1056 | * |
||
1057 | * @param Order (optional) |
||
1058 | * |
||
1059 | * @return int |
||
1060 | */ |
||
1061 | public function CalculatedDeferTimeInSeconds($order = null) |
||
1065 | |||
1066 | /** |
||
1067 | * can this order step be delayed? |
||
1068 | * in general, if there is a customer message |
||
1069 | * we should be able to delay it |
||
1070 | * |
||
1071 | * This method can be overridden in any orderstep |
||
1072 | * @return bool |
||
1073 | **/ |
||
1074 | protected function canBeDefered() |
||
1078 | |||
1079 | |||
1080 | /************************************************** |
||
1081 | * Order Status Logs |
||
1082 | **************************************************/ |
||
1083 | |||
1084 | /** |
||
1085 | * The OrderStatusLog that is relevant to the particular step. |
||
1086 | * |
||
1087 | * @var string |
||
1088 | */ |
||
1089 | protected $relevantLogEntryClassName = ''; |
||
1090 | |||
1091 | /** |
||
1092 | * @return string |
||
1093 | */ |
||
1094 | public function getRelevantLogEntryClassName() |
||
1098 | |||
1099 | /** |
||
1100 | * @param string |
||
1101 | */ |
||
1102 | public function setRelevantLogEntryClassName($s) |
||
1106 | |||
1107 | /** |
||
1108 | * returns the OrderStatusLog that is relevant to this step. |
||
1109 | * |
||
1110 | * @param Order $order |
||
1111 | * |
||
1112 | * @return OrderStatusLog | null |
||
1113 | */ |
||
1114 | public function RelevantLogEntry(Order $order) |
||
1120 | |||
1121 | /** |
||
1122 | * returns the OrderStatusLogs that are relevant to this step. |
||
1123 | * It is important that getRelevantLogEntryClassName returns |
||
1124 | * a specific enough ClassName and not a base class name. |
||
1125 | * |
||
1126 | * @param Order $order |
||
1127 | * |
||
1128 | * @return DataObjectSet | null |
||
1129 | */ |
||
1130 | public function RelevantLogEntries(Order $order) |
||
1140 | |||
1141 | /************************************************** |
||
1142 | * Silverstripe Standard Data Object Methods |
||
1143 | **************************************************/ |
||
1144 | |||
1145 | /** |
||
1146 | * Standard SS method |
||
1147 | * These are only created programmatically. |
||
1148 | * |
||
1149 | * @param Member $member |
||
1150 | * |
||
1151 | * @return bool |
||
1152 | */ |
||
1153 | public function canCreate($member = null) |
||
1157 | |||
1158 | /** |
||
1159 | * Standard SS method. |
||
1160 | * |
||
1161 | * @param Member $member |
||
1162 | * |
||
1163 | * @return bool |
||
1164 | */ |
||
1165 | public function canView($member = null) |
||
1180 | |||
1181 | /** |
||
1182 | * the default for this is TRUE, but for completed order steps |
||
1183 | * |
||
1184 | * we do not allow this. |
||
1185 | * |
||
1186 | * @param Order $order |
||
1187 | * @param Member $member optional |
||
1188 | * @return bool |
||
1189 | */ |
||
1190 | public function canOverrideCanViewForOrder($order, $member = null) |
||
1199 | |||
1200 | /** |
||
1201 | * standard SS method. |
||
1202 | * |
||
1203 | * @param Member | NULL |
||
1204 | * |
||
1205 | * @return bool |
||
1206 | */ |
||
1207 | public function canEdit($member = null) |
||
1222 | |||
1223 | /** |
||
1224 | * Standard SS method. |
||
1225 | * |
||
1226 | * @param Member $member |
||
1227 | * |
||
1228 | * @return bool |
||
1229 | */ |
||
1230 | public function canDelete($member = null) |
||
1260 | |||
1261 | /** |
||
1262 | * standard SS method. |
||
1263 | */ |
||
1264 | public function onBeforeWrite() |
||
1293 | |||
1294 | /** |
||
1295 | * move linked orders to the next status |
||
1296 | * standard SS method. |
||
1297 | */ |
||
1298 | public function onBeforeDelete() |
||
1319 | |||
1320 | /** |
||
1321 | * standard SS method. |
||
1322 | */ |
||
1323 | public function onAfterDelete() |
||
1328 | |||
1329 | protected function NextOrderStep() |
||
1335 | |||
1336 | protected function PreviousOrderStep() |
||
1342 | |||
1343 | /** |
||
1344 | * standard SS method |
||
1345 | * USED TO BE: Unpaid,Query,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart. |
||
1346 | */ |
||
1347 | public function requireDefaultRecords() |
||
1414 | |||
1415 | /** |
||
1416 | * returns the standard EcommerceDBConfig for use within OrderSteps. |
||
1417 | * |
||
1418 | * @return EcommerceDBConfig |
||
1419 | */ |
||
1420 | protected function EcomConfig() |
||
1424 | |||
1425 | /** |
||
1426 | * Explains the current order step. |
||
1427 | * |
||
1428 | * @return string |
||
1429 | */ |
||
1430 | protected function myDescription() |
||
1434 | } |
||
1435 |
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.