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 = 'Order_InvoiceEmail'; |
||
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( |
||
861 | |||
862 | /** |
||
863 | * sets the email class used for emailing the |
||
864 | * customer during a specific step (IF ANY!). |
||
865 | * |
||
866 | * @param string |
||
867 | */ |
||
868 | public function setEmailClassName($s) |
||
872 | |||
873 | /** |
||
874 | * returns a link that can be used to test |
||
875 | * the email being sent during this step |
||
876 | * this method returns NULL if no email |
||
877 | * is being sent OR if there is no suitable Order |
||
878 | * to test with... |
||
879 | * |
||
880 | * @return string |
||
881 | */ |
||
882 | protected function testEmailLink() |
||
908 | |||
909 | /** |
||
910 | * Has an email been sent to the customer for this |
||
911 | * order step. |
||
912 | *"-10 days". |
||
913 | * |
||
914 | * @param Order $order |
||
915 | * @param bool $checkDateOfOrder |
||
916 | * |
||
917 | * @return bool |
||
918 | **/ |
||
919 | public function hasBeenSent(Order $order, $checkDateOfOrder = true) |
||
943 | |||
944 | /** |
||
945 | * For some ordersteps this returns true... |
||
946 | * |
||
947 | * @return bool |
||
948 | **/ |
||
949 | protected function hasCustomerMessage() |
||
953 | |||
954 | |||
955 | /** |
||
956 | * Formatted answer for "hasCustomerMessage". |
||
957 | * |
||
958 | * @return string |
||
959 | */ |
||
960 | public function HasCustomerMessageNice() |
||
968 | |||
969 | public function CalculatedEmailSubject($order = null) |
||
973 | |||
974 | public function CalculatedCustomerMessage($order = null) |
||
978 | |||
979 | /** |
||
980 | * Formatted answer for "hasCustomerMessage". |
||
981 | * |
||
982 | * @return string |
||
983 | */ |
||
984 | public function ShowAsSummary() |
||
988 | |||
989 | /** |
||
990 | * |
||
991 | * |
||
992 | * @return string |
||
993 | */ |
||
994 | public function getShowAsSummary() |
||
1027 | |||
1028 | /** |
||
1029 | * @return string |
||
1030 | */ |
||
1031 | protected function humanReadeableDeferTimeInSeconds() |
||
1048 | |||
1049 | /** |
||
1050 | * Formatted answer for "hasCustomerMessage". |
||
1051 | * |
||
1052 | * @return string |
||
1053 | */ |
||
1054 | public function NameAndDescription() |
||
1058 | |||
1059 | public function getNameAndDescription() |
||
1065 | |||
1066 | /** |
||
1067 | * This allows you to set the time to something other than the standard DeferTimeInSeconds |
||
1068 | * value based on the order provided. |
||
1069 | * |
||
1070 | * @param Order (optional) |
||
1071 | * |
||
1072 | * @return int |
||
1073 | */ |
||
1074 | public function CalculatedDeferTimeInSeconds($order = null) |
||
1078 | |||
1079 | /** |
||
1080 | * can this order step be delayed? |
||
1081 | * in general, if there is a customer message |
||
1082 | * we should be able to delay it |
||
1083 | * |
||
1084 | * This method can be overridden in any orderstep |
||
1085 | * @return bool |
||
1086 | **/ |
||
1087 | protected function canBeDefered() |
||
1091 | |||
1092 | |||
1093 | /************************************************** |
||
1094 | * Order Status Logs |
||
1095 | **************************************************/ |
||
1096 | |||
1097 | /** |
||
1098 | * The OrderStatusLog that is relevant to the particular step. |
||
1099 | * |
||
1100 | * @var string |
||
1101 | */ |
||
1102 | protected $relevantLogEntryClassName = ''; |
||
1103 | |||
1104 | /** |
||
1105 | * @return string |
||
1106 | */ |
||
1107 | public function getRelevantLogEntryClassName() |
||
1111 | |||
1112 | /** |
||
1113 | * @param string |
||
1114 | */ |
||
1115 | public function setRelevantLogEntryClassName($s) |
||
1119 | |||
1120 | /** |
||
1121 | * returns the OrderStatusLog that is relevant to this step. |
||
1122 | * |
||
1123 | * @param Order $order |
||
1124 | * |
||
1125 | * @return OrderStatusLog | null |
||
1126 | */ |
||
1127 | public function RelevantLogEntry(Order $order) |
||
1133 | |||
1134 | /** |
||
1135 | * returns the OrderStatusLogs that are relevant to this step. |
||
1136 | * It is important that getRelevantLogEntryClassName returns |
||
1137 | * a specific enough ClassName and not a base class name. |
||
1138 | * |
||
1139 | * @param Order $order |
||
1140 | * |
||
1141 | * @return DataObjectSet | null |
||
1142 | */ |
||
1143 | public function RelevantLogEntries(Order $order) |
||
1153 | |||
1154 | /************************************************** |
||
1155 | * Silverstripe Standard Data Object Methods |
||
1156 | **************************************************/ |
||
1157 | |||
1158 | /** |
||
1159 | * Standard SS method |
||
1160 | * These are only created programmatically. |
||
1161 | * |
||
1162 | * @param Member $member |
||
1163 | * |
||
1164 | * @return bool |
||
1165 | */ |
||
1166 | public function canCreate($member = null) |
||
1170 | |||
1171 | /** |
||
1172 | * Standard SS method. |
||
1173 | * |
||
1174 | * @param Member $member |
||
1175 | * |
||
1176 | * @return bool |
||
1177 | */ |
||
1178 | public function canView($member = null) |
||
1193 | |||
1194 | /** |
||
1195 | * the default for this is TRUE, but for completed order steps |
||
1196 | * |
||
1197 | * we do not allow this. |
||
1198 | * |
||
1199 | * @param Order $order |
||
1200 | * @param Member $member optional |
||
1201 | * @return bool |
||
1202 | */ |
||
1203 | public function canOverrideCanViewForOrder($order, $member = null) |
||
1212 | |||
1213 | /** |
||
1214 | * standard SS method. |
||
1215 | * |
||
1216 | * @param Member | NULL |
||
1217 | * |
||
1218 | * @return bool |
||
1219 | */ |
||
1220 | public function canEdit($member = null) |
||
1235 | |||
1236 | /** |
||
1237 | * Standard SS method. |
||
1238 | * |
||
1239 | * @param Member $member |
||
1240 | * |
||
1241 | * @return bool |
||
1242 | */ |
||
1243 | public function canDelete($member = null) |
||
1273 | |||
1274 | /** |
||
1275 | * standard SS method. |
||
1276 | */ |
||
1277 | public function onBeforeWrite() |
||
1306 | |||
1307 | /** |
||
1308 | * move linked orders to the next status |
||
1309 | * standard SS method. |
||
1310 | */ |
||
1311 | public function onBeforeDelete() |
||
1332 | |||
1333 | /** |
||
1334 | * standard SS method. |
||
1335 | */ |
||
1336 | public function onAfterDelete() |
||
1341 | |||
1342 | protected function NextOrderStep() |
||
1348 | |||
1349 | protected function PreviousOrderStep() |
||
1355 | |||
1356 | /** |
||
1357 | * standard SS method |
||
1358 | * USED TO BE: Unpaid,Query,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart. |
||
1359 | */ |
||
1360 | public function requireDefaultRecords() |
||
1365 | |||
1366 | protected function checkValidityOfOrderSteps() |
||
1432 | |||
1433 | /** |
||
1434 | * returns the standard EcommerceDBConfig for use within OrderSteps. |
||
1435 | * |
||
1436 | * @return EcommerceDBConfig |
||
1437 | */ |
||
1438 | protected function EcomConfig() |
||
1442 | |||
1443 | /** |
||
1444 | * Explains the current order step. |
||
1445 | * |
||
1446 | * @return string |
||
1447 | */ |
||
1448 | protected function myDescription() |
||
1452 | } |
||
1453 |
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.