Complex classes like Order 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 Order, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Order extends DataObject |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Status codes and what they mean: |
||
| 38 | * |
||
| 39 | * Unpaid (default): Order created but no successful payment by customer yet |
||
| 40 | * Query: Order not being processed yet (customer has a query, or could be out of stock) |
||
| 41 | * Paid: Order successfully paid for by customer |
||
| 42 | * Processing: Order paid for, package is currently being processed before shipping to customer |
||
| 43 | * Sent: Order paid for, processed for shipping, and now sent to the customer |
||
| 44 | * Complete: Order completed (paid and shipped). Customer assumed to have received their goods |
||
| 45 | * AdminCancelled: Order cancelled by the administrator |
||
| 46 | * MemberCancelled: Order cancelled by the customer (Member) |
||
| 47 | */ |
||
| 48 | private static $db = array( |
||
| 49 | 'Total' => 'Currency', |
||
| 50 | 'Reference' => 'Varchar', //allow for customised order numbering schemes |
||
| 51 | //status |
||
| 52 | 'Placed' => "SS_Datetime", //date the order was placed (went from Cart to Order) |
||
| 53 | 'Paid' => 'SS_Datetime', //no outstanding payment left |
||
| 54 | 'ReceiptSent' => 'SS_Datetime', //receipt emailed to customer |
||
| 55 | 'Printed' => 'SS_Datetime', |
||
| 56 | 'Dispatched' => 'SS_Datetime', //products have been sent to customer |
||
| 57 | 'Status' => "Enum('Unpaid,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart','Cart')", |
||
| 58 | //customer (for guest orders) |
||
| 59 | 'FirstName' => 'Varchar', |
||
| 60 | 'Surname' => 'Varchar', |
||
| 61 | 'Email' => 'Varchar', |
||
| 62 | 'Notes' => 'Text', |
||
| 63 | 'IPAddress' => 'Varchar(15)', |
||
| 64 | //separate shipping |
||
| 65 | 'SeparateBillingAddress' => 'Boolean', |
||
| 66 | // keep track of customer locale |
||
| 67 | 'Locale' => 'DBLocale', |
||
| 68 | ); |
||
| 69 | |||
| 70 | private static $has_one = array( |
||
| 71 | 'Member' => 'Member', |
||
| 72 | 'ShippingAddress' => 'Address', |
||
| 73 | 'BillingAddress' => 'Address', |
||
| 74 | ); |
||
| 75 | |||
| 76 | private static $has_many = array( |
||
| 77 | 'Items' => 'OrderItem', |
||
| 78 | 'Modifiers' => 'OrderModifier', |
||
| 79 | 'OrderStatusLogs' => 'OrderStatusLog', |
||
| 80 | ); |
||
| 81 | |||
| 82 | private static $defaults = array( |
||
| 83 | 'Status' => 'Cart', |
||
| 84 | ); |
||
| 85 | |||
| 86 | private static $casting = array( |
||
| 87 | 'FullBillingAddress' => 'Text', |
||
| 88 | 'FullShippingAddress' => 'Text', |
||
| 89 | 'Total' => 'Currency', |
||
| 90 | 'SubTotal' => 'Currency', |
||
| 91 | 'TotalPaid' => 'Currency', |
||
| 92 | 'Shipping' => 'Currency', |
||
| 93 | 'TotalOutstanding' => 'Currency', |
||
| 94 | ); |
||
| 95 | |||
| 96 | private static $summary_fields = array( |
||
| 97 | 'Reference' => 'Order No', |
||
| 98 | 'Placed' => 'Date', |
||
| 99 | 'Name' => 'Customer', |
||
| 100 | 'LatestEmail' => 'Email', |
||
| 101 | 'Total' => 'Total', |
||
| 102 | 'Status' => 'Status', |
||
| 103 | ); |
||
| 104 | |||
| 105 | private static $searchable_fields = array( |
||
| 106 | 'Reference' => array(), |
||
| 107 | 'FirstName' => array( |
||
| 108 | 'title' => 'Customer Name', |
||
| 109 | ), |
||
| 110 | 'Email' => array( |
||
| 111 | 'title' => 'Customer Email', |
||
| 112 | ), |
||
| 113 | 'Status' => array( |
||
| 114 | 'filter' => 'ExactMatchFilter', |
||
| 115 | 'field' => 'CheckboxSetField', |
||
| 116 | ), |
||
| 117 | ); |
||
| 118 | |||
| 119 | private static $singular_name = "Order"; |
||
| 120 | |||
| 121 | private static $plural_name = "Orders"; |
||
| 122 | |||
| 123 | private static $default_sort = "\"Placed\" DESC, \"Created\" DESC"; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Statuses for orders that have been placed. |
||
| 127 | */ |
||
| 128 | private static $placed_status = array( |
||
| 129 | 'Paid', |
||
| 130 | 'Unpaid', |
||
| 131 | 'Processing', |
||
| 132 | 'Sent', |
||
| 133 | 'Complete', |
||
| 134 | 'MemberCancelled', |
||
| 135 | 'AdminCancelled', |
||
| 136 | ); |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Statuses for which an order can be paid for |
||
| 140 | */ |
||
| 141 | private static $payable_status = array( |
||
| 142 | 'Cart', |
||
| 143 | 'Unpaid', |
||
| 144 | 'Processing', |
||
| 145 | 'Sent', |
||
| 146 | ); |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Statuses that shouldn't show in user account. |
||
| 150 | */ |
||
| 151 | private static $hidden_status = array('Cart'); |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Status for logging changes |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | private static $log_status = array(); |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Flags to determine when an order can be cancelled. |
||
| 162 | */ |
||
| 163 | private static $cancel_before_payment = true; |
||
| 164 | |||
| 165 | private static $cancel_before_processing = false; |
||
| 166 | |||
| 167 | private static $cancel_before_sending = false; |
||
| 168 | |||
| 169 | private static $cancel_after_sending = false; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Place an order before payment processing begins |
||
| 173 | * |
||
| 174 | * @var boolean |
||
| 175 | */ |
||
| 176 | private static $place_before_payment = false; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Modifiers represent the additional charges or |
||
| 180 | * deductions associated to an order, such as |
||
| 181 | * shipping, taxes, vouchers etc. |
||
| 182 | */ |
||
| 183 | private static $modifiers = array(); |
||
| 184 | |||
| 185 | private static $rounding_precision = 2; |
||
| 186 | |||
| 187 | private static $reference_id_padding = 5; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var boolean Will allow completion of orders with GrandTotal=0, |
||
| 191 | * which could be the case for orders paid with loyalty points or vouchers. |
||
| 192 | * Will send the "Paid" date on the order, even though no actual payment was taken. |
||
| 193 | * Will trigger the payment related extension points: |
||
| 194 | * Order->onPayment, OrderItem->onPayment, Order->onPaid. |
||
| 195 | */ |
||
| 196 | private static $allow_zero_order_total = false; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * A flag indicating that an order-status-log entry should be written |
||
| 200 | * @var bool |
||
| 201 | */ |
||
| 202 | protected $flagOrderStatusWrite = false; |
||
| 203 | |||
| 204 | 3 | public static function get_order_status_options() |
|
| 205 | { |
||
| 206 | $values = array(); |
||
| 207 | 3 | foreach (singleton('Order')->dbObject('Status')->enumValues(false) as $value) { |
|
| 208 | $values[$value] = _t('Order.STATUS_' . strtoupper($value), $value); |
||
| 209 | } |
||
| 210 | return $values; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Create CMS fields for cms viewing and editing orders |
||
| 215 | */ |
||
| 216 | public function getCMSFields() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Adjust scafolded search context |
||
| 243 | * |
||
| 244 | * @return SearchContext the updated search context |
||
| 245 | */ |
||
| 246 | public function getDefaultSearchContext() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Hack for swapping out relation list with OrderItemList |
||
| 301 | */ |
||
| 302 | 45 | public function getComponents($componentName, $filter = "", $sort = "", $join = "", $limit = null) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Returns the subtotal of the items for this order. |
||
| 319 | */ |
||
| 320 | 33 | public function SubTotal() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Calculate the total |
||
| 331 | * |
||
| 332 | * @return the final total |
||
| 333 | */ |
||
| 334 | 26 | public function calculate() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * This is needed to maintain backwards compatiability with |
||
| 345 | * some subsystems using modifiers. eg discounts |
||
| 346 | */ |
||
| 347 | public function getModifier($className, $forcecreate = false) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Enforce rounding precision when setting total |
||
| 355 | */ |
||
| 356 | 78 | public function setTotal($val) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Get final value of order. |
||
| 363 | * Retrieves value from DataObject's record array. |
||
| 364 | */ |
||
| 365 | 22 | public function Total() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Alias for Total. |
||
| 372 | */ |
||
| 373 | 20 | public function GrandTotal() |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Calculate how much is left to be paid on the order. |
||
| 380 | * Enforces rounding precision. |
||
| 381 | * |
||
| 382 | * Payments that have been authorized via a non-manual gateway should count towards the total paid amount. |
||
| 383 | * However, it's possible to exclude these by setting the $includeAuthorized parameter to false, which is |
||
| 384 | * useful to determine the status of the Order. Order status should only change to 'Paid' when all |
||
| 385 | * payments are 'Captured'. |
||
| 386 | * |
||
| 387 | * @param bool $includeAuthorized whether or not to include authorized payments (excluding manual payments) |
||
| 388 | * @return float |
||
| 389 | */ |
||
| 390 | 18 | public function TotalOutstanding($includeAuthorized = true) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Get the order status. This will return a localized value if available. |
||
| 400 | * |
||
| 401 | * @return string the payment status |
||
| 402 | */ |
||
| 403 | 5 | public function getStatusI18N() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Get the link for finishing order processing. |
||
| 410 | */ |
||
| 411 | 3 | public function Link() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Returns TRUE if the order can be cancelled |
||
| 421 | * PRECONDITION: Order is in the DB. |
||
| 422 | * |
||
| 423 | * @return boolean |
||
| 424 | */ |
||
| 425 | 4 | public function canCancel($member = null) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Check if an order can be paid for. |
||
| 448 | * |
||
| 449 | * @return boolean |
||
| 450 | */ |
||
| 451 | 7 | public function canPay($member = null) |
|
| 466 | |||
| 467 | /* |
||
| 468 | * Prevent deleting orders. |
||
| 469 | * @return boolean |
||
| 470 | */ |
||
| 471 | 1 | public function canDelete($member = null) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Check if an order can be viewed. |
||
| 483 | * |
||
| 484 | * @return boolean |
||
| 485 | */ |
||
| 486 | public function canView($member = null) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Check if an order can be edited. |
||
| 498 | * |
||
| 499 | * @return boolean |
||
| 500 | */ |
||
| 501 | 1 | public function canEdit($member = null) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Prevent standard creation of orders. |
||
| 513 | * |
||
| 514 | * @return boolean |
||
| 515 | */ |
||
| 516 | 1 | public function canCreate($member = null, $context = array()) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Return the currency of this order. |
||
| 528 | * Note: this is a fixed value across the entire site. |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | 8 | public function Currency() |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Get the latest email for this order.z |
||
| 539 | */ |
||
| 540 | 10 | public function getLatestEmail() |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Gets the name of the customer. |
||
| 550 | */ |
||
| 551 | public function getName() |
||
| 557 | |||
| 558 | public function getTitle() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Get shipping address, or member default shipping address. |
||
| 565 | */ |
||
| 566 | 11 | public function getShippingAddress() |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Get billing address, if marked to use seperate address, otherwise use shipping address, |
||
| 573 | * or the member default billing address. |
||
| 574 | */ |
||
| 575 | 9 | public function getBillingAddress() |
|
| 583 | |||
| 584 | /** |
||
| 585 | * @param string $type - Billing or Shipping |
||
| 586 | * @return Address |
||
| 587 | * @throws Exception |
||
| 588 | */ |
||
| 589 | 11 | protected function getAddress($type) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Check if the two addresses saved differ. |
||
| 614 | * |
||
| 615 | * @return boolean |
||
| 616 | */ |
||
| 617 | public function getAddressesDiffer() |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Has this order been sent to the customer? |
||
| 624 | * (at "Sent" status). |
||
| 625 | * |
||
| 626 | * @return boolean |
||
| 627 | */ |
||
| 628 | public function IsSent() |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Is this order currently being processed? |
||
| 635 | * (at "Sent" OR "Processing" status). |
||
| 636 | * |
||
| 637 | * @return boolean |
||
| 638 | */ |
||
| 639 | public function IsProcessing() |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Return whether this Order has been paid for (Status == Paid) |
||
| 646 | * or Status == Processing, where it's been paid for, but is |
||
| 647 | * currently in a processing state. |
||
| 648 | * |
||
| 649 | * @return boolean |
||
| 650 | */ |
||
| 651 | public function IsPaid() |
||
| 655 | |||
| 656 | public function IsCart() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Create a unique reference identifier string for this order. |
||
| 663 | */ |
||
| 664 | public function generateReference() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Get the reference for this order, or fall back to order ID. |
||
| 680 | */ |
||
| 681 | public function getReference() |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Force creating an order reference |
||
| 688 | */ |
||
| 689 | protected function onBeforeWrite() |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Called from @see onBeforeWrite whenever status changes |
||
| 713 | * @param string $fromStatus status to transition away from |
||
| 714 | * @param string $toStatus target status |
||
| 715 | */ |
||
| 716 | protected function statusTransition($fromStatus, $toStatus) |
||
| 740 | |||
| 741 | /** |
||
| 742 | * delete attributes, statuslogs, and payments |
||
| 743 | */ |
||
| 744 | protected function onBeforeDelete() |
||
| 764 | |||
| 765 | public function onAfterWrite() |
||
| 789 | |||
| 790 | public function debug() |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Provide i18n entities for the order class |
||
| 814 | * |
||
| 815 | * @return array |
||
| 816 | */ |
||
| 817 | public function provideI18nEntities() |
||
| 832 | } |
||
| 833 |
Too many fields generally indicate a class which does too much and does not follow the single responsibility principle.
We suggest taking a look at the “Code” section for further suggestions on how to fix this.