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 | 'DeferTimeInSeconds' => true, |
||
23 | 'ProcessAttempts' => true |
||
24 | ); |
||
25 | |||
26 | private static $casting = array( |
||
27 | 'ToBeProcessedAt' => 'SS_Datetime', |
||
28 | 'HasBeenInQueueSince' => 'SS_Datetime' |
||
29 | ); |
||
30 | |||
31 | private static $default_sort = [ |
||
32 | 'ID' => 'DESC' |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * standard SS variable. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $summary_fields = array( |
||
41 | 'Order.Title' => 'Order', |
||
42 | 'Order.Status.Title' => 'Current Step', |
||
43 | 'ProcessAttempts' => 'Attempts', |
||
44 | 'ToBeProcessedAt.Nice' => 'To be processed at', |
||
45 | 'ToBeProcessedAt.Ago' => 'That is ...', |
||
46 | 'HasBeenInQueueForSince.Nice' => 'Added to queue ...', |
||
47 | 'InProcess.Nice' => 'Currently Running' |
||
48 | ); |
||
49 | |||
50 | /** |
||
51 | * standard SS variable. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | private static $searchable_fields = array( |
||
56 | 'OrderID' => array( |
||
57 | 'field' => 'NumericField', |
||
58 | 'title' => 'Order Number', |
||
59 | ) |
||
60 | ); |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Standard SS method. |
||
65 | * |
||
66 | * @param Member $member |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | public function canCreate($member = null) |
||
74 | |||
75 | /** |
||
76 | * Standard SS method. |
||
77 | * |
||
78 | * @param Member $member |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | public function canView($member = null) |
||
101 | |||
102 | /** |
||
103 | * Standard SS method. |
||
104 | * |
||
105 | * @param Member $member |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function canEdit($member = null) |
||
113 | |||
114 | /** |
||
115 | * Standard SS method |
||
116 | * Queues can be deleted if needed. |
||
117 | * |
||
118 | * @param Member $member |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function canDelete($member = null) |
||
126 | |||
127 | /** |
||
128 | * standard SS variable. |
||
129 | * |
||
130 | * @var string |
||
131 | */ |
||
132 | private static $singular_name = 'Order To Be Processed'; |
||
133 | public function i18n_singular_name() |
||
137 | |||
138 | /** |
||
139 | * standard SS variable. |
||
140 | * |
||
141 | * @var string |
||
142 | */ |
||
143 | private static $plural_name = 'Orders to be Processed'; |
||
144 | public function i18n_plural_name() |
||
148 | |||
149 | |||
150 | /** |
||
151 | * META METHOD: Add an order to the job list if it does not exist already. |
||
152 | * |
||
153 | * @param Order $order |
||
154 | * @param Int $deferInSeconds |
||
155 | */ |
||
156 | public function AddOrderToQueue($order, $deferTimeInSeconds) |
||
182 | |||
183 | /** |
||
184 | * META METHOD |
||
185 | * processes the order ... |
||
186 | * returns TRUE if SUCCESSFUL and a message if unsuccessful ... |
||
187 | * |
||
188 | * |
||
189 | * @param Order $order optional |
||
190 | * @return boolean | string |
||
191 | */ |
||
192 | public function process($order = null) |
||
250 | |||
251 | /** |
||
252 | * META METHOD: returns the queue object if it exists |
||
253 | * |
||
254 | * @param Order $order |
||
255 | * |
||
256 | * @return null | OrderProcessQueue |
||
257 | */ |
||
258 | public function getQueueObject($order) |
||
264 | |||
265 | /** |
||
266 | * META METHOD: Once you are done, you can remove the item like this ... |
||
267 | * |
||
268 | * @param Order $order |
||
269 | */ |
||
270 | public function removeOrderFromQueue($order) |
||
277 | |||
278 | /** |
||
279 | * META METHOD: returns a list of orders to be processed |
||
280 | * @param int $id force this Order to be processed |
||
281 | * @param int $limit total number of orders that can be retrieved at any one time |
||
282 | * |
||
283 | * @return DataList (of orders) |
||
284 | */ |
||
285 | public function OrdersToBeProcessed($id = 0, $limit = 9999) |
||
310 | |||
311 | /** |
||
312 | * META METHOD: all orders with a queue object |
||
313 | * @param int $id force this Order to be processed |
||
314 | * @param int $limit total number of orders that can be retrieved at any one time |
||
315 | * |
||
316 | * @return DataList (of orders) |
||
317 | */ |
||
318 | public function AllOrdersInQueue($limit = 9999) |
||
327 | |||
328 | /** |
||
329 | * META METHOD: returns a list of orders NOT YET to be processed |
||
330 | * @param int $limit total number of orders that can be retrieved at any one time |
||
331 | * |
||
332 | * @return DataList (of orders) |
||
333 | */ |
||
334 | public function OrdersInQueueThatAreNotReady($limit = 9999) |
||
357 | |||
358 | /** |
||
359 | * non-database method of working out if an Order is ready to go. |
||
360 | * |
||
361 | * @return bool |
||
362 | */ |
||
363 | public function isReadyToGo() |
||
367 | |||
368 | /** |
||
369 | * |
||
370 | * casted variable |
||
371 | * @return SS_DateTime |
||
372 | */ |
||
373 | public function ToBeProcessedAt() |
||
377 | |||
378 | /** |
||
379 | * |
||
380 | * casted variable |
||
381 | * @return SS_DateTime |
||
382 | */ |
||
383 | public function getToBeProcessedAt() |
||
387 | |||
388 | |||
389 | /** |
||
390 | * |
||
391 | * casted variable |
||
392 | * @return SS_DateTime |
||
393 | */ |
||
394 | public function HasBeenInQueueForSince() |
||
398 | |||
399 | /** |
||
400 | * |
||
401 | * casted variable |
||
402 | * @return SS_DateTime |
||
403 | */ |
||
404 | public function getHasBeenInQueueForSince() |
||
408 | |||
409 | |||
410 | /** |
||
411 | * CMS Fields |
||
412 | * @return FieldList |
||
413 | */ |
||
414 | public function getCMSFields() |
||
458 | |||
459 | public function requireDefaultRecords() |
||
468 | |||
469 | protected function sortPhrase() |
||
476 | |||
477 | /** |
||
478 | * sort phrase for orders, based in order IDs... |
||
479 | * @param array $orderIds |
||
480 | * @return string |
||
481 | */ |
||
482 | protected function sortPhraseForOrderIDs($orderIDs) |
||
486 | } |
||
487 |