Complex classes like Queue 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 Queue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | final class Queue implements QueueInterface |
||
| 15 | { |
||
| 16 | const MONGO_INT32_MAX = 2147483647;//2147483648 can overflow in php mongo without using the MongoInt64 |
||
| 17 | |||
| 18 | /** |
||
| 19 | * mongo collection to use for queue. |
||
| 20 | * |
||
| 21 | * @var \MongoCollection |
||
| 22 | */ |
||
| 23 | private $collection; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Construct queue. |
||
| 27 | * |
||
| 28 | * @param \MongoCollection|string $collectionOrUrl A MongoCollection instance or the mongo connection url. |
||
| 29 | * @param string $db the mongo db name |
||
| 30 | * @param string $collection the collection name to use for the queue |
||
| 31 | * |
||
| 32 | * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string |
||
| 33 | */ |
||
| 34 | public function __construct($collectionOrUrl, $db = null, $collection = null) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Ensure an index for the get() method. |
||
| 60 | * |
||
| 61 | * @param array $beforeSort Fields in get() call to index before the sort field in same format |
||
| 62 | * as \MongoCollection::ensureIndex() |
||
| 63 | * @param array $afterSort Fields in get() call to index after the sort field in same format as |
||
| 64 | * \MongoCollection::ensureIndex() |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | * |
||
| 68 | * @throws \InvalidArgumentException value of $beforeSort or $afterSort is not 1 or -1 for ascending and descending |
||
| 69 | * @throws \InvalidArgumentException key in $beforeSort or $afterSort was not a string |
||
| 70 | */ |
||
| 71 | public function ensureGetIndex(array $beforeSort = [], array $afterSort = []) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Ensure an index for the count() method. |
||
| 94 | * Is a no-op if the generated index is a prefix of an existing one. If you have a similar ensureGetIndex call, |
||
| 95 | * call it first. |
||
| 96 | * |
||
| 97 | * @param array $fields fields in count() call to index in same format as \MongoCollection::ensureIndex() |
||
| 98 | * @param bool $includeRunning whether to include the running field in the index |
||
| 99 | * |
||
| 100 | * @return void |
||
| 101 | * |
||
| 102 | * @throws \InvalidArgumentException $includeRunning was not a boolean |
||
| 103 | * @throws \InvalidArgumentException key in $fields was not a string |
||
| 104 | * @throws \InvalidArgumentException value of $fields is not 1 or -1 for ascending and descending |
||
| 105 | */ |
||
| 106 | public function ensureCountIndex(array $fields, $includeRunning) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get a non running message from the queue. |
||
| 125 | * |
||
| 126 | * @param array $query in same format as \MongoCollection::find() where top level fields do not contain operators. |
||
| 127 | * Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
| 128 | * invalid {$and: [{...}, {...}]} |
||
| 129 | * @param int $runningResetDuration second duration the message can stay unacked before it resets and can be |
||
| 130 | * retreived again. |
||
| 131 | * @param int $waitDurationInMillis millisecond duration to wait for a message. |
||
| 132 | * @param int $pollDurationInMillis millisecond duration to wait between polls. |
||
| 133 | * |
||
| 134 | * @return array|null the message or null if one is not found |
||
| 135 | * |
||
| 136 | * @throws \InvalidArgumentException $runningResetDuration, $waitDurationInMillis or $pollDurationInMillis was not |
||
| 137 | * an int |
||
| 138 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 139 | */ |
||
| 140 | public function get(array $query, $runningResetDuration, $waitDurationInMillis = 3000, $pollDurationInMillis = 200) |
||
| 209 | //@codeCoverageIgnoreEnd |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Count queue messages. |
||
| 213 | * |
||
| 214 | * @param array $query in same format as \MongoCollection::find() where top level fields do not contain operators. |
||
| 215 | * Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, invalid {$and: [{...}, {...}]} |
||
| 216 | * @param bool|null $running query a running message or not or all |
||
| 217 | * |
||
| 218 | * @return int the count |
||
| 219 | * |
||
| 220 | * @throws \InvalidArgumentException $running was not null and not a bool |
||
| 221 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 222 | */ |
||
| 223 | public function count(array $query, $running = null) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Acknowledge a message was processed and remove from queue. |
||
| 242 | * |
||
| 243 | * @param array $message message received from get() |
||
| 244 | * |
||
| 245 | * @return void |
||
| 246 | * |
||
| 247 | * @throws \InvalidArgumentException $message does not have a field "id" that is a MongoId |
||
| 248 | */ |
||
| 249 | public function ack(array $message) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Atomically acknowledge and send a message to the queue. |
||
| 265 | * |
||
| 266 | * @param array $message the message to ack received from get() |
||
| 267 | * @param array $payload the data to store in the message to send. Data is handled same way |
||
| 268 | * as \MongoCollection::insert() |
||
| 269 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 270 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 271 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 272 | * |
||
| 273 | * @return void |
||
| 274 | * |
||
| 275 | * @throws \InvalidArgumentException $message does not have a field "id" that is a MongoId |
||
| 276 | * @throws \InvalidArgumentException $earliestGet was not an int |
||
| 277 | * @throws \InvalidArgumentException $priority was not a float |
||
| 278 | * @throws \InvalidArgumentException $priority is NaN |
||
| 279 | * @throws \InvalidArgumentException $newTimestamp was not a bool |
||
| 280 | */ |
||
| 281 | public function ackSend(array $message, array $payload, $earliestGet = 0, $priority = 0.0, $newTimestamp = true) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Requeue message to the queue. Same as ackSend() with the same message. |
||
| 329 | * |
||
| 330 | * @param array $message message received from get(). |
||
| 331 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 332 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 333 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | * |
||
| 337 | * @throws \InvalidArgumentException $message does not have a field "id" that is a MongoId |
||
| 338 | * @throws \InvalidArgumentException $earliestGet was not an int |
||
| 339 | * @throws \InvalidArgumentException $priority was not a float |
||
| 340 | * @throws \InvalidArgumentException priority is NaN |
||
| 341 | * @throws \InvalidArgumentException $newTimestamp was not a bool |
||
| 342 | */ |
||
| 343 | public function requeue(array $message, $earliestGet = 0, $priority = 0.0, $newTimestamp = true) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Send a message to the queue. |
||
| 352 | * |
||
| 353 | * @param array $payload the data to store in the message. Data is handled same way as \MongoCollection::insert() |
||
| 354 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 355 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 356 | * |
||
| 357 | * @return void |
||
| 358 | * |
||
| 359 | * @throws \InvalidArgumentException $earliestGet was not an int |
||
| 360 | * @throws \InvalidArgumentException $priority was not a float |
||
| 361 | * @throws \InvalidArgumentException $priority is NaN |
||
| 362 | */ |
||
| 363 | public function send(array $payload, $earliestGet = 0, $priority = 0.0) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Ensure index of correct specification and a unique name whether the specification or name already exist or not. |
||
| 394 | * Will not create index if $index is a prefix of an existing index |
||
| 395 | * |
||
| 396 | * @param array $index index to create in same format as \MongoCollection::ensureIndex() |
||
| 397 | * |
||
| 398 | * @return void |
||
| 399 | * |
||
| 400 | * @throws \Exception couldnt create index after 5 attempts |
||
| 401 | */ |
||
| 402 | private function ensureIndex(array $index) |
||
| 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 | private static function verifySort(array $sort, $label, &$completeFields) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Helper method to validate get queries. |
||
| 463 | * |
||
| 464 | * @param array $query The user provided query. |
||
| 465 | * @param array &$completeQuery The validated query for gets. |
||
| 466 | * |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | private static function verifyQuery(array $query, array &$completeQuery) |
||
| 479 | } |
||
| 480 |