Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like RepeatOrder 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 RepeatOrder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class RepeatOrder extends DataObject |
||
| 8 | { |
||
| 9 | |||
| 10 | |||
| 11 | ####################### |
||
| 12 | ### Names Section |
||
| 13 | ####################### |
||
| 14 | |||
| 15 | private static $singular_name = 'Repeat Order'; |
||
|
|
|||
| 16 | |||
| 17 | public function i18n_singular_name() |
||
| 21 | |||
| 22 | private static $plural_name = 'Repeat Orders'; |
||
| 23 | |||
| 24 | public function i18n_plural_name() |
||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * Minimum of days in the future that the order is lodged. |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | private static $minimum_days_in_the_future = 1; |
||
| 36 | |||
| 37 | |||
| 38 | private static $allow_non_members = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Standard SS variable |
||
| 42 | */ |
||
| 43 | private static $db = [ |
||
| 44 | 'Status' => 'Enum(\'Pending, Active, MemberCancelled, AdminCancelled, Finished\', \'Pending\')', |
||
| 45 | //dates |
||
| 46 | 'Start' => 'Date', |
||
| 47 | 'End' => 'Date', |
||
| 48 | 'Period' => 'Varchar', |
||
| 49 | //payment |
||
| 50 | 'PaymentMethod' => 'Varchar', |
||
| 51 | 'CreditCardOnFile' => 'Boolean', |
||
| 52 | 'PaymentNote' => 'Text', |
||
| 53 | //computed values and notes |
||
| 54 | 'ItemsForSearchPurposes' => 'Text', //FOR SEARCH PURPOSES ONLY! |
||
| 55 | 'Notes' => 'Text' |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Standard SS variable |
||
| 60 | */ |
||
| 61 | private static $has_one = [ |
||
| 62 | 'Member' => 'Member', |
||
| 63 | 'OriginatingOrder' => 'Order' |
||
| 64 | ]; |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Standard SS variable |
||
| 69 | */ |
||
| 70 | private static $has_many = [ |
||
| 71 | 'OrderItems' => 'RepeatOrder_OrderItem', //products & quanitites |
||
| 72 | 'Orders' => 'Order' |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Standard SS variable. |
||
| 77 | */ |
||
| 78 | private static $indexes = [ |
||
| 79 | 'Status' => true, |
||
| 80 | 'Start' => true, |
||
| 81 | 'End' => true, |
||
| 82 | 'Period' => true |
||
| 83 | ]; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Standard SS variable |
||
| 87 | */ |
||
| 88 | private static $casting = [ |
||
| 89 | 'OrderItemList' => 'Text', |
||
| 90 | 'FirstOrderDate' => 'Date', |
||
| 91 | 'LastOrderDate' => 'Date', |
||
| 92 | 'TodaysOrderDate' => 'Date', |
||
| 93 | 'NextOrderDate' => 'Date', |
||
| 94 | 'FinalOrderDate' => 'Date', |
||
| 95 | 'DeliverySchedule' => 'Text', |
||
| 96 | 'HasFutureOrders' => 'Boolean' |
||
| 97 | ]; |
||
| 98 | |||
| 99 | ####################### |
||
| 100 | ### Field Names and Presentation Section |
||
| 101 | ####################### |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Standard SS variable |
||
| 105 | */ |
||
| 106 | private static $searchable_fields = [ |
||
| 107 | 'Status' => 'ExactMatchFilter', |
||
| 108 | 'Period' => 'PartialMatchFilter', |
||
| 109 | //payment |
||
| 110 | 'PaymentMethod' => 'PartialMatchFilter', |
||
| 111 | 'CreditCardOnFile' => 'ExactMatchFilter', |
||
| 112 | 'PaymentNote' => 'PartialMatchFilter', |
||
| 113 | //computed values and notes |
||
| 114 | 'Notes' => 'PartialMatchFilter', |
||
| 115 | 'ItemsForSearchPurposes' => 'PartialMatchFilter', |
||
| 116 | ]; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Standard SS variable |
||
| 120 | */ |
||
| 121 | private static $summary_fields = [ |
||
| 122 | 'ID' => 'Repeat Order ID', |
||
| 123 | 'Member.Surname' => 'Surname', |
||
| 124 | 'Member.Email' => 'Email', |
||
| 125 | 'HasFutureOrders.Nice' => 'Future Orders', |
||
| 126 | 'OrderItemList' => 'Order Item List', |
||
| 127 | 'Start' => 'Start', |
||
| 128 | 'End' => 'End', |
||
| 129 | 'Period' => 'Period', |
||
| 130 | 'Status' => 'Status' |
||
| 131 | ]; |
||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | private static $field_labels = [ |
||
| 136 | 'Status' => 'Status', |
||
| 137 | //dates |
||
| 138 | 'Start' => 'Start Date', |
||
| 139 | 'End' => 'End Date', |
||
| 140 | 'Period' => 'Repeat Schedule', |
||
| 141 | //payment |
||
| 142 | 'PaymentMethod' => 'Payment Type', |
||
| 143 | 'CreditCardOnFile' => 'Credit Card on File', |
||
| 144 | 'PaymentNote' => 'Payment Notes', |
||
| 145 | 'Notes' => 'Notes' |
||
| 146 | ]; |
||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Standard SS variable |
||
| 152 | */ |
||
| 153 | private static $default_sort = 'Created DESC'; |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Dropdown options for Period |
||
| 158 | * @var array 'strtotime period' > 'nice name' |
||
| 159 | */ |
||
| 160 | private static $period_fields = [ |
||
| 161 | '1 day' => 'Daily', |
||
| 162 | '1 week' => 'Weekly', |
||
| 163 | '2 weeks' => 'Fornightly', |
||
| 164 | '1 month' => 'Monthly' |
||
| 165 | ]; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public static function default_period_key() |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * @var array |
||
| 184 | */ |
||
| 185 | private static $status_nice = array( |
||
| 186 | 'Pending' => 'Pending', |
||
| 187 | 'Active' => 'Active', |
||
| 188 | 'MemberCancelled' => 'Pending Cancellation', |
||
| 189 | 'AdminCancelled' => 'Cancelled', |
||
| 190 | 'Finished' => 'Finished', |
||
| 191 | ); |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @var array |
||
| 196 | */ |
||
| 197 | protected static $payment_methods = array( |
||
| 198 | 'DirectCreditPayment' => 'Direct Credit (payment into bank account)' |
||
| 199 | ); |
||
| 200 | |||
| 201 | /** |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public static function default_payment_method_key() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Link for viewing |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function Link() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Link for editing |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function ModifyLink() |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Link for cancelling |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function CancelLink() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Link for end of view / edit / cancel session |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function DoneLink() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * returns a list of actual orders that have been created from this repeat order. |
||
| 265 | * @return ArrayList|null |
||
| 266 | */ |
||
| 267 | public function AutomaticallyCreatedOrders() |
||
| 280 | |||
| 281 | //=================================================== |
||
| 282 | //=================================================== |
||
| 283 | //=================================================== |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Create due draft orders |
||
| 287 | */ |
||
| 288 | public static function create_automatically_created_orders() |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * adds the orders that |
||
| 303 | */ |
||
| 304 | public function addAutomaticallyCreatedOrders() |
||
| 339 | |||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * creates order from repeatorder for a specific day. |
||
| 344 | * IF it does not already exists. |
||
| 345 | * |
||
| 346 | */ |
||
| 347 | protected function createOrderFromRepeatOrder($orderDateInteger) |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * Create a RepeatOrder from a regular Order and its Order Items |
||
| 388 | * @param Order $order |
||
| 389 | * @return RepeatOrder |
||
| 390 | */ |
||
| 391 | public static function create_repeat_order_from_order(Order $order) |
||
| 419 | |||
| 420 | |||
| 421 | |||
| 422 | |||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function TablePaymentMethod() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function TableStatus() |
||
| 448 | |||
| 449 | |||
| 450 | //========================================================================================================================================================== |
||
| 451 | |||
| 452 | |||
| 453 | |||
| 454 | public function getCMSFields() |
||
| 540 | |||
| 541 | |||
| 542 | //============ |
||
| 543 | //============ |
||
| 544 | //============ |
||
| 545 | |||
| 546 | public function onBeforeWrite() |
||
| 551 | |||
| 552 | |||
| 553 | //============ |
||
| 554 | //============ |
||
| 555 | //============ |
||
| 556 | |||
| 557 | /** |
||
| 558 | * List of products |
||
| 559 | * |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | public function OrderItemList() |
||
| 566 | /** |
||
| 567 | * |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function getOrderItemList() |
||
| 586 | |||
| 587 | //=========================================================================================================================================================================================== |
||
| 588 | |||
| 589 | /** |
||
| 590 | * The first order date |
||
| 591 | * |
||
| 592 | * @return Date|null |
||
| 593 | */ |
||
| 594 | public function FirstOrderDate() |
||
| 598 | |||
| 599 | View Code Duplication | public function getFirstOrderDate() |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Last date that an order was placed |
||
| 611 | * |
||
| 612 | * @return Date|null |
||
| 613 | */ |
||
| 614 | public function LastOrderDate() |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Last date that an order was placed |
||
| 621 | * |
||
| 622 | * @return Date|null |
||
| 623 | */ |
||
| 624 | public function getLastOrderDate() |
||
| 639 | |||
| 640 | /** |
||
| 641 | * today's' date for the order - if ANY! |
||
| 642 | * |
||
| 643 | * @return Date|null |
||
| 644 | */ |
||
| 645 | public function TodaysOrderDate() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * today's' date for the order - if ANY! |
||
| 652 | * |
||
| 653 | * @return Date|null |
||
| 654 | */ |
||
| 655 | View Code Duplication | public function getTodaysOrderDate() |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Next date (from the viewpoint of today) |
||
| 670 | * |
||
| 671 | * @return Date|null |
||
| 672 | */ |
||
| 673 | public function NextOrderDate() |
||
| 677 | |||
| 678 | View Code Duplication | public function getNextOrderDate() |
|
| 690 | |||
| 691 | |||
| 692 | /** |
||
| 693 | * Last Delivery Date |
||
| 694 | * |
||
| 695 | * @return Date|null |
||
| 696 | */ |
||
| 697 | public function FinalOrderDate() |
||
| 701 | |||
| 702 | View Code Duplication | public function getFinalOrderDate() |
|
| 714 | |||
| 715 | /** |
||
| 716 | * List of delivery dates |
||
| 717 | * |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | public function DeliverySchedule() |
||
| 724 | |||
| 725 | public function getDeliverySchedule() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * @var Array |
||
| 735 | */ |
||
| 736 | private static $_schedule = []; |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Work out the delivery schedule |
||
| 740 | * @return Array |
||
| 741 | */ |
||
| 742 | protected function workOutSchedule() |
||
| 767 | |||
| 768 | |||
| 769 | /** |
||
| 770 | * Are there any orders scheduled for the future |
||
| 771 | * @return bool |
||
| 772 | */ |
||
| 773 | public function HasFutureOrders() |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Are there any orders scheduled for the future |
||
| 780 | * @return bool |
||
| 781 | */ |
||
| 782 | public function getHasFutureOrders() |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Are there any orders scheduled for today |
||
| 793 | * @return bool |
||
| 794 | */ |
||
| 795 | public function HasAnOrderToday() |
||
| 802 | |||
| 803 | //=========================================================================================================================================================================================== |
||
| 804 | |||
| 805 | public function canView($member = null) |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Can it be edited, alias for canEdit |
||
| 825 | * |
||
| 826 | * @return bool |
||
| 827 | */ |
||
| 828 | public function canModify($member = null) |
||
| 838 | |||
| 839 | public function canEdit($member = null) |
||
| 856 | |||
| 857 | public function canDelete($member = null) |
||
| 867 | } |
||
| 868 |