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() |
||
577 | |||
578 | /** |
||
579 | * link to edit the record. |
||
580 | * |
||
581 | * @param string | Null $action - e.g. edit |
||
582 | * |
||
583 | * @return string |
||
584 | */ |
||
585 | public function CMSEditLink($action = null) |
||
589 | |||
590 | /** |
||
591 | * tells the order to display itself with an alternative display page. |
||
592 | * in that way, orders can be displayed differently for certain steps |
||
593 | * for example, in a print step, the order can be displayed in a |
||
594 | * PRINT ONLY format. |
||
595 | * |
||
596 | * When the method return null, the order is displayed using the standard display page |
||
597 | * |
||
598 | * @see Order::DisplayPage |
||
599 | * |
||
600 | * @return null|object (Page) |
||
601 | **/ |
||
602 | public function AlternativeDisplayPage() |
||
606 | |||
607 | /** |
||
608 | * Allows the opportunity for the Order Step to add any fields to Order::getCMSFields |
||
609 | * Usually this is added before ActionNextStepManually. |
||
610 | * |
||
611 | * @param FieldList $fields |
||
612 | * @param Order $order |
||
613 | * |
||
614 | * @return FieldList |
||
615 | **/ |
||
616 | public function addOrderStepFields(FieldList $fields, Order $order) |
||
620 | |||
621 | /** |
||
622 | *@return ValidationResult |
||
623 | **/ |
||
624 | public function validate() |
||
642 | |||
643 | /************************************************** |
||
644 | * moving between statusses... |
||
645 | **************************************************/ |
||
646 | /** |
||
647 | *initStep: |
||
648 | * makes sure the step is ready to run.... (e.g. check if the order is ready to be emailed as receipt). |
||
649 | * should be able to run this function many times to check if the step is ready. |
||
650 | * |
||
651 | * @see Order::doNextStatus |
||
652 | * |
||
653 | * @param Order object |
||
654 | * |
||
655 | * @return bool - true if the current step is ready to be run... |
||
656 | **/ |
||
657 | public function initStep(Order $order) |
||
663 | |||
664 | /** |
||
665 | *doStep: |
||
666 | * should only be able to run this function once |
||
667 | * (init stops you from running it twice - in theory....) |
||
668 | * runs the actual step. |
||
669 | * |
||
670 | * @see Order::doNextStatus |
||
671 | * |
||
672 | * @param Order object |
||
673 | * |
||
674 | * @return bool - true if run correctly. |
||
675 | **/ |
||
676 | public function doStep(Order $order) |
||
682 | |||
683 | /** |
||
684 | * nextStep: |
||
685 | * returns the next step (after it checks if everything is in place for the next step to run...). |
||
686 | * |
||
687 | * @see Order::doNextStatus |
||
688 | * |
||
689 | * @param Order $order |
||
690 | * |
||
691 | * @return OrderStep | Null (next step OrderStep object) |
||
692 | **/ |
||
693 | public function nextStep(Order $order) |
||
710 | |||
711 | /************************************************** |
||
712 | * Boolean checks |
||
713 | **************************************************/ |
||
714 | |||
715 | /** |
||
716 | * Checks if a step has passed (been completed) in comparison to the current step. |
||
717 | * |
||
718 | * @param string $code: the name of the step to check |
||
719 | * @param bool $orIsEqualTo if set to true, this method will return TRUE if the step being checked is the current one |
||
720 | * |
||
721 | * @return bool |
||
722 | **/ |
||
723 | public function hasPassed($code, $orIsEqualTo = false) |
||
742 | |||
743 | /** |
||
744 | * @param string $code |
||
745 | * |
||
746 | * @return bool |
||
747 | **/ |
||
748 | public function hasPassedOrIsEqualTo($code) |
||
752 | |||
753 | /** |
||
754 | * @param string $code |
||
755 | * |
||
756 | * @return bool |
||
757 | **/ |
||
758 | public function hasNotPassed($code) |
||
762 | |||
763 | /** |
||
764 | * Opposite of hasPassed. |
||
765 | * |
||
766 | * @param string $code |
||
767 | * |
||
768 | * @return bool |
||
769 | **/ |
||
770 | public function isBefore($code) |
||
774 | |||
775 | /** |
||
776 | *@return bool |
||
777 | **/ |
||
778 | protected function isDefaultStatusOption() |
||
782 | |||
783 | /************************************************** |
||
784 | |||
785 | **************************************************/ |
||
786 | |||
787 | /** |
||
788 | * @var string |
||
789 | */ |
||
790 | protected $emailClassName = 'Order_InvoiceEmail'; |
||
791 | |||
792 | /** |
||
793 | * returns the email class used for emailing the |
||
794 | * customer during a specific step (IF ANY!). |
||
795 | * |
||
796 | * @return string |
||
797 | */ |
||
798 | public function getEmailClassName() |
||
802 | |||
803 | /** |
||
804 | * return true if done already or mailed successfully now. |
||
805 | * |
||
806 | * @param order $order |
||
807 | * @param string $subject |
||
808 | * @param string $message |
||
809 | * @param bool $resend |
||
810 | * @param bool | string $adminOnlyOrToEmail you can set to false = send to customer, true: send to admin, or email = send to email |
||
811 | * @param string $emailClassName |
||
812 | * |
||
813 | * @return boolean; |
||
814 | */ |
||
815 | protected function sendEmailForStep( |
||
864 | |||
865 | /** |
||
866 | * sets the email class used for emailing the |
||
867 | * customer during a specific step (IF ANY!). |
||
868 | * |
||
869 | * @param string |
||
870 | */ |
||
871 | public function setEmailClassName($s) |
||
875 | |||
876 | /** |
||
877 | * returns a link that can be used to test |
||
878 | * the email being sent during this step |
||
879 | * this method returns NULL if no email |
||
880 | * is being sent OR if there is no suitable Order |
||
881 | * to test with... |
||
882 | * |
||
883 | * @return string |
||
884 | */ |
||
885 | protected function testEmailLink() |
||
911 | |||
912 | /** |
||
913 | * Has an email been sent to the customer for this |
||
914 | * order step. |
||
915 | *"-10 days". |
||
916 | * |
||
917 | * @param Order $order |
||
918 | * @param bool $checkDateOfOrder |
||
919 | * |
||
920 | * @return bool |
||
921 | **/ |
||
922 | public function hasBeenSent(Order $order, $checkDateOfOrder = true) |
||
946 | |||
947 | /** |
||
948 | * For some ordersteps this returns true... |
||
949 | * |
||
950 | * @return bool |
||
951 | **/ |
||
952 | protected function hasCustomerMessage() |
||
956 | |||
957 | |||
958 | /** |
||
959 | * Formatted answer for "hasCustomerMessage". |
||
960 | * |
||
961 | * @return string |
||
962 | */ |
||
963 | public function HasCustomerMessageNice() |
||
971 | |||
972 | public function CalculatedEmailSubject($order = null) |
||
976 | |||
977 | public function CalculatedCustomerMessage($order = null) |
||
981 | |||
982 | /** |
||
983 | * Formatted answer for "hasCustomerMessage". |
||
984 | * |
||
985 | * @return string |
||
986 | */ |
||
987 | public function ShowAsSummary() |
||
991 | |||
992 | /** |
||
993 | * |
||
994 | * |
||
995 | * @return string |
||
996 | */ |
||
997 | public function getShowAsSummary() |
||
1030 | |||
1031 | /** |
||
1032 | * @return string |
||
1033 | */ |
||
1034 | protected function humanReadeableDeferTimeInSeconds() |
||
1051 | |||
1052 | /** |
||
1053 | * Formatted answer for "hasCustomerMessage". |
||
1054 | * |
||
1055 | * @return string |
||
1056 | */ |
||
1057 | public function NameAndDescription() |
||
1061 | |||
1062 | public function getNameAndDescription() |
||
1068 | |||
1069 | /** |
||
1070 | * This allows you to set the time to something other than the standard DeferTimeInSeconds |
||
1071 | * value based on the order provided. |
||
1072 | * |
||
1073 | * @param Order (optional) |
||
1074 | * |
||
1075 | * @return int |
||
1076 | */ |
||
1077 | public function CalculatedDeferTimeInSeconds($order = null) |
||
1081 | |||
1082 | /** |
||
1083 | * can this order step be delayed? |
||
1084 | * in general, if there is a customer message |
||
1085 | * we should be able to delay it |
||
1086 | * |
||
1087 | * This method can be overridden in any orderstep |
||
1088 | * @return bool |
||
1089 | **/ |
||
1090 | protected function canBeDefered() |
||
1094 | |||
1095 | |||
1096 | /************************************************** |
||
1097 | * Order Status Logs |
||
1098 | **************************************************/ |
||
1099 | |||
1100 | /** |
||
1101 | * The OrderStatusLog that is relevant to the particular step. |
||
1102 | * |
||
1103 | * @var string |
||
1104 | */ |
||
1105 | protected $relevantLogEntryClassName = ''; |
||
1106 | |||
1107 | /** |
||
1108 | * @return string |
||
1109 | */ |
||
1110 | public function getRelevantLogEntryClassName() |
||
1114 | |||
1115 | /** |
||
1116 | * @param string |
||
1117 | */ |
||
1118 | public function setRelevantLogEntryClassName($s) |
||
1122 | |||
1123 | /** |
||
1124 | * returns the OrderStatusLog that is relevant to this step. |
||
1125 | * |
||
1126 | * @param Order $order |
||
1127 | * |
||
1128 | * @return OrderStatusLog | null |
||
1129 | */ |
||
1130 | public function RelevantLogEntry(Order $order) |
||
1136 | |||
1137 | /** |
||
1138 | * returns the OrderStatusLogs that are relevant to this step. |
||
1139 | * It is important that getRelevantLogEntryClassName returns |
||
1140 | * a specific enough ClassName and not a base class name. |
||
1141 | * |
||
1142 | * @param Order $order |
||
1143 | * |
||
1144 | * @return DataObjectSet | null |
||
1145 | */ |
||
1146 | public function RelevantLogEntries(Order $order) |
||
1156 | |||
1157 | /************************************************** |
||
1158 | * Silverstripe Standard Data Object Methods |
||
1159 | **************************************************/ |
||
1160 | |||
1161 | /** |
||
1162 | * Standard SS method |
||
1163 | * These are only created programmatically. |
||
1164 | * |
||
1165 | * @param Member $member |
||
1166 | * |
||
1167 | * @return bool |
||
1168 | */ |
||
1169 | public function canCreate($member = null) |
||
1173 | |||
1174 | /** |
||
1175 | * Standard SS method. |
||
1176 | * |
||
1177 | * @param Member $member |
||
1178 | * |
||
1179 | * @return bool |
||
1180 | */ |
||
1181 | public function canView($member = null) |
||
1196 | |||
1197 | /** |
||
1198 | * the default for this is TRUE, but for completed order steps |
||
1199 | * |
||
1200 | * we do not allow this. |
||
1201 | * |
||
1202 | * @param Order $order |
||
1203 | * @param Member $member optional |
||
1204 | * @return bool |
||
1205 | */ |
||
1206 | public function canOverrideCanViewForOrder($order, $member = null) |
||
1215 | |||
1216 | /** |
||
1217 | * standard SS method. |
||
1218 | * |
||
1219 | * @param Member | NULL |
||
1220 | * |
||
1221 | * @return bool |
||
1222 | */ |
||
1223 | public function canEdit($member = null) |
||
1238 | |||
1239 | /** |
||
1240 | * Standard SS method. |
||
1241 | * |
||
1242 | * @param Member $member |
||
1243 | * |
||
1244 | * @return bool |
||
1245 | */ |
||
1246 | public function canDelete($member = null) |
||
1276 | |||
1277 | /** |
||
1278 | * standard SS method. |
||
1279 | */ |
||
1280 | public function onBeforeWrite() |
||
1309 | |||
1310 | /** |
||
1311 | * move linked orders to the next status |
||
1312 | * standard SS method. |
||
1313 | */ |
||
1314 | public function onBeforeDelete() |
||
1335 | |||
1336 | /** |
||
1337 | * standard SS method. |
||
1338 | */ |
||
1339 | public function onAfterDelete() |
||
1344 | |||
1345 | protected function NextOrderStep() |
||
1351 | |||
1352 | protected function PreviousOrderStep() |
||
1358 | |||
1359 | /** |
||
1360 | * standard SS method |
||
1361 | * USED TO BE: Unpaid,Query,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart. |
||
1362 | */ |
||
1363 | public function requireDefaultRecords() |
||
1368 | |||
1369 | protected function checkValidityOfOrderSteps() |
||
1435 | |||
1436 | /** |
||
1437 | * returns the standard EcommerceDBConfig for use within OrderSteps. |
||
1438 | * |
||
1439 | * @return EcommerceDBConfig |
||
1440 | */ |
||
1441 | protected function EcomConfig() |
||
1445 | |||
1446 | /** |
||
1447 | * Explains the current order step. |
||
1448 | * |
||
1449 | * @return string |
||
1450 | */ |
||
1451 | protected function myDescription() |
||
1455 | } |
||
1456 |