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 | * standard SS variable. |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | private static $db = array( |
||
21 | 'Name' => 'Varchar(50)', |
||
22 | 'Code' => 'Varchar(50)', |
||
23 | 'Description' => 'Text', |
||
24 | 'EmailSubject' => 'Varchar(200)', |
||
25 | 'CustomerMessage' => 'HTMLText', |
||
26 | //customer privileges |
||
27 | 'CustomerCanEdit' => 'Boolean', |
||
28 | 'CustomerCanCancel' => 'Boolean', |
||
29 | 'CustomerCanPay' => 'Boolean', |
||
30 | //What to show the customer... |
||
31 | 'ShowAsUncompletedOrder' => 'Boolean', |
||
32 | 'ShowAsInProcessOrder' => 'Boolean', |
||
33 | 'ShowAsCompletedOrder' => 'Boolean', |
||
34 | 'HideStepFromCustomer' => 'Boolean', |
||
35 | //sorting index |
||
36 | 'Sort' => 'Int', |
||
37 | 'DeferTimeInSeconds' => 'Int', |
||
38 | 'DeferFromSubmitTime' => 'Boolean' |
||
39 | ); |
||
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | * standard SS variable. |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | private static $indexes = array( |
||
49 | 'Code' => true, |
||
50 | 'Sort' => true, |
||
51 | ); |
||
52 | |||
53 | /** |
||
54 | * standard SS variable. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | private static $has_many = array( |
||
59 | 'Orders' => 'Order', |
||
60 | 'OrderEmailRecords' => 'OrderEmailRecord', |
||
61 | ); |
||
62 | |||
63 | /** |
||
64 | * standard SS variable. |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | private static $field_labels = array( |
||
69 | 'Sort' => 'Sorting Index', |
||
70 | 'CustomerCanEdit' => 'Customer can edit order', |
||
71 | 'CustomerCanPay' => 'Customer can pay order', |
||
72 | 'CustomerCanCancel' => 'Customer can cancel order', |
||
73 | ); |
||
74 | |||
75 | /** |
||
76 | * standard SS variable. |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | private static $summary_fields = array( |
||
81 | 'NameAndDescription' => 'Step', |
||
82 | 'ShowAsSummary' => 'Phase', |
||
83 | ); |
||
84 | |||
85 | /** |
||
86 | * standard SS variable. |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | private static $casting = array( |
||
91 | 'Title' => 'Varchar', |
||
92 | 'CustomerCanEditNice' => 'Varchar', |
||
93 | 'CustomerCanPayNice' => 'Varchar', |
||
94 | 'CustomerCanCancelNice' => 'Varchar', |
||
95 | 'ShowAsUncompletedOrderNice' => 'Varchar', |
||
96 | 'ShowAsInProcessOrderNice' => 'Varchar', |
||
97 | 'ShowAsCompletedOrderNice' => 'Varchar', |
||
98 | 'HideStepFromCustomerNice' => 'Varchar', |
||
99 | 'HasCustomerMessageNice' => 'Varchar', |
||
100 | 'ShowAsSummary' => 'HTMLText', |
||
101 | 'NameAndDescription' => 'HTMLText' |
||
102 | ); |
||
103 | |||
104 | /** |
||
105 | * standard SS variable. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | private static $searchable_fields = array( |
||
110 | 'Name' => array( |
||
111 | 'title' => 'Name', |
||
112 | 'filter' => 'PartialMatchFilter', |
||
113 | ), |
||
114 | 'Code' => array( |
||
115 | 'title' => 'Code', |
||
116 | 'filter' => 'PartialMatchFilter', |
||
117 | ), |
||
118 | ); |
||
119 | |||
120 | |||
121 | /** |
||
122 | * casted variable. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | public function Title() |
||
134 | |||
135 | /** |
||
136 | * casted variable. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function CustomerCanEditNice() |
||
152 | |||
153 | /** |
||
154 | * casted variable. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function CustomerCanPayNice() |
||
170 | |||
171 | /** |
||
172 | * casted variable. |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function CustomerCanCancelNice() |
||
188 | |||
189 | public function ShowAsUncompletedOrderNice() |
||
201 | |||
202 | /** |
||
203 | * casted variable. |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | public function ShowAsInProcessOrderNice() |
||
219 | |||
220 | /** |
||
221 | * casted variable. |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | public function ShowAsCompletedOrderNice() |
||
237 | |||
238 | /** |
||
239 | * do not show in steps at all. |
||
240 | * @return boolean |
||
241 | */ |
||
242 | public function HideFromEveryone() |
||
246 | |||
247 | /** |
||
248 | * casted variable. |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | public function HideStepFromCustomerNice() |
||
256 | |||
257 | public function getHideStepFromCustomerNice() |
||
265 | |||
266 | /** |
||
267 | * standard SS variable. |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | private static $singular_name = 'Order Step'; |
||
272 | public function i18n_singular_name() |
||
276 | |||
277 | /** |
||
278 | * standard SS variable. |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | private static $plural_name = 'Order Steps'; |
||
283 | public function i18n_plural_name() |
||
287 | |||
288 | /** |
||
289 | * Standard SS variable. |
||
290 | * |
||
291 | * @var string |
||
292 | */ |
||
293 | private static $description = 'A step that any order goes through.'; |
||
294 | |||
295 | /** |
||
296 | * SUPER IMPORTANT TO KEEP ORDER! |
||
297 | * standard SS variable. |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | private static $default_sort = '"Sort" ASC'; |
||
302 | |||
303 | /** |
||
304 | * returns all the order steps |
||
305 | * that the admin should / can edit.... |
||
306 | * |
||
307 | * @return DataList |
||
308 | */ |
||
309 | public static function admin_manageable_steps() |
||
314 | |||
315 | /** |
||
316 | * return StatusIDs (orderstep IDs) from orders that are bad.... |
||
317 | * (basically StatusID values that do not exist) |
||
318 | * |
||
319 | * @return array |
||
320 | */ |
||
321 | public static function bad_order_step_ids() |
||
333 | |||
334 | /** |
||
335 | * turns code into ID. |
||
336 | * |
||
337 | * @param string $code |
||
338 | * @param int |
||
339 | */ |
||
340 | public static function get_status_id_from_code($code) |
||
351 | |||
352 | /** |
||
353 | *@return array |
||
354 | **/ |
||
355 | public static function get_codes_for_order_steps_to_include() |
||
368 | |||
369 | /** |
||
370 | * returns a list of ordersteps that have not been created yet. |
||
371 | * |
||
372 | * @return array |
||
373 | **/ |
||
374 | public static function get_not_created_codes_for_order_steps_to_include() |
||
388 | |||
389 | /** |
||
390 | *@return string |
||
391 | **/ |
||
392 | public function getMyCode() |
||
401 | |||
402 | /** |
||
403 | * IMPORTANT:: MUST HAVE Code must be defined!!! |
||
404 | * standard SS variable. |
||
405 | * |
||
406 | * @return array |
||
407 | */ |
||
408 | private static $defaults = array( |
||
409 | 'CustomerCanEdit' => 0, |
||
410 | 'CustomerCanCancel' => 0, |
||
411 | 'CustomerCanPay' => 1, |
||
412 | 'ShowAsUncompletedOrder' => 0, |
||
413 | 'ShowAsInProcessOrder' => 0, |
||
414 | 'ShowAsCompletedOrder' => 0, |
||
415 | 'Code' => 'ORDERSTEP', |
||
416 | ); |
||
417 | |||
418 | /** |
||
419 | * standard SS method. |
||
420 | */ |
||
421 | public function populateDefaults() |
||
426 | |||
427 | /** |
||
428 | *@return FieldList |
||
429 | **/ |
||
430 | public function getCMSFields() |
||
513 | |||
514 | /** |
||
515 | * link to edit the record. |
||
516 | * |
||
517 | * @param string | Null $action - e.g. edit |
||
518 | * |
||
519 | * @return string |
||
520 | */ |
||
521 | public function CMSEditLink($action = null) |
||
529 | |||
530 | /** |
||
531 | * tells the order to display itself with an alternative display page. |
||
532 | * in that way, orders can be displayed differently for certain steps |
||
533 | * for example, in a print step, the order can be displayed in a |
||
534 | * PRINT ONLY format. |
||
535 | * |
||
536 | * When the method return null, the order is displayed using the standard display page |
||
537 | * |
||
538 | * @see Order::DisplayPage |
||
539 | * |
||
540 | * @return null|object (Page) |
||
541 | **/ |
||
542 | public function AlternativeDisplayPage() |
||
546 | |||
547 | /** |
||
548 | * Allows the opportunity for the Order Step to add any fields to Order::getCMSFields |
||
549 | * Usually this is added before ActionNextStepManually. |
||
550 | * |
||
551 | * @param FieldList $fields |
||
552 | * @param Order $order |
||
553 | * |
||
554 | * @return FieldList |
||
555 | **/ |
||
556 | public function addOrderStepFields(FieldList $fields, Order $order) |
||
560 | |||
561 | /** |
||
562 | *@return ValidationResult |
||
563 | **/ |
||
564 | public function validate() |
||
582 | |||
583 | /************************************************** |
||
584 | * moving between statusses... |
||
585 | **************************************************/ |
||
586 | /** |
||
587 | *initStep: |
||
588 | * makes sure the step is ready to run.... (e.g. check if the order is ready to be emailed as receipt). |
||
589 | * should be able to run this function many times to check if the step is ready. |
||
590 | * |
||
591 | * @see Order::doNextStatus |
||
592 | * |
||
593 | * @param Order object |
||
594 | * |
||
595 | * @return bool - true if the current step is ready to be run... |
||
596 | **/ |
||
597 | public function initStep(Order $order) |
||
603 | |||
604 | /** |
||
605 | *doStep: |
||
606 | * should only be able to run this function once |
||
607 | * (init stops you from running it twice - in theory....) |
||
608 | * runs the actual step. |
||
609 | * |
||
610 | * @see Order::doNextStatus |
||
611 | * |
||
612 | * @param Order object |
||
613 | * |
||
614 | * @return bool - true if run correctly. |
||
615 | **/ |
||
616 | public function doStep(Order $order) |
||
622 | |||
623 | /** |
||
624 | * nextStep: |
||
625 | * returns the next step (after it checks if everything is in place for the next step to run...). |
||
626 | * |
||
627 | * @see Order::doNextStatus |
||
628 | * |
||
629 | * @param Order $order |
||
630 | * |
||
631 | * @return OrderStep | Null (next step OrderStep object) |
||
632 | **/ |
||
633 | public function nextStep(Order $order) |
||
644 | |||
645 | /************************************************** |
||
646 | * Boolean checks |
||
647 | **************************************************/ |
||
648 | |||
649 | /** |
||
650 | * Checks if a step has passed (been completed) in comparison to the current step. |
||
651 | * |
||
652 | * @param string $code: the name of the step to check |
||
653 | * @param bool $orIsEqualTo if set to true, this method will return TRUE if the step being checked is the current one |
||
654 | * |
||
655 | * @return bool |
||
656 | **/ |
||
657 | public function hasPassed($code, $orIsEqualTo = false) |
||
675 | |||
676 | /** |
||
677 | * @param string $code |
||
678 | * |
||
679 | * @return bool |
||
680 | **/ |
||
681 | public function hasPassedOrIsEqualTo($code) |
||
685 | |||
686 | /** |
||
687 | * @param string $code |
||
688 | * |
||
689 | * @return bool |
||
690 | **/ |
||
691 | public function hasNotPassed($code) |
||
695 | |||
696 | /** |
||
697 | * Opposite of hasPassed. |
||
698 | * |
||
699 | * @param string $code |
||
700 | * |
||
701 | * @return bool |
||
702 | **/ |
||
703 | public function isBefore($code) |
||
707 | |||
708 | /** |
||
709 | *@return bool |
||
710 | **/ |
||
711 | protected function isDefaultStatusOption() |
||
715 | |||
716 | /************************************************** |
||
717 | |||
718 | **************************************************/ |
||
719 | |||
720 | /** |
||
721 | * @var string |
||
722 | */ |
||
723 | protected $emailClassName = ''; |
||
724 | |||
725 | /** |
||
726 | * returns the email class used for emailing the |
||
727 | * customer during a specific step (IF ANY!). |
||
728 | * |
||
729 | * @return string |
||
730 | */ |
||
731 | public function getEmailClassName() |
||
735 | |||
736 | /** |
||
737 | * return true if done already or mailed successfully now. |
||
738 | * |
||
739 | * @param order $order |
||
740 | * @param string $subject |
||
741 | * @param string $message |
||
742 | * @param bool $resend |
||
743 | * @param bool | string $adminOnlyOrToEmail you can set to false = send to customer, true: send to admin, or email = send to email |
||
744 | * @param string $emailClassName |
||
745 | * |
||
746 | * @return boolean; |
||
747 | */ |
||
748 | protected function sendEmailForStep( |
||
794 | |||
795 | /** |
||
796 | * sets the email class used for emailing the |
||
797 | * customer during a specific step (IF ANY!). |
||
798 | * |
||
799 | * @param string |
||
800 | */ |
||
801 | public function setEmailClassName($s) |
||
805 | |||
806 | /** |
||
807 | * returns a link that can be used to test |
||
808 | * the email being sent during this step |
||
809 | * this method returns NULL if no email |
||
810 | * is being sent OR if there is no suitable Order |
||
811 | * to test with... |
||
812 | * |
||
813 | * @return string |
||
814 | */ |
||
815 | protected function testEmailLink() |
||
829 | |||
830 | /** |
||
831 | * Has an email been sent to the customer for this |
||
832 | * order step. |
||
833 | *"-10 days". |
||
834 | * |
||
835 | * @param Order $order |
||
836 | * @param bool $checkDateOfOrder |
||
837 | * |
||
838 | * @return bool |
||
839 | **/ |
||
840 | public function hasBeenSent(Order $order, $checkDateOfOrder = true) |
||
864 | |||
865 | /** |
||
866 | * For some ordersteps this returns true... |
||
867 | * |
||
868 | * @return bool |
||
869 | **/ |
||
870 | protected function hasCustomerMessage() |
||
874 | |||
875 | |||
876 | /** |
||
877 | * Formatted answer for "hasCustomerMessage". |
||
878 | * |
||
879 | * @return string |
||
880 | */ |
||
881 | public function HasCustomerMessageNice() |
||
889 | |||
890 | /** |
||
891 | * Formatted answer for "hasCustomerMessage". |
||
892 | * |
||
893 | * @return string |
||
894 | */ |
||
895 | public function ShowAsSummary() |
||
899 | |||
900 | /** |
||
901 | * |
||
902 | * |
||
903 | * @return string |
||
904 | */ |
||
905 | public function getShowAsSummary() |
||
938 | |||
939 | /** |
||
940 | * @return string |
||
941 | */ |
||
942 | protected function humanReadeableDeferTimeInSeconds() |
||
959 | |||
960 | /** |
||
961 | * Formatted answer for "hasCustomerMessage". |
||
962 | * |
||
963 | * @return string |
||
964 | */ |
||
965 | public function NameAndDescription() |
||
969 | |||
970 | public function getNameAndDescription() |
||
976 | |||
977 | /** |
||
978 | * This allows you to set the time to something other than the standard DeferTimeInSeconds |
||
979 | * value based on the order provided. |
||
980 | * |
||
981 | * @param Order |
||
982 | * |
||
983 | * @return int |
||
984 | */ |
||
985 | public function CalculatedDeferTimeInSeconds($order) |
||
989 | |||
990 | /** |
||
991 | * can this order step be delayed? |
||
992 | * in general, if there is a customer message |
||
993 | * we should be able to delay it |
||
994 | * |
||
995 | * This method can be overridden in any orderstep |
||
996 | * @return bool |
||
997 | **/ |
||
998 | protected function canBeDefered() |
||
1002 | |||
1003 | |||
1004 | /************************************************** |
||
1005 | * Order Status Logs |
||
1006 | **************************************************/ |
||
1007 | |||
1008 | /** |
||
1009 | * The OrderStatusLog that is relevant to the particular step. |
||
1010 | * |
||
1011 | * @var string |
||
1012 | */ |
||
1013 | protected $relevantLogEntryClassName = ''; |
||
1014 | |||
1015 | /** |
||
1016 | * @return string |
||
1017 | */ |
||
1018 | public function getRelevantLogEntryClassName() |
||
1022 | |||
1023 | /** |
||
1024 | * @param string |
||
1025 | */ |
||
1026 | public function setRelevantLogEntryClassName($s) |
||
1030 | |||
1031 | /** |
||
1032 | * returns the OrderStatusLog that is relevant to this step. |
||
1033 | * |
||
1034 | * @param Order $order |
||
1035 | * |
||
1036 | * @return OrderStatusLog | null |
||
1037 | */ |
||
1038 | public function RelevantLogEntry(Order $order) |
||
1044 | |||
1045 | /** |
||
1046 | * returns the OrderStatusLogs that are relevant to this step. |
||
1047 | * |
||
1048 | * @param Order $order |
||
1049 | * |
||
1050 | * @return DataObjectSet | null |
||
1051 | */ |
||
1052 | public function RelevantLogEntries(Order $order) |
||
1058 | |||
1059 | /************************************************** |
||
1060 | * Silverstripe Standard Data Object Methods |
||
1061 | **************************************************/ |
||
1062 | |||
1063 | /** |
||
1064 | * Standard SS method |
||
1065 | * These are only created programmatically. |
||
1066 | * |
||
1067 | * @param Member $member |
||
1068 | * |
||
1069 | * @return bool |
||
1070 | */ |
||
1071 | public function canCreate($member = null) |
||
1075 | |||
1076 | /** |
||
1077 | * Standard SS method. |
||
1078 | * |
||
1079 | * @param Member $member |
||
1080 | * |
||
1081 | * @return bool |
||
1082 | */ |
||
1083 | public function canView($member = null) |
||
1098 | |||
1099 | /** |
||
1100 | * the default for this is TRUE, but for completed order steps |
||
1101 | * |
||
1102 | * we do not allow this. |
||
1103 | * |
||
1104 | * @param Order $order |
||
1105 | * @param Member $member optional |
||
1106 | * @return bool |
||
1107 | */ |
||
1108 | public function canOverrideCanViewForOrder($order, $member = null) |
||
1117 | |||
1118 | /** |
||
1119 | * standard SS method. |
||
1120 | * |
||
1121 | * @param Member | NULL |
||
1122 | * |
||
1123 | * @return bool |
||
1124 | */ |
||
1125 | public function canEdit($member = null) |
||
1140 | |||
1141 | /** |
||
1142 | * Standard SS method. |
||
1143 | * |
||
1144 | * @param Member $member |
||
1145 | * |
||
1146 | * @return bool |
||
1147 | */ |
||
1148 | public function canDelete($member = null) |
||
1178 | |||
1179 | /** |
||
1180 | * standard SS method. |
||
1181 | */ |
||
1182 | public function onBeforeWrite() |
||
1211 | |||
1212 | /** |
||
1213 | * move linked orders to the next status |
||
1214 | * standard SS method. |
||
1215 | */ |
||
1216 | public function onBeforeDelete() |
||
1237 | |||
1238 | /** |
||
1239 | * standard SS method. |
||
1240 | */ |
||
1241 | public function onAfterDelete() |
||
1246 | |||
1247 | protected function NextOrderStep() |
||
1253 | |||
1254 | protected function PreviousOrderStep() |
||
1260 | |||
1261 | /** |
||
1262 | * standard SS method |
||
1263 | * USED TO BE: Unpaid,Query,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart. |
||
1264 | */ |
||
1265 | public function requireDefaultRecords() |
||
1318 | |||
1319 | /** |
||
1320 | * returns the standard EcommerceDBConfig for use within OrderSteps. |
||
1321 | * |
||
1322 | * @return EcommerceDBConfig |
||
1323 | */ |
||
1324 | protected function EcomConfig() |
||
1328 | |||
1329 | /** |
||
1330 | * Explains the current order step. |
||
1331 | * |
||
1332 | * @return string |
||
1333 | */ |
||
1334 | protected function myDescription() |
||
1338 | } |
||
1339 |
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.