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() |
||
316 | /** |
||
317 | * returns all the order steps |
||
318 | * that the admin should / can edit.... |
||
319 | * |
||
320 | * @return DataList |
||
321 | */ |
||
322 | public static function non_admin_manageable_steps() |
||
328 | |||
329 | /** |
||
330 | * return StatusIDs (orderstep IDs) from orders that are bad.... |
||
331 | * (basically StatusID values that do not exist) |
||
332 | * |
||
333 | * @return array |
||
334 | */ |
||
335 | public static function bad_order_step_ids() |
||
347 | |||
348 | /** |
||
349 | * turns code into ID. |
||
350 | * |
||
351 | * @param string $code |
||
352 | * @param int |
||
353 | */ |
||
354 | public static function get_status_id_from_code($code) |
||
365 | |||
366 | /** |
||
367 | *@return array |
||
368 | **/ |
||
369 | public static function get_codes_for_order_steps_to_include() |
||
382 | |||
383 | /** |
||
384 | * returns a list of ordersteps that have not been created yet. |
||
385 | * |
||
386 | * @return array |
||
387 | **/ |
||
388 | public static function get_not_created_codes_for_order_steps_to_include() |
||
402 | |||
403 | /** |
||
404 | *@return string |
||
405 | **/ |
||
406 | public function getMyCode() |
||
415 | |||
416 | /** |
||
417 | * IMPORTANT:: MUST HAVE Code must be defined!!! |
||
418 | * standard SS variable. |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | private static $defaults = array( |
||
423 | 'CustomerCanEdit' => 0, |
||
424 | 'CustomerCanCancel' => 0, |
||
425 | 'CustomerCanPay' => 1, |
||
426 | 'ShowAsUncompletedOrder' => 0, |
||
427 | 'ShowAsInProcessOrder' => 0, |
||
428 | 'ShowAsCompletedOrder' => 0, |
||
429 | 'Code' => 'ORDERSTEP', |
||
430 | ); |
||
431 | |||
432 | /** |
||
433 | * standard SS method. |
||
434 | */ |
||
435 | public function populateDefaults() |
||
440 | |||
441 | /** |
||
442 | *@return FieldList |
||
443 | **/ |
||
444 | public function getCMSFields() |
||
551 | |||
552 | /** |
||
553 | * link to edit the record. |
||
554 | * |
||
555 | * @param string | Null $action - e.g. edit |
||
556 | * |
||
557 | * @return string |
||
558 | */ |
||
559 | public function CMSEditLink($action = null) |
||
567 | |||
568 | /** |
||
569 | * tells the order to display itself with an alternative display page. |
||
570 | * in that way, orders can be displayed differently for certain steps |
||
571 | * for example, in a print step, the order can be displayed in a |
||
572 | * PRINT ONLY format. |
||
573 | * |
||
574 | * When the method return null, the order is displayed using the standard display page |
||
575 | * |
||
576 | * @see Order::DisplayPage |
||
577 | * |
||
578 | * @return null|object (Page) |
||
579 | **/ |
||
580 | public function AlternativeDisplayPage() |
||
584 | |||
585 | /** |
||
586 | * Allows the opportunity for the Order Step to add any fields to Order::getCMSFields |
||
587 | * Usually this is added before ActionNextStepManually. |
||
588 | * |
||
589 | * @param FieldList $fields |
||
590 | * @param Order $order |
||
591 | * |
||
592 | * @return FieldList |
||
593 | **/ |
||
594 | public function addOrderStepFields(FieldList $fields, Order $order) |
||
598 | |||
599 | /** |
||
600 | *@return ValidationResult |
||
601 | **/ |
||
602 | public function validate() |
||
620 | |||
621 | /************************************************** |
||
622 | * moving between statusses... |
||
623 | **************************************************/ |
||
624 | /** |
||
625 | *initStep: |
||
626 | * makes sure the step is ready to run.... (e.g. check if the order is ready to be emailed as receipt). |
||
627 | * should be able to run this function many times to check if the step is ready. |
||
628 | * |
||
629 | * @see Order::doNextStatus |
||
630 | * |
||
631 | * @param Order object |
||
632 | * |
||
633 | * @return bool - true if the current step is ready to be run... |
||
634 | **/ |
||
635 | public function initStep(Order $order) |
||
641 | |||
642 | /** |
||
643 | *doStep: |
||
644 | * should only be able to run this function once |
||
645 | * (init stops you from running it twice - in theory....) |
||
646 | * runs the actual step. |
||
647 | * |
||
648 | * @see Order::doNextStatus |
||
649 | * |
||
650 | * @param Order object |
||
651 | * |
||
652 | * @return bool - true if run correctly. |
||
653 | **/ |
||
654 | public function doStep(Order $order) |
||
660 | |||
661 | /** |
||
662 | * nextStep: |
||
663 | * returns the next step (after it checks if everything is in place for the next step to run...). |
||
664 | * |
||
665 | * @see Order::doNextStatus |
||
666 | * |
||
667 | * @param Order $order |
||
668 | * |
||
669 | * @return OrderStep | Null (next step OrderStep object) |
||
670 | **/ |
||
671 | public function nextStep(Order $order) |
||
682 | |||
683 | /************************************************** |
||
684 | * Boolean checks |
||
685 | **************************************************/ |
||
686 | |||
687 | /** |
||
688 | * Checks if a step has passed (been completed) in comparison to the current step. |
||
689 | * |
||
690 | * @param string $code: the name of the step to check |
||
691 | * @param bool $orIsEqualTo if set to true, this method will return TRUE if the step being checked is the current one |
||
692 | * |
||
693 | * @return bool |
||
694 | **/ |
||
695 | public function hasPassed($code, $orIsEqualTo = false) |
||
713 | |||
714 | /** |
||
715 | * @param string $code |
||
716 | * |
||
717 | * @return bool |
||
718 | **/ |
||
719 | public function hasPassedOrIsEqualTo($code) |
||
723 | |||
724 | /** |
||
725 | * @param string $code |
||
726 | * |
||
727 | * @return bool |
||
728 | **/ |
||
729 | public function hasNotPassed($code) |
||
733 | |||
734 | /** |
||
735 | * Opposite of hasPassed. |
||
736 | * |
||
737 | * @param string $code |
||
738 | * |
||
739 | * @return bool |
||
740 | **/ |
||
741 | public function isBefore($code) |
||
745 | |||
746 | /** |
||
747 | *@return bool |
||
748 | **/ |
||
749 | protected function isDefaultStatusOption() |
||
753 | |||
754 | /************************************************** |
||
755 | |||
756 | **************************************************/ |
||
757 | |||
758 | /** |
||
759 | * @var string |
||
760 | */ |
||
761 | protected $emailClassName = ''; |
||
762 | |||
763 | /** |
||
764 | * returns the email class used for emailing the |
||
765 | * customer during a specific step (IF ANY!). |
||
766 | * |
||
767 | * @return string |
||
768 | */ |
||
769 | public function getEmailClassName() |
||
773 | |||
774 | /** |
||
775 | * return true if done already or mailed successfully now. |
||
776 | * |
||
777 | * @param order $order |
||
778 | * @param string $subject |
||
779 | * @param string $message |
||
780 | * @param bool $resend |
||
781 | * @param bool | string $adminOnlyOrToEmail you can set to false = send to customer, true: send to admin, or email = send to email |
||
782 | * @param string $emailClassName |
||
783 | * |
||
784 | * @return boolean; |
||
785 | */ |
||
786 | protected function sendEmailForStep( |
||
832 | |||
833 | /** |
||
834 | * sets the email class used for emailing the |
||
835 | * customer during a specific step (IF ANY!). |
||
836 | * |
||
837 | * @param string |
||
838 | */ |
||
839 | public function setEmailClassName($s) |
||
843 | |||
844 | /** |
||
845 | * returns a link that can be used to test |
||
846 | * the email being sent during this step |
||
847 | * this method returns NULL if no email |
||
848 | * is being sent OR if there is no suitable Order |
||
849 | * to test with... |
||
850 | * |
||
851 | * @return string |
||
852 | */ |
||
853 | protected function testEmailLink() |
||
876 | |||
877 | /** |
||
878 | * Has an email been sent to the customer for this |
||
879 | * order step. |
||
880 | *"-10 days". |
||
881 | * |
||
882 | * @param Order $order |
||
883 | * @param bool $checkDateOfOrder |
||
884 | * |
||
885 | * @return bool |
||
886 | **/ |
||
887 | public function hasBeenSent(Order $order, $checkDateOfOrder = true) |
||
911 | |||
912 | /** |
||
913 | * For some ordersteps this returns true... |
||
914 | * |
||
915 | * @return bool |
||
916 | **/ |
||
917 | protected function hasCustomerMessage() |
||
921 | |||
922 | |||
923 | /** |
||
924 | * Formatted answer for "hasCustomerMessage". |
||
925 | * |
||
926 | * @return string |
||
927 | */ |
||
928 | public function HasCustomerMessageNice() |
||
936 | |||
937 | /** |
||
938 | * Formatted answer for "hasCustomerMessage". |
||
939 | * |
||
940 | * @return string |
||
941 | */ |
||
942 | public function ShowAsSummary() |
||
946 | |||
947 | /** |
||
948 | * |
||
949 | * |
||
950 | * @return string |
||
951 | */ |
||
952 | public function getShowAsSummary() |
||
985 | |||
986 | /** |
||
987 | * @return string |
||
988 | */ |
||
989 | protected function humanReadeableDeferTimeInSeconds() |
||
1006 | |||
1007 | /** |
||
1008 | * Formatted answer for "hasCustomerMessage". |
||
1009 | * |
||
1010 | * @return string |
||
1011 | */ |
||
1012 | public function NameAndDescription() |
||
1016 | |||
1017 | public function getNameAndDescription() |
||
1023 | |||
1024 | /** |
||
1025 | * This allows you to set the time to something other than the standard DeferTimeInSeconds |
||
1026 | * value based on the order provided. |
||
1027 | * |
||
1028 | * @param Order |
||
1029 | * |
||
1030 | * @return int |
||
1031 | */ |
||
1032 | public function CalculatedDeferTimeInSeconds($order) |
||
1036 | |||
1037 | /** |
||
1038 | * can this order step be delayed? |
||
1039 | * in general, if there is a customer message |
||
1040 | * we should be able to delay it |
||
1041 | * |
||
1042 | * This method can be overridden in any orderstep |
||
1043 | * @return bool |
||
1044 | **/ |
||
1045 | protected function canBeDefered() |
||
1049 | |||
1050 | |||
1051 | /************************************************** |
||
1052 | * Order Status Logs |
||
1053 | **************************************************/ |
||
1054 | |||
1055 | /** |
||
1056 | * The OrderStatusLog that is relevant to the particular step. |
||
1057 | * |
||
1058 | * @var string |
||
1059 | */ |
||
1060 | protected $relevantLogEntryClassName = ''; |
||
1061 | |||
1062 | /** |
||
1063 | * @return string |
||
1064 | */ |
||
1065 | public function getRelevantLogEntryClassName() |
||
1069 | |||
1070 | /** |
||
1071 | * @param string |
||
1072 | */ |
||
1073 | public function setRelevantLogEntryClassName($s) |
||
1077 | |||
1078 | /** |
||
1079 | * returns the OrderStatusLog that is relevant to this step. |
||
1080 | * |
||
1081 | * @param Order $order |
||
1082 | * |
||
1083 | * @return OrderStatusLog | null |
||
1084 | */ |
||
1085 | public function RelevantLogEntry(Order $order) |
||
1091 | |||
1092 | /** |
||
1093 | * returns the OrderStatusLogs that are relevant to this step. |
||
1094 | * It is important that getRelevantLogEntryClassName returns |
||
1095 | * a specific enough ClassName and not a base class name. |
||
1096 | * |
||
1097 | * @param Order $order |
||
1098 | * |
||
1099 | * @return DataObjectSet | null |
||
1100 | */ |
||
1101 | public function RelevantLogEntries(Order $order) |
||
1111 | |||
1112 | /************************************************** |
||
1113 | * Silverstripe Standard Data Object Methods |
||
1114 | **************************************************/ |
||
1115 | |||
1116 | /** |
||
1117 | * Standard SS method |
||
1118 | * These are only created programmatically. |
||
1119 | * |
||
1120 | * @param Member $member |
||
1121 | * |
||
1122 | * @return bool |
||
1123 | */ |
||
1124 | public function canCreate($member = null) |
||
1128 | |||
1129 | /** |
||
1130 | * Standard SS method. |
||
1131 | * |
||
1132 | * @param Member $member |
||
1133 | * |
||
1134 | * @return bool |
||
1135 | */ |
||
1136 | public function canView($member = null) |
||
1151 | |||
1152 | /** |
||
1153 | * the default for this is TRUE, but for completed order steps |
||
1154 | * |
||
1155 | * we do not allow this. |
||
1156 | * |
||
1157 | * @param Order $order |
||
1158 | * @param Member $member optional |
||
1159 | * @return bool |
||
1160 | */ |
||
1161 | public function canOverrideCanViewForOrder($order, $member = null) |
||
1170 | |||
1171 | /** |
||
1172 | * standard SS method. |
||
1173 | * |
||
1174 | * @param Member | NULL |
||
1175 | * |
||
1176 | * @return bool |
||
1177 | */ |
||
1178 | public function canEdit($member = null) |
||
1193 | |||
1194 | /** |
||
1195 | * Standard SS method. |
||
1196 | * |
||
1197 | * @param Member $member |
||
1198 | * |
||
1199 | * @return bool |
||
1200 | */ |
||
1201 | public function canDelete($member = null) |
||
1231 | |||
1232 | /** |
||
1233 | * standard SS method. |
||
1234 | */ |
||
1235 | public function onBeforeWrite() |
||
1264 | |||
1265 | /** |
||
1266 | * move linked orders to the next status |
||
1267 | * standard SS method. |
||
1268 | */ |
||
1269 | public function onBeforeDelete() |
||
1290 | |||
1291 | /** |
||
1292 | * standard SS method. |
||
1293 | */ |
||
1294 | public function onAfterDelete() |
||
1299 | |||
1300 | protected function NextOrderStep() |
||
1306 | |||
1307 | protected function PreviousOrderStep() |
||
1313 | |||
1314 | /** |
||
1315 | * standard SS method |
||
1316 | * USED TO BE: Unpaid,Query,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart. |
||
1317 | */ |
||
1318 | public function requireDefaultRecords() |
||
1374 | |||
1375 | /** |
||
1376 | * returns the standard EcommerceDBConfig for use within OrderSteps. |
||
1377 | * |
||
1378 | * @return EcommerceDBConfig |
||
1379 | */ |
||
1380 | protected function EcomConfig() |
||
1384 | |||
1385 | /** |
||
1386 | * Explains the current order step. |
||
1387 | * |
||
1388 | * @return string |
||
1389 | */ |
||
1390 | protected function myDescription() |
||
1394 | } |
||
1395 |
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.