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( |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Count queue messages. |
||
| 137 | * |
||
| 138 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain |
||
| 139 | * operators. Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
| 140 | * invalid {$and: [{...}, {...}]} |
||
| 141 | * @param bool|null $running query a running message or not or all |
||
| 142 | * |
||
| 143 | * @return int the count |
||
| 144 | * |
||
| 145 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 146 | */ |
||
| 147 | final public function count(array $query, bool $running = null) : int |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Acknowledge a message was processed and remove from queue. |
||
| 161 | * |
||
| 162 | * @param array $message message received from get() |
||
| 163 | * |
||
| 164 | * @return void |
||
| 165 | * |
||
| 166 | * @throws \InvalidArgumentException $message does not have a field "id" that is a MongoDB\BSON\ObjectID |
||
| 167 | */ |
||
| 168 | final public function ack(array $message) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Atomically acknowledge and send a message to the queue. |
||
| 184 | * |
||
| 185 | * @param array $message the message to ack received from get() |
||
| 186 | * @param array $payload the data to store in the message to send. Data is handled same way |
||
| 187 | * as \MongoDB\Collection::insertOne() |
||
| 188 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 189 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 190 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | * |
||
| 194 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
| 195 | * @throws \InvalidArgumentException $priority is NaN |
||
| 196 | */ |
||
| 197 | final public function ackSend( |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Requeue message to the queue. Same as ackSend() with the same message. |
||
| 225 | * |
||
| 226 | * @param array $message message received from get(). |
||
| 227 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 228 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 229 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 230 | * |
||
| 231 | * @return void |
||
| 232 | * |
||
| 233 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
| 234 | * @throws \InvalidArgumentException priority is NaN |
||
| 235 | */ |
||
| 236 | final public function requeue( |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Send a message to the queue. |
||
| 249 | * |
||
| 250 | * @param array $payload the data to store in the message. Data is handled same way |
||
| 251 | * as \MongoDB\Collection::insertOne() |
||
| 252 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 253 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 254 | * |
||
| 255 | * @return void |
||
| 256 | * |
||
| 257 | * @throws \InvalidArgumentException $priority is NaN |
||
| 258 | */ |
||
| 259 | final public function send(array $payload, int $earliestGet = 0, float $priority = 0.0) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Ensure index of correct specification and a unique name whether the specification or name already exist or not. |
||
| 280 | * Will not create index if $index is a prefix of an existing index |
||
| 281 | * |
||
| 282 | * @param array $index index to create in same format as \MongoDB\Collection::createIndex() |
||
| 283 | * |
||
| 284 | * @return void |
||
| 285 | * |
||
| 286 | * @throws \Exception couldnt create index after 5 attempts |
||
| 287 | */ |
||
| 288 | final private function ensureIndex(array $index) |
||
| 303 | |||
| 304 | private function buildPayloadQuery(array $initialQuery, array $payloadQuery) |
||
| 316 | |||
| 317 | private function calculateSleepTime(int $pollDurationInMillis) : int |
||
| 324 | |||
| 325 | private function calcuateResetTimeStamp(int $runningResetDuration) : int |
||
| 335 | |||
| 336 | private function tryFindOneAndUpdate(array $query, array $update, array &$queueMessage) : bool |
||
| 355 | |||
| 356 | private function getIdFromMessage($message) |
||
| 368 | |||
| 369 | private function isIndexIncludedInExistingIndex(array $index) : bool |
||
| 381 | |||
| 382 | private function tryCreateIndex(array $index) : bool |
||
| 392 | |||
| 393 | private function tryCreateNamedIndex(array $index, string $name) : bool |
||
| 407 | |||
| 408 | private function indexExists(array $index) : bool |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Helper method to validate keys and values for the given sort array |
||
| 421 | * |
||
| 422 | * @param array $sort The proposed sort for a mongo index. |
||
| 423 | * @param string $label The name of the variable given to the public ensureXIndex method. |
||
| 424 | * @param array &$completedFields The final index array with payload. prefix added to fields. |
||
| 425 | * |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | final private function verifySort(array $sort, string $label, array &$completeFields) |
||
| 440 | |||
| 441 | private function throwIfTrue( |
||
| 451 | |||
| 452 | private function getEarliestGetAsUTCDateTime(int $timestamp) : UTCDateTime |
||
| 458 | } |
||
| 459 |