Complex classes like OrderAttribute 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 OrderAttribute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class OrderAttribute extends DataObject implements EditableEcommerceObject |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * what variables are accessible through http://mysite.com/api/ecommerce/v1/ShippingAddress/. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private static $api_access = array( |
||
|
|
|||
| 22 | 'view' => array( |
||
| 23 | 'CalculatedTotal', |
||
| 24 | 'Sort', |
||
| 25 | 'GroupSort', |
||
| 26 | 'TableTitle', |
||
| 27 | 'TableSubTitleNOHTML', |
||
| 28 | 'CartTitle', |
||
| 29 | 'CartSubTitle', |
||
| 30 | 'Order', |
||
| 31 | ), |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Standard SS variable. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private static $db = array( |
||
| 40 | 'CalculatedTotal' => 'Currency', |
||
| 41 | 'Sort' => 'Int', |
||
| 42 | 'GroupSort' => 'Int', |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Standard SS variable. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private static $has_one = array( |
||
| 51 | 'Order' => 'Order', |
||
| 52 | ); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Standard SS variable. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private static $casting = array( |
||
| 60 | 'TableTitle' => 'HTMLText', |
||
| 61 | 'TableSubTitle' => 'HTMLText', |
||
| 62 | 'TableSubTitleNOHTML' => 'Text', |
||
| 63 | 'CartTitle' => 'HTMLText', |
||
| 64 | 'CartSubTitle' => 'HTMLText', |
||
| 65 | 'CalculatedTotalAsMoney' => 'Money', |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Standard SS variable. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | **/ |
||
| 73 | private static $default_sort = [ |
||
| 74 | 'OrderAttribute.GroupSort' => 'ASC', |
||
| 75 | 'OrderAttribute.Sort' => 'ASC', |
||
| 76 | 'OrderAttribute.ID' => 'ASC' |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Standard SS variable. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private static $indexes = array( |
||
| 85 | 'GroupSort' => true, |
||
| 86 | 'Sort' => true, |
||
| 87 | 'ID' => true |
||
| 88 | ); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Standard SS variable. |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | private static $singular_name = 'Order Entry'; |
||
| 96 | public function i18n_singular_name() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Standard SS variable. |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | private static $plural_name = 'Order Extra Descriptions'; |
||
| 107 | public function i18n_plural_name() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Standard SS variable. |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private static $description = 'Any item that is added to the order - be it before (e.g. product) or after the subtotal (e.g. tax).'; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * save edit status for speed's sake. |
||
| 121 | * |
||
| 122 | * @var bool |
||
| 123 | */ |
||
| 124 | protected $_canEdit = null; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * save view status for speed's sake. |
||
| 128 | * |
||
| 129 | * @var bool |
||
| 130 | */ |
||
| 131 | protected $_canView = null; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * we use this variable to make sure that the parent::runUpdate() is called in all child classes |
||
| 135 | * this is similar to the checks run for parent::init in the controller class. |
||
| 136 | * |
||
| 137 | * @var bool |
||
| 138 | **/ |
||
| 139 | protected $baseInitCalled = false; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * extended in OrderModifier and OrderItem |
||
| 143 | * Starts up the order Atribute |
||
| 144 | * TODO: introduce system like we have for Controller |
||
| 145 | * which makes sure that all parent init methods are called. |
||
| 146 | */ |
||
| 147 | public function init() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * standard SS method. |
||
| 154 | * |
||
| 155 | * @param Member $member |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | **/ |
||
| 159 | public function canCreate($member = null) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Standard SS method |
||
| 177 | * This is an important method. |
||
| 178 | * |
||
| 179 | * @param Member $member |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | **/ |
||
| 183 | public function canView($member = null) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Standard SS method |
||
| 213 | * This is an important method. |
||
| 214 | * |
||
| 215 | * @param Member $member |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | **/ |
||
| 219 | public function canEdit($member = null) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Standard SS method. |
||
| 240 | * |
||
| 241 | * @param Member $member |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | **/ |
||
| 245 | public function canDelete($member = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * link to edit the record. |
||
| 252 | * |
||
| 253 | * @param string | Null $action - e.g. edit |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function CMSEditLink($action = null) |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * Helps in speeding up code. |
||
| 265 | * This can be a static variable as it is the same for all OrderItems for an Order. |
||
| 266 | * |
||
| 267 | * @var array |
||
| 268 | */ |
||
| 269 | private static $_price_has_been_fixed = array(); |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @param int $orderID |
||
| 274 | * @param bool $value |
||
| 275 | */ |
||
| 276 | public static function set_price_has_been_fixed($orderID = 0, $value = false) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param int $orderID |
||
| 284 | * @return bool|null |
||
| 285 | */ |
||
| 286 | public static function get_price_has_been_fixed($orderID = 0) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @description - tells you if an order item price has been "fixed" |
||
| 295 | * meaning that is has been saved in the CalculatedTotal field so that |
||
| 296 | * it can not be altered. |
||
| 297 | * |
||
| 298 | * Default returns false; this is good for uncompleted orders |
||
| 299 | * but not so good for completed ones. |
||
| 300 | * |
||
| 301 | * We use direct calls to self::$_price_has_been_fixed to make the code simpler and faster. |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | **/ |
||
| 305 | protected function priceHasBeenFixed($recalculate = false) |
||
| 321 | |||
| 322 | ###################### |
||
| 323 | ## TEMPLATE METHODS ## |
||
| 324 | ###################### |
||
| 325 | |||
| 326 | /** |
||
| 327 | * This is a key function that returns the type of the |
||
| 328 | * object. In principle anything can be returned |
||
| 329 | * but the intention is to only return a few options |
||
| 330 | * e.g. OrderItem, Tax, Delivery, etc... so that |
||
| 331 | * computations can be carried out based on the type of |
||
| 332 | * OrderAttribute we are looking at. |
||
| 333 | * It also allows to get a group of Order Attributes that |
||
| 334 | * contains both modifiers and orderItems. |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function OrderAttributeType() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * returns the order - for some unknown reason it seems we need this. |
||
| 345 | * |
||
| 346 | * @return Order | null |
||
| 347 | */ |
||
| 348 | public function Order() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Return a string of class names, in order |
||
| 355 | * of hierarchy from OrderAttribute for the |
||
| 356 | * current attribute. |
||
| 357 | * |
||
| 358 | * e.g.: "product_orderitem orderitem |
||
| 359 | * orderattribute". |
||
| 360 | * |
||
| 361 | * Used by the templates and for ajax updating functionality. |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function Classes() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * returns the instance of EcommerceConfigAjax for use in templates. |
||
| 382 | * In templates, it is used like this: |
||
| 383 | * $EcommerceConfigAjax.TableID. |
||
| 384 | * |
||
| 385 | * @return EcommerceConfigAjax |
||
| 386 | **/ |
||
| 387 | public function AJAXDefinitions() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * returns the instance of EcommerceDBConfig. |
||
| 394 | * |
||
| 395 | * @return EcommerceDBConfig |
||
| 396 | **/ |
||
| 397 | public function EcomConfig() |
||
| 401 | |||
| 402 | /* |
||
| 403 | * Should this item be shown on check out page table? |
||
| 404 | * @return bool |
||
| 405 | **/ |
||
| 406 | public function ShowInTable() |
||
| 410 | |||
| 411 | /** |
||
| 412 | *Should this item be shown on in the cart (which is on other pages than the checkout page). |
||
| 413 | * |
||
| 414 | *@return bool |
||
| 415 | **/ |
||
| 416 | public function ShowInCart() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Return a name of what this attribute is |
||
| 423 | * called e.g. "Product 21" or "Discount". |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function TableTitle() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Return a name of what this attribute is |
||
| 438 | * called e.g. "Product 21" or "Discount" |
||
| 439 | * Cart is a short version of table. |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function CartTitle() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * the sub title for the order item or order modifier. |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | **/ |
||
| 457 | public function TableSubTitle() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * the sub title for the order item or order modifier. |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | **/ |
||
| 471 | public function TableSubTitleNOHTML() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * the sub title for the order item or order modifier. |
||
| 482 | * Cart is a short version of table. |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | **/ |
||
| 486 | public function CartSubTitle() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Returns the Money object of the CalculatedTotal. |
||
| 497 | * |
||
| 498 | * @return Money |
||
| 499 | **/ |
||
| 500 | public function CalculatedTotalAsMoney() |
||
| 508 | |||
| 509 | public function runUpdate($force = false) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Standard SS method |
||
| 516 | * We add the Sort value from the OrderAttribute_Group to the OrderAttribute. |
||
| 517 | */ |
||
| 518 | public function onBeforeWrite() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Standard SS method. |
||
| 530 | */ |
||
| 531 | public function onAfterWrite() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Debug helper method. |
||
| 540 | * Access through : /shoppingcart/debug/. |
||
| 541 | */ |
||
| 542 | public function debug() |
||
| 548 | } |
||
| 549 |