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 |
||
| 17 | abstract class AbstractQueue |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Maximum millisecond value to use for UTCDateTime creation. |
||
| 21 | * |
||
| 22 | * @var integer |
||
| 23 | */ |
||
| 24 | const MONGO_INT32_MAX = PHP_INT_MAX; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * mongo collection to use for queue. |
||
| 28 | * |
||
| 29 | * @var \MongoDB\Collection |
||
| 30 | */ |
||
| 31 | protected $collection; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Ensure an index for the get() method. |
||
| 35 | * |
||
| 36 | * @param array $beforeSort Fields in get() call to index before the sort field in same format |
||
| 37 | * as \MongoDB\Collection::ensureIndex() |
||
| 38 | * @param array $afterSort Fields in get() call to index after the sort field in same format as |
||
| 39 | * \MongoDB\Collection::ensureIndex() |
||
| 40 | * |
||
| 41 | * @return void |
||
| 42 | * |
||
| 43 | * @throws \InvalidArgumentException value of $beforeSort or $afterSort is not 1 or -1 for ascending and descending |
||
| 44 | * @throws \InvalidArgumentException key in $beforeSort or $afterSort was not a string |
||
| 45 | */ |
||
| 46 | final public function ensureGetIndex(array $beforeSort = [], array $afterSort = []) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Ensure an index for the count() method. |
||
| 64 | * Is a no-op if the generated index is a prefix of an existing one. If you have a similar ensureGetIndex call, |
||
| 65 | * call it first. |
||
| 66 | * |
||
| 67 | * @param array $fields fields in count() call to index in same format as \MongoDB\Collection::createIndex() |
||
| 68 | * @param bool $includeRunning whether to include the running field in the index |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | * |
||
| 72 | * @throws \InvalidArgumentException key in $fields was not a string |
||
| 73 | * @throws \InvalidArgumentException value of $fields is not 1 or -1 for ascending and descending |
||
| 74 | */ |
||
| 75 | final public function ensureCountIndex(array $fields, bool $includeRunning) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get a non running message from the queue. |
||
| 90 | * |
||
| 91 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain |
||
| 92 | * operators. Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
| 93 | * invalid {$and: [{...}, {...}]} |
||
| 94 | * @param int $runningResetDuration second duration the message can stay unacked before it resets and can be |
||
| 95 | * retreived again. |
||
| 96 | * @param int $waitDurationInMillis millisecond duration to wait for a message. |
||
| 97 | * @param int $pollDurationInMillis millisecond duration to wait between polls. |
||
| 98 | * |
||
| 99 | * @return array|null the message or null if one is not found |
||
| 100 | * |
||
| 101 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 102 | */ |
||
| 103 | final public function get( |
||
| 139 | //@codeCoverageIgnoreEnd |
||
| 140 | |||
| 141 | private function buildPayloadQuery(array $initialQuery, array $payloadQuery) |
||
| 153 | |||
| 154 | private function calculateSleepTime(int $pollDurationInMillis) : int |
||
| 161 | |||
| 162 | private function calcuateResetTimeStamp(int $runningResetDuration) : int |
||
| 172 | |||
| 173 | private function tryFindOneAndUpdate(array $query, array $update, array &$queueMessage) : bool |
||
| 192 | |||
| 193 | private function getIdFromMessage($message) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Count queue messages. |
||
| 208 | * |
||
| 209 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain |
||
| 210 | * operators. Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
| 211 | * invalid {$and: [{...}, {...}]} |
||
| 212 | * @param bool|null $running query a running message or not or all |
||
| 213 | * |
||
| 214 | * @return int the count |
||
| 215 | * |
||
| 216 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 217 | */ |
||
| 218 | final public function count(array $query, bool $running = null) : int |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Acknowledge a message was processed and remove from queue. |
||
| 232 | * |
||
| 233 | * @param array $message message received from get() |
||
| 234 | * |
||
| 235 | * @return void |
||
| 236 | * |
||
| 237 | * @throws \InvalidArgumentException $message does not have a field "id" that is a MongoDB\BSON\ObjectID |
||
| 238 | */ |
||
| 239 | final public function ack(array $message) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Atomically acknowledge and send a message to the queue. |
||
| 255 | * |
||
| 256 | * @param array $message the message to ack received from get() |
||
| 257 | * @param array $payload the data to store in the message to send. Data is handled same way |
||
| 258 | * as \MongoDB\Collection::insertOne() |
||
| 259 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 260 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 261 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | * |
||
| 265 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
| 266 | * @throws \InvalidArgumentException $priority is NaN |
||
| 267 | */ |
||
| 268 | final public function ackSend( |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Requeue message to the queue. Same as ackSend() with the same message. |
||
| 296 | * |
||
| 297 | * @param array $message message received from get(). |
||
| 298 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 299 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 300 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 301 | * |
||
| 302 | * @return void |
||
| 303 | * |
||
| 304 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
| 305 | * @throws \InvalidArgumentException priority is NaN |
||
| 306 | */ |
||
| 307 | final public function requeue( |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Send a message to the queue. |
||
| 320 | * |
||
| 321 | * @param array $payload the data to store in the message. Data is handled same way |
||
| 322 | * as \MongoDB\Collection::insertOne() |
||
| 323 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 324 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | * |
||
| 328 | * @throws \InvalidArgumentException $priority is NaN |
||
| 329 | */ |
||
| 330 | final public function send(array $payload, int $earliestGet = 0, float $priority = 0.0) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Ensure index of correct specification and a unique name whether the specification or name already exist or not. |
||
| 351 | * Will not create index if $index is a prefix of an existing index |
||
| 352 | * |
||
| 353 | * @param array $index index to create in same format as \MongoDB\Collection::createIndex() |
||
| 354 | * |
||
| 355 | * @return void |
||
| 356 | * |
||
| 357 | * @throws \Exception couldnt create index after 5 attempts |
||
| 358 | */ |
||
| 359 | final private function ensureIndex(array $index) |
||
| 374 | |||
| 375 | private function isIndexIncludedInExistingIndex(array $index) : bool |
||
| 387 | |||
| 388 | private function tryCreateIndex(array $index) : bool |
||
| 398 | |||
| 399 | private function tryCreateNamedIndex(array $index, string $name) : bool |
||
| 413 | |||
| 414 | private function indexExists(array $index) : bool |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Helper method to validate keys and values for the given sort array |
||
| 427 | * |
||
| 428 | * @param array $sort The proposed sort for a mongo index. |
||
| 429 | * @param string $label The name of the variable given to the public ensureXIndex method. |
||
| 430 | * @param array &$completedFields The final index array with payload. prefix added to fields. |
||
| 431 | * |
||
| 432 | * @return void |
||
| 433 | */ |
||
| 434 | final private function verifySort(array $sort, string $label, array &$completeFields) |
||
| 446 | |||
| 447 | private function throwIfTrue( |
||
| 457 | |||
| 458 | private function getEarliestGetAsUTCDateTime(int $timestamp) : UTCDateTime |
||
| 464 | } |
||
| 465 |