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 |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Count queue messages. |
||
| 195 | * |
||
| 196 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain |
||
| 197 | * operators. Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
| 198 | * invalid {$and: [{...}, {...}]} |
||
| 199 | * @param bool|null $running query a running message or not or all |
||
| 200 | * |
||
| 201 | * @return int the count |
||
| 202 | * |
||
| 203 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 204 | */ |
||
| 205 | final public function count(array $query, bool $running = null) : int |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Acknowledge a message was processed and remove from queue. |
||
| 219 | * |
||
| 220 | * @param array $message message received from get() |
||
| 221 | * |
||
| 222 | * @return void |
||
| 223 | * |
||
| 224 | * @throws \InvalidArgumentException $message does not have a field "id" that is a MongoDB\BSON\ObjectID |
||
| 225 | */ |
||
| 226 | final public function ack(array $message) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Atomically acknowledge and send a message to the queue. |
||
| 242 | * |
||
| 243 | * @param array $message the message to ack received from get() |
||
| 244 | * @param array $payload the data to store in the message to send. Data is handled same way |
||
| 245 | * as \MongoDB\Collection::insertOne() |
||
| 246 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 247 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 248 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 249 | * |
||
| 250 | * @return void |
||
| 251 | * |
||
| 252 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
| 253 | * @throws \InvalidArgumentException $priority is NaN |
||
| 254 | */ |
||
| 255 | final public function ackSend( |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Requeue message to the queue. Same as ackSend() with the same message. |
||
| 294 | * |
||
| 295 | * @param array $message message received from get(). |
||
| 296 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 297 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 298 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 299 | * |
||
| 300 | * @return void |
||
| 301 | * |
||
| 302 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
| 303 | * @throws \InvalidArgumentException priority is NaN |
||
| 304 | */ |
||
| 305 | final public function requeue( |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Send a message to the queue. |
||
| 318 | * |
||
| 319 | * @param array $payload the data to store in the message. Data is handled same way |
||
| 320 | * as \MongoDB\Collection::insertOne() |
||
| 321 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 322 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 323 | * |
||
| 324 | * @return void |
||
| 325 | * |
||
| 326 | * @throws \InvalidArgumentException $priority is NaN |
||
| 327 | */ |
||
| 328 | final public function send(array $payload, int $earliestGet = 0, float $priority = 0.0) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Ensure index of correct specification and a unique name whether the specification or name already exist or not. |
||
| 349 | * Will not create index if $index is a prefix of an existing index |
||
| 350 | * |
||
| 351 | * @param array $index index to create in same format as \MongoDB\Collection::createIndex() |
||
| 352 | * |
||
| 353 | * @return void |
||
| 354 | * |
||
| 355 | * @throws \Exception couldnt create index after 5 attempts |
||
| 356 | */ |
||
| 357 | final private function ensureIndex(array $index) |
||
| 372 | |||
| 373 | private function isIndexIncludedInExistingIndex(array $index) : bool |
||
| 385 | |||
| 386 | private function tryCreateIndex(array $index) : bool |
||
| 396 | |||
| 397 | private function tryCreateNamedIndex(array $index, string $name) : bool |
||
| 411 | |||
| 412 | private function indexExists(array $index) : bool |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Helper method to validate keys and values for the given sort array |
||
| 425 | * |
||
| 426 | * @param array $sort The proposed sort for a mongo index. |
||
| 427 | * @param string $label The name of the variable given to the public ensureXIndex method. |
||
| 428 | * @param array &$completedFields The final index array with payload. prefix added to fields. |
||
| 429 | * |
||
| 430 | * @return void |
||
| 431 | */ |
||
| 432 | final private static function verifySort(array $sort, string $label, array &$completeFields) |
||
| 448 | } |
||
| 449 |