Complex classes like OrderProcessQueue 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 OrderProcessQueue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class OrderProcessQueue extends DataObject |
||
|
|
|||
| 9 | { |
||
| 10 | private static $db = array( |
||
| 11 | 'DeferTimeInSeconds' => 'Int', |
||
| 12 | 'InProcess' => 'Boolean', |
||
| 13 | 'ProcessAttempts' => 'Int' |
||
| 14 | ); |
||
| 15 | |||
| 16 | private static $has_one = array( |
||
| 17 | 'Order' => 'Order', |
||
| 18 | 'OrderStep' => 'OrderStep' |
||
| 19 | ); |
||
| 20 | |||
| 21 | private static $indexes = array( |
||
| 22 | 'Created' => true, |
||
| 23 | 'DeferTimeInSeconds' => true, |
||
| 24 | 'ProcessAttempts' => true |
||
| 25 | ); |
||
| 26 | |||
| 27 | private static $casting = array( |
||
| 28 | 'ToBeProcessedAt' => 'SS_Datetime', |
||
| 29 | 'HasBeenInQueueSince' => 'SS_Datetime' |
||
| 30 | ); |
||
| 31 | |||
| 32 | private static $default_sort = array( |
||
| 33 | 'Created' => 'DESC' |
||
| 34 | ); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * standard SS variable. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private static $summary_fields = array( |
||
| 42 | 'Order.Title' => 'Order', |
||
| 43 | 'Order.Status.Title' => 'Current Step', |
||
| 44 | 'ProcessAttempts' => 'Attempts', |
||
| 45 | 'ToBeProcessedAt.Nice' => 'To be processed at', |
||
| 46 | 'ToBeProcessedAt.Ago' => 'That is ...', |
||
| 47 | 'HasBeenInQueueForSince.Nice' => 'Added to queue ...', |
||
| 48 | 'InProcess.Nice' => 'Currently Running' |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * standard SS variable. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | private static $searchable_fields = array( |
||
| 57 | 'OrderID' => array( |
||
| 58 | 'field' => 'NumericField', |
||
| 59 | 'title' => 'Order Number', |
||
| 60 | ) |
||
| 61 | ); |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * Standard SS method. |
||
| 66 | * |
||
| 67 | * @param Member $member |
||
| 68 | * |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | public function canCreate($member = null) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Standard SS method. |
||
| 78 | * |
||
| 79 | * @param Member $member |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public function canView($member = null) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Standard SS method. |
||
| 105 | * |
||
| 106 | * @param Member $member |
||
| 107 | * |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function canEdit($member = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Standard SS method |
||
| 117 | * Queues can be deleted if needed. |
||
| 118 | * |
||
| 119 | * @param Member $member |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function canDelete($member = null) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * standard SS variable. |
||
| 130 | * |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | private static $singular_name = 'Order To Be Processed'; |
||
| 134 | public function i18n_singular_name() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * standard SS variable. |
||
| 141 | * |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | private static $plural_name = 'Orders to be Processed'; |
||
| 145 | public function i18n_plural_name() |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * META METHOD: Add an order to the job list if it does not exist already. |
||
| 153 | * |
||
| 154 | * @param Order $order |
||
| 155 | * @param Int $deferInSeconds |
||
| 156 | */ |
||
| 157 | public function AddOrderToQueue($order, $deferTimeInSeconds) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * META METHOD |
||
| 182 | * processes the order ... |
||
| 183 | * returns TRUE if SUCCESSFUL and a message if unsuccessful ... |
||
| 184 | * |
||
| 185 | * |
||
| 186 | * @param Order $order optional |
||
| 187 | * @return boolean | string |
||
| 188 | */ |
||
| 189 | public function process($order = null) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * META METHOD: returns the queue object if it exists |
||
| 244 | * |
||
| 245 | * @param Order $order |
||
| 246 | * |
||
| 247 | * @return null | OrderProcessQueue |
||
| 248 | */ |
||
| 249 | public function getQueueObject($order) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * META METHOD: Once you are done, you can remove the item like this ... |
||
| 258 | * |
||
| 259 | * @param Order $order |
||
| 260 | */ |
||
| 261 | public function removeOrderFromQueue($order) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * META METHOD: returns a list of orders to be processed |
||
| 271 | * @param int $id force this Order to be processed |
||
| 272 | * @param int $limit total number of orders that can be retrieved at any one time |
||
| 273 | * |
||
| 274 | * @return DataList (of orders) |
||
| 275 | */ |
||
| 276 | public function OrdersToBeProcessed($id = 0, $limit = 9999) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * META METHOD: all orders with a queue object |
||
| 304 | * @param int $id force this Order to be processed |
||
| 305 | * @param int $limit total number of orders that can be retrieved at any one time |
||
| 306 | * |
||
| 307 | * @return DataList (of orders) |
||
| 308 | */ |
||
| 309 | public function AllOrdersInQueue($limit = 9999) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * META METHOD: returns a list of orders NOT YET to be processed |
||
| 321 | * @param int $limit total number of orders that can be retrieved at any one time |
||
| 322 | * |
||
| 323 | * @return DataList (of orders) |
||
| 324 | */ |
||
| 325 | public function OrdersInQueueThatAreNotReady($limit = 9999) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * non-database method of working out if an Order is ready to go. |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | public function isReadyToGo() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * |
||
| 361 | * casted variable |
||
| 362 | * @return SS_DateTime |
||
| 363 | */ |
||
| 364 | public function ToBeProcessedAt() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * |
||
| 371 | * casted variable |
||
| 372 | * @return SS_DateTime |
||
| 373 | */ |
||
| 374 | public function getToBeProcessedAt() |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * |
||
| 382 | * casted variable |
||
| 383 | * @return SS_DateTime |
||
| 384 | */ |
||
| 385 | public function HasBeenInQueueForSince() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * |
||
| 392 | * casted variable |
||
| 393 | * @return SS_DateTime |
||
| 394 | */ |
||
| 395 | public function getHasBeenInQueueForSince() |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * CMS Fields |
||
| 403 | * @return FieldList |
||
| 404 | */ |
||
| 405 | public function getCMSFields() |
||
| 449 | |||
| 450 | public function requireDefaultRecords() |
||
| 459 | |||
| 460 | protected function sortPhrase() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * sort phrase for orders, based in order IDs... |
||
| 470 | * @param array $orderIds |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | protected function sortPhraseForOrder($orderIds) |
||
| 477 | |||
| 478 | } |
||
| 479 |
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.