Complex classes like AbstractQueue 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 AbstractQueue, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class AbstractQueue implements QueueInterface |
||
17 | { |
||
18 | /** |
||
19 | * Maximum millisecond value to use for UTCDateTime creation. |
||
20 | * |
||
21 | * @var integer |
||
22 | */ |
||
23 | const MONGO_INT32_MAX = PHP_INT_MAX; |
||
24 | |||
25 | /** |
||
26 | * mongo collection to use for queue. |
||
27 | * |
||
28 | * @var \MongoDB\Collection |
||
29 | */ |
||
30 | protected $collection; |
||
31 | |||
32 | /** |
||
33 | * Ensure an index for the get() method. |
||
34 | * |
||
35 | * @param array $beforeSort Fields in get() call to index before the sort field in same format |
||
36 | * as \MongoDB\Collection::ensureIndex() |
||
37 | * @param array $afterSort Fields in get() call to index after the sort field in same format as |
||
38 | * \MongoDB\Collection::ensureIndex() |
||
39 | * |
||
40 | * @return void |
||
41 | * |
||
42 | * @throws \InvalidArgumentException value of $beforeSort or $afterSort is not 1 or -1 for ascending and descending |
||
43 | * @throws \InvalidArgumentException key in $beforeSort or $afterSort was not a string |
||
44 | */ |
||
45 | final public function ensureGetIndex(array $beforeSort = [], array $afterSort = []) |
||
60 | |||
61 | /** |
||
62 | * Ensure an index for the count() method. |
||
63 | * Is a no-op if the generated index is a prefix of an existing one. If you have a similar ensureGetIndex call, |
||
64 | * call it first. |
||
65 | * |
||
66 | * @param array $fields fields in count() call to index in same format as \MongoDB\Collection::createIndex() |
||
67 | * @param bool $includeRunning whether to include the running field in the index |
||
68 | * |
||
69 | * @return void |
||
70 | * |
||
71 | * @throws \InvalidArgumentException key in $fields was not a string |
||
72 | * @throws \InvalidArgumentException value of $fields is not 1 or -1 for ascending and descending |
||
73 | */ |
||
74 | final public function ensureCountIndex(array $fields, bool $includeRunning) |
||
86 | |||
87 | /** |
||
88 | * Get a non running message from the queue. |
||
89 | * |
||
90 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain |
||
91 | * operators. Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
92 | * invalid {$and: [{...}, {...}]} |
||
93 | * @param int $runningResetDuration second duration the message can stay unacked before it resets and can be |
||
94 | * retreived again. |
||
95 | * @param int $waitDurationInMillis millisecond duration to wait for a message. |
||
96 | * @param int $pollDurationInMillis millisecond duration to wait between polls. |
||
97 | * |
||
98 | * @return array|null the message or null if one is not found |
||
99 | * |
||
100 | * @throws \InvalidArgumentException key in $query was not a string |
||
101 | */ |
||
102 | final public function get( |
||
138 | //@codeCoverageIgnoreEnd |
||
139 | |||
140 | private function buildPayloadQuery(array $initialQuery, array $payloadQuery) |
||
152 | |||
153 | private function calculateSleepTime(int $pollDurationInMillis) : int |
||
160 | |||
161 | private function calcuateResetTimeStamp(int $runningResetDuration) : int |
||
171 | |||
172 | private function tryFindOneAndUpdate(array $query, array $update, array &$queueMessage) : bool |
||
191 | |||
192 | private function getIdFromMessage($message) |
||
204 | |||
205 | /** |
||
206 | * Count queue messages. |
||
207 | * |
||
208 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain |
||
209 | * operators. Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
210 | * invalid {$and: [{...}, {...}]} |
||
211 | * @param bool|null $running query a running message or not or all |
||
212 | * |
||
213 | * @return int the count |
||
214 | * |
||
215 | * @throws \InvalidArgumentException key in $query was not a string |
||
216 | */ |
||
217 | final public function count(array $query, bool $running = null) : int |
||
228 | |||
229 | /** |
||
230 | * Acknowledge a message was processed and remove from queue. |
||
231 | * |
||
232 | * @param array $message message received from get() |
||
233 | * |
||
234 | * @return void |
||
235 | * |
||
236 | * @throws \InvalidArgumentException $message does not have a field "id" that is a MongoDB\BSON\ObjectID |
||
237 | */ |
||
238 | final public function ack(array $message) |
||
251 | |||
252 | /** |
||
253 | * Atomically acknowledge and send a message to the queue. |
||
254 | * |
||
255 | * @param array $message the message to ack received from get() |
||
256 | * @param array $payload the data to store in the message to send. Data is handled same way |
||
257 | * as \MongoDB\Collection::insertOne() |
||
258 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
259 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
260 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
261 | * |
||
262 | * @return void |
||
263 | * |
||
264 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
265 | * @throws \InvalidArgumentException $priority is NaN |
||
266 | */ |
||
267 | final public function ackSend( |
||
303 | |||
304 | /** |
||
305 | * Requeue message to the queue. Same as ackSend() with the same message. |
||
306 | * |
||
307 | * @param array $message message received from get(). |
||
308 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
309 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
310 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
311 | * |
||
312 | * @return void |
||
313 | * |
||
314 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
315 | * @throws \InvalidArgumentException priority is NaN |
||
316 | */ |
||
317 | final public function requeue( |
||
327 | |||
328 | /** |
||
329 | * Send a message to the queue. |
||
330 | * |
||
331 | * @param array $payload the data to store in the message. Data is handled same way |
||
332 | * as \MongoDB\Collection::insertOne() |
||
333 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
334 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
335 | * |
||
336 | * @return void |
||
337 | * |
||
338 | * @throws \InvalidArgumentException $priority is NaN |
||
339 | */ |
||
340 | final public function send(array $payload, int $earliestGet = 0, float $priority = 0.0) |
||
358 | |||
359 | /** |
||
360 | * Ensure index of correct specification and a unique name whether the specification or name already exist or not. |
||
361 | * Will not create index if $index is a prefix of an existing index |
||
362 | * |
||
363 | * @param array $index index to create in same format as \MongoDB\Collection::createIndex() |
||
364 | * |
||
365 | * @return void |
||
366 | * |
||
367 | * @throws \Exception couldnt create index after 5 attempts |
||
368 | */ |
||
369 | final private function ensureIndex(array $index) |
||
384 | |||
385 | private function isIndexIncludedInExistingIndex(array $index) : bool |
||
397 | |||
398 | private function tryCreateIndex(array $index) : bool |
||
408 | |||
409 | private function tryCreateNamedIndex(array $index, string $name) : bool |
||
423 | |||
424 | private function indexExists(array $index) : bool |
||
434 | |||
435 | /** |
||
436 | * Helper method to validate keys and values for the given sort array |
||
437 | * |
||
438 | * @param array $sort The proposed sort for a mongo index. |
||
439 | * @param string $label The name of the variable given to the public ensureXIndex method. |
||
440 | * @param array &$completedFields The final index array with payload. prefix added to fields. |
||
441 | * |
||
442 | * @return void |
||
443 | */ |
||
444 | final private static function verifySort(array $sort, string $label, array &$completeFields) |
||
460 | } |
||
461 |