| Total Complexity | 42 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 19 | abstract class AbstractQueue |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Maximum millisecond value to use for UTCDateTime creation. |
||
| 23 | * |
||
| 24 | * @var integer |
||
| 25 | */ |
||
| 26 | const MONGO_INT32_MAX = PHP_INT_MAX; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * mongo collection to use for queue. |
||
| 30 | * |
||
| 31 | * @var \MongoDB\Collection |
||
| 32 | */ |
||
| 33 | protected $collection; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | const FIND_ONE_AND_UPDATE_OPTIONS = [ |
||
| 39 | 'sort' => ['priority' => 1, 'created' => 1], |
||
| 40 | 'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'], |
||
| 41 | 'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER, |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var integer |
||
| 46 | */ |
||
| 47 | const DEFAULT_MAX_NUMBER_OF_MESSAGES = 1; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var integer |
||
| 51 | */ |
||
| 52 | const DEFAULT_RUNNING_RESET_DURATION = 600000; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var integer |
||
| 56 | */ |
||
| 57 | const DEFAULT_WAIT_DURATION_IN_MILLISECONDS = 3000; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var integer |
||
| 61 | */ |
||
| 62 | const DEFAULT_POLL_DURATION_IN_MILLISECONDS = 200; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | const DEFAULT_GET_OPTIONS = [ |
||
| 68 | 'maxNumberOfMessages' => self::DEFAULT_MAX_NUMBER_OF_MESSAGES, |
||
| 69 | 'runningResetDuration' => self::DEFAULT_RUNNING_RESET_DURATION, |
||
| 70 | 'waitDurationInMillis' => self::DEFAULT_WAIT_DURATION_IN_MILLISECONDS, |
||
| 71 | 'pollDurationInMillis' => self::DEFAULT_POLL_DURATION_IN_MILLISECONDS, |
||
| 72 | ]; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Ensure an index for the get() method. |
||
| 76 | * |
||
| 77 | * @param array $beforeSort Fields in get() call to index before the sort field in same format |
||
| 78 | * as \MongoDB\Collection::ensureIndex() |
||
| 79 | * @param array $afterSort Fields in get() call to index after the sort field in same format as |
||
| 80 | * \MongoDB\Collection::ensureIndex() |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | * |
||
| 84 | * @throws \InvalidArgumentException value of $beforeSort or $afterSort is not 1 or -1 for ascending and descending |
||
| 85 | * @throws \InvalidArgumentException key in $beforeSort or $afterSort was not a string |
||
| 86 | */ |
||
| 87 | final public function ensureGetIndex(array $beforeSort = [], array $afterSort = []) |
||
| 88 | { |
||
| 89 | //using general rule: equality, sort, range or more equality tests in that order for index |
||
| 90 | $completeFields = ['earliestGet' => 1]; |
||
| 91 | |||
| 92 | $this->verifySort($beforeSort, 'beforeSort', $completeFields); |
||
| 93 | |||
| 94 | $completeFields['priority'] = 1; |
||
| 95 | $completeFields['created'] = 1; |
||
| 96 | |||
| 97 | $this->verifySort($afterSort, 'afterSort', $completeFields); |
||
| 98 | |||
| 99 | //for the main query in get() |
||
| 100 | $this->ensureIndex($completeFields); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Ensure an index for the count() method. |
||
| 105 | * Is a no-op if the generated index is a prefix of an existing one. If you have a similar ensureGetIndex call, |
||
| 106 | * call it first. |
||
| 107 | * |
||
| 108 | * @param array $fields fields in count() call to index in same format as \MongoDB\Collection::createIndex() |
||
| 109 | * @param bool $includeRunning whether to include the running field in the index |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | * |
||
| 113 | * @throws \InvalidArgumentException key in $fields was not a string |
||
| 114 | * @throws \InvalidArgumentException value of $fields is not 1 or -1 for ascending and descending |
||
| 115 | */ |
||
| 116 | final public function ensureCountIndex(array $fields, bool $includeRunning) |
||
| 117 | { |
||
| 118 | $completeFields = []; |
||
| 119 | |||
| 120 | if ($includeRunning) { |
||
| 121 | $completeFields['earliestGet'] = 1; |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->verifySort($fields, 'fields', $completeFields); |
||
| 125 | |||
| 126 | $this->ensureIndex($completeFields); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get a non running message from the queue. |
||
| 131 | * |
||
| 132 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain |
||
| 133 | * operators. Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
| 134 | * invalid {$and: [{...}, {...}]} |
||
| 135 | * @param array $options Associative array of get options. |
||
| 136 | * runningResetDuration => integer |
||
| 137 | * The duration (in miiliseconds) that the received messages are hidden from |
||
| 138 | * subsequent retrieve requests after being retrieved by a get() request. |
||
| 139 | * waitDurationInMillis => integer |
||
| 140 | * The duration (in milliseconds) for which the call will wait for a message to |
||
| 141 | * arrive in the queue before returning. If a message is available, the call will |
||
| 142 | * return sooner than WaitTimeSeconds. |
||
| 143 | * pollDurationInMillis => integer |
||
| 144 | * The millisecond duration to wait between polls. |
||
| 145 | * maxNumberOfMessages => integer |
||
| 146 | * The maximum number of messages to return with get(). All of the messages are not |
||
| 147 | * necessarily returned. |
||
| 148 | * |
||
| 149 | * @return array Array of messages. |
||
| 150 | * |
||
| 151 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 152 | */ |
||
| 153 | final public function get(array $query, array $options = []) : array |
||
| 154 | { |
||
| 155 | $completeQuery = $this->buildPayloadQuery( |
||
| 156 | ['earliestGet' => ['$lte' => new UTCDateTime((int)(microtime(true) * 1000))]], |
||
| 157 | $query |
||
| 158 | ); |
||
| 159 | |||
| 160 | $options += self::DEFAULT_GET_OPTIONS; |
||
| 161 | $update = ['$set' => ['earliestGet' => $this->calculateEarliestGet($options['runningResetDuration'])]]; |
||
| 162 | $end = $this->calculateEndTime($options['waitDurationInMillis']); |
||
| 163 | $sleepTime = $this->calculateSleepTime($options['pollDurationInMillis']); |
||
| 164 | $messages = new ArrayObject(); |
||
| 165 | while (count($messages) < $options['maxNumberOfMessages']) { |
||
| 166 | if ($this->tryFindOneAndUpdate($completeQuery, $update, $messages)) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | if (microtime(true) < $end) { |
||
| 171 | usleep($sleepTime); |
||
| 172 | } |
||
| 173 | |||
| 174 | break; |
||
| 175 | } |
||
| 176 | |||
| 177 | return $messages->getArrayCopy(); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Count queue messages. |
||
| 182 | * |
||
| 183 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain |
||
| 184 | * operators. Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
| 185 | * invalid {$and: [{...}, {...}]} |
||
| 186 | * @param bool|null $running query a running message or not or all |
||
| 187 | * |
||
| 188 | * @return int the count |
||
| 189 | * |
||
| 190 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 191 | */ |
||
| 192 | final public function count(array $query, bool $running = null) : int |
||
| 193 | { |
||
| 194 | $totalQuery = []; |
||
| 195 | |||
| 196 | if ($running === true || $running === false) { |
||
| 197 | $key = $running ? '$gt' : '$lte'; |
||
| 198 | $totalQuery['earliestGet'] = [$key => new UTCDateTime((int)(microtime(true) * 1000))]; |
||
| 199 | } |
||
| 200 | |||
| 201 | return $this->collection->countDocuments($this->buildPayloadQuery($totalQuery, $query)); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Acknowledge a message was processed and remove from queue. |
||
| 206 | * |
||
| 207 | * @param Message $message message received from get() |
||
| 208 | * |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | final public function ack(Message $message) |
||
| 212 | { |
||
| 213 | $this->collection->deleteOne(['_id' => $message->getId()]); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Atomically acknowledge and send a message to the queue. |
||
| 218 | * |
||
| 219 | * @param Message $message message received from get(). |
||
| 220 | * |
||
| 221 | * @return void |
||
| 222 | */ |
||
| 223 | final public function requeue(Message $message) |
||
| 224 | { |
||
| 225 | $set = [ |
||
| 226 | 'payload' => $message->getPayload(), |
||
| 227 | 'earliestGet' => $message->getEarliestGet(), |
||
| 228 | 'priority' => $message->getPriority(), |
||
| 229 | 'machineName' => gethostname(), |
||
| 230 | 'created' => new UTCDateTime(), |
||
| 231 | ]; |
||
| 232 | |||
| 233 | $this->collection->updateOne(['_id' => $message->getId()], ['$set' => $set], ['upsert' => true]); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Send a message to the queue. |
||
| 238 | * |
||
| 239 | * @param Message $message The message to send. |
||
| 240 | * |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | final public function send(Message $message) |
||
| 244 | { |
||
| 245 | $document = [ |
||
| 246 | '_id' => $message->getId(), |
||
| 247 | 'payload' => $message->getPayload(), |
||
| 248 | 'earliestGet' => $message->getEarliestGet(), |
||
| 249 | 'priority' => $message->getPriority(), |
||
| 250 | 'machineName' => gethostname(), |
||
| 251 | 'created' => new UTCDateTime(), |
||
| 252 | ]; |
||
| 253 | |||
| 254 | $this->collection->insertOne($document); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Ensure index of correct specification and a unique name whether the specification or name already exist or not. |
||
| 259 | * Will not create index if $index is a prefix of an existing index |
||
| 260 | * |
||
| 261 | * @param array $index index to create in same format as \MongoDB\Collection::createIndex() |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | * |
||
| 265 | * @throws \Exception couldnt create index after 5 attempts |
||
| 266 | */ |
||
| 267 | private function ensureIndex(array $index) |
||
| 280 | //@codeCoverageIgnoreEnd |
||
| 281 | } |
||
| 282 | |||
| 283 | private function buildPayloadQuery(array $initialQuery, array $payloadQuery) |
||
| 284 | { |
||
| 285 | foreach ($payloadQuery as $key => $value) { |
||
| 294 | } |
||
| 295 | |||
| 296 | private function calculateSleepTime(int $pollDurationInMillis) : int |
||
| 297 | { |
||
| 298 | $pollDurationInMillis = max($pollDurationInMillis, 0); |
||
| 299 | $sleepTime = $pollDurationInMillis * 1000; |
||
| 300 | return $sleepTime; |
||
| 301 | } |
||
| 302 | |||
| 303 | private function calculateEarliestGet(int $runningResetDuration) : UTCDateTime |
||
| 304 | { |
||
| 305 | $resetTimestamp = time() + $runningResetDuration; |
||
| 306 | //ints overflow to floats, max at PHP_INT_MAX |
||
| 307 | return new UTCDateTime(min(max(0, $resetTimestamp * 1000), self::MONGO_INT32_MAX)); |
||
| 308 | } |
||
| 309 | |||
| 310 | private function tryFindOneAndUpdate(array $query, array $update, ArrayObject $messages) : bool |
||
| 311 | { |
||
| 312 | $document = $this->collection->findOneAndUpdate($query, $update, self::FIND_ONE_AND_UPDATE_OPTIONS); |
||
| 313 | if ($document === null) { |
||
| 314 | return false; |
||
| 315 | } |
||
| 316 | |||
| 317 | $messages[] = new Message( |
||
| 318 | $document['_id'], |
||
| 319 | $document['payload'], |
||
| 320 | $document['earliestGet'], |
||
| 321 | $document['priority'] |
||
| 322 | ); |
||
| 323 | |||
| 324 | return true; |
||
| 325 | } |
||
| 326 | |||
| 327 | private function isIndexIncludedInExistingIndex(array $index) : bool |
||
| 338 | } |
||
| 339 | |||
| 340 | private function tryCreateIndex(array $index) : bool |
||
| 341 | { |
||
| 342 | for ($name = uniqid(); strlen($name) > 0; $name = substr($name, 0, -1)) { |
||
| 343 | if ($this->tryCreateNamedIndex($index, $name)) { |
||
| 344 | return true; |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | return false; |
||
| 349 | } |
||
| 350 | |||
| 351 | private function tryCreateNamedIndex(array $index, string $name) : bool |
||
| 352 | { |
||
| 353 | //creating an index with same name and different spec does nothing. |
||
| 354 | //creating an index with same spec and different name does nothing. |
||
| 355 | //so we use any generated name, and then find the right spec after we have called, |
||
| 356 | //and just go with that name. |
||
| 357 | try { |
||
| 358 | $this->collection->createIndex($index, ['name' => $name, 'background' => true]); |
||
| 359 | } catch (\MongoDB\Exception\Exception $e) { |
||
| 360 | //this happens when the name was too long, let continue |
||
| 361 | } |
||
| 362 | |||
| 363 | return $this->indexExists($index); |
||
| 364 | } |
||
| 365 | |||
| 366 | private function indexExists(array $index) : bool |
||
| 367 | { |
||
| 368 | foreach ($this->collection->listIndexes() as $existingIndex) { |
||
| 369 | if ($existingIndex['key'] === $index) { |
||
| 370 | return true; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | return false; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Helper method to validate keys and values for the given sort array |
||
| 379 | * |
||
| 380 | * @param array $sort The proposed sort for a mongo index. |
||
| 381 | * @param string $label The name of the variable given to the public ensureXIndex method. |
||
| 382 | * @param array &$completedFields The final index array with payload. prefix added to fields. |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | private function verifySort(array $sort, string $label, array &$completeFields) |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | private function throwIfTrue( |
||
| 400 | bool $condition, |
||
| 401 | string $message, |
||
| 402 | string $exceptionClass = '\\InvalidArgumentException' |
||
| 403 | ) { |
||
| 404 | if ($condition === true) { |
||
| 405 | $reflectionClass = new \ReflectionClass($exceptionClass); |
||
| 406 | throw $reflectionClass->newInstanceArgs([$message]); |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | private function calculateEndTime(int $waitDurationInMillis) : float |
||
| 413 | } |
||
| 414 | } |
||
| 415 |