Complex classes like AbstractQueueManager 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 AbstractQueueManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | abstract class AbstractQueueManager implements |
||
46 | QueueManagerInterface, |
||
47 | LoggerAwareInterface |
||
48 | { |
||
49 | use LoggerAwareTrait; |
||
50 | |||
51 | /** |
||
52 | * The queue processing rate (throttle), in items per second. |
||
53 | * |
||
54 | * @var integer |
||
55 | */ |
||
56 | private $rate = 0; |
||
57 | |||
58 | /** |
||
59 | * The batch limit. |
||
60 | * |
||
61 | * @var integer |
||
62 | */ |
||
63 | private $limit = 0; |
||
64 | |||
65 | /** |
||
66 | * The chunk size to batch the queue with. |
||
67 | * |
||
68 | * @var integer |
||
69 | */ |
||
70 | private $chunkSize = 0; |
||
71 | |||
72 | /** |
||
73 | * The queue ID. |
||
74 | * |
||
75 | * If set, then it will load only the items from this queue. |
||
76 | * If NULL, load *all* queued items. |
||
77 | * |
||
78 | * @var mixed |
||
79 | */ |
||
80 | private $queueId; |
||
81 | |||
82 | /** |
||
83 | * Items that were successfully processed |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | private $successItems = []; |
||
88 | |||
89 | /** |
||
90 | * Item that failed to process |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | private $failedItems = []; |
||
95 | |||
96 | /** |
||
97 | * Items that were skipped during the processing |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | private $skippedItems = []; |
||
102 | |||
103 | /** |
||
104 | * The callback routine when an item is processed (whether resolved or rejected). |
||
105 | * |
||
106 | * @var callable $itemCallback |
||
107 | */ |
||
108 | private $itemCallback; |
||
109 | |||
110 | /** |
||
111 | * The callback routine when the item is resolved. |
||
112 | * |
||
113 | * @var callable $itemSuccessCallback |
||
114 | */ |
||
115 | private $itemSuccessCallback; |
||
116 | |||
117 | /** |
||
118 | * The callback routine when the item is rejected. |
||
119 | * |
||
120 | * @var callable $itemFailureCallback |
||
121 | */ |
||
122 | private $itemFailureCallback; |
||
123 | |||
124 | /** |
||
125 | * The callback routine when the queue is processed. |
||
126 | * |
||
127 | * @var callable $processedCallback |
||
128 | */ |
||
129 | private $processedCallback; |
||
130 | |||
131 | /** |
||
132 | * @var FactoryInterface $queueItemFactory |
||
133 | */ |
||
134 | private $queueItemFactory; |
||
135 | |||
136 | /** |
||
137 | * Construct new queue manager. |
||
138 | * |
||
139 | * @param array $data Dependencies and settings. |
||
140 | */ |
||
141 | public function __construct(array $data = []) |
||
158 | |||
159 | /** |
||
160 | * Set the manager's data. |
||
161 | * |
||
162 | * @param array $data The queue data to set. |
||
163 | * @return AbstractQueueManager Chainable |
||
164 | */ |
||
165 | public function setData(array $data) |
||
173 | |||
174 | /** |
||
175 | * Set the queue's ID. |
||
176 | * |
||
177 | * @param mixed $id The unique queue identifier. |
||
178 | * @return self |
||
179 | */ |
||
180 | public function setQueueId($id) |
||
185 | |||
186 | /** |
||
187 | * Get the queue's ID. |
||
188 | * |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public function queueId() |
||
195 | |||
196 | /** |
||
197 | * @param integer $rate The throttling / processing rate, in items per second. |
||
198 | * @return self |
||
199 | */ |
||
200 | public function setRate($rate) |
||
205 | |||
206 | /** |
||
207 | * @return integer |
||
208 | */ |
||
209 | public function rate() |
||
213 | |||
214 | /** |
||
215 | * @param integer $limit The maximum number of items to load. |
||
216 | * @return self |
||
217 | */ |
||
218 | public function setLimit($limit) |
||
223 | |||
224 | /** |
||
225 | * @return integer |
||
226 | */ |
||
227 | public function limit() |
||
231 | |||
232 | /** |
||
233 | * @param integer $chunkSize The size of the chunk of items to process at the same time in the queue. |
||
234 | * @return self |
||
235 | */ |
||
236 | public function setChunkSize($chunkSize) |
||
241 | |||
242 | /** |
||
243 | * @return integer |
||
244 | */ |
||
245 | public function chunkSize() |
||
249 | |||
250 | /** |
||
251 | * Set the callback routine when an item is processed. |
||
252 | * |
||
253 | * @param callable $callback A item callback routine. |
||
254 | * @return self |
||
255 | */ |
||
256 | public function setItemCallback(callable $callback) |
||
261 | |||
262 | /** |
||
263 | * Set the callback routine when the item is resolved. |
||
264 | * |
||
265 | * @param callable $callback A item callback routine. |
||
266 | * @return self |
||
267 | */ |
||
268 | public function setItemSuccessCallback(callable $callback) |
||
273 | |||
274 | /** |
||
275 | * Set the callback routine when the item is rejected. |
||
276 | * |
||
277 | * @param callable $callback A item callback routine. |
||
278 | * @return self |
||
279 | */ |
||
280 | public function setItemFailureCallback(callable $callback) |
||
285 | |||
286 | /** |
||
287 | * Set the callback routine when the queue is processed. |
||
288 | * |
||
289 | * @param callable $callback A queue callback routine. |
||
290 | * @return self |
||
291 | */ |
||
292 | public function setProcessedCallback(callable $callback) |
||
297 | |||
298 | /** |
||
299 | * Process the queue. |
||
300 | * |
||
301 | * It can be process in a single batch or in multiple chunks to reduce memory |
||
302 | * If no callback is passed and a self::$processedCallback is set, the latter is used. |
||
303 | * |
||
304 | * @param callable $callback An optional alternative callback routine executed |
||
305 | * after all queue items are processed. |
||
306 | * @return boolean Success / Failure |
||
307 | */ |
||
308 | public function processQueue(callable $callback = null) |
||
356 | |||
357 | /** |
||
358 | * @param mixed $queuedItems The items to process. |
||
359 | * @return void |
||
360 | */ |
||
361 | private function processItems($queuedItems) |
||
408 | |||
409 | /** |
||
410 | * Throttle processing of items. |
||
411 | * |
||
412 | * @return void |
||
413 | */ |
||
414 | private function throttle() |
||
420 | |||
421 | /** |
||
422 | * Create a queue items collection loader. |
||
423 | * |
||
424 | * @return CollectionLoader |
||
425 | */ |
||
426 | public function createQueueItemsLoader() |
||
436 | |||
437 | /** |
||
438 | * Configure the queue items collection loader. |
||
439 | * |
||
440 | * @param CollectionLoader $loader The collection loader to prepare. |
||
441 | * @return void |
||
442 | */ |
||
443 | protected function configureQueueItemsLoader(CollectionLoader $loader) |
||
473 | |||
474 | /** |
||
475 | * Retrieve the items of the current queue. |
||
476 | * |
||
477 | * @return \Charcoal\Model\Collection|array |
||
478 | */ |
||
479 | public function loadQueueItems() |
||
492 | |||
493 | /** |
||
494 | * Retrieve the total of queued items. |
||
495 | * |
||
496 | * @return integer |
||
497 | */ |
||
498 | public function totalQueuedItems() |
||
505 | |||
506 | /** |
||
507 | * Retrieve the number of chunks to process. |
||
508 | * |
||
509 | * @return integer |
||
510 | */ |
||
511 | public function totalChunks() |
||
522 | |||
523 | /** |
||
524 | * Retrieve the queue item prototype model. |
||
525 | * |
||
526 | * @return QueueItemInterface |
||
527 | */ |
||
528 | public function queueItemProto() |
||
532 | |||
533 | /** |
||
534 | * Retrieve the class name of the queue item model. |
||
535 | * |
||
536 | * @return string |
||
537 | */ |
||
538 | abstract public function getQueueItemClass(); |
||
539 | |||
540 | /** |
||
541 | * @return FactoryInterface |
||
542 | */ |
||
543 | protected function queueItemFactory() |
||
547 | |||
548 | /** |
||
549 | * @param FactoryInterface $factory The factory used to create queue items. |
||
550 | * @return self |
||
551 | */ |
||
552 | private function setQueueItemFactory(FactoryInterface $factory) |
||
557 | } |
||
558 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.