| Total Complexity | 50 |
| Total Lines | 500 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 1 |
Complex classes like QueueEntity 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 QueueEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class QueueEntity implements PublisherInterface, ConsumerInterface, AMQPEntityInterface, LoggerAwareInterface |
||
| 27 | { |
||
| 28 | use LoggerAwareTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @const int Retry count when a Channel Closed exeption is thrown |
||
| 32 | */ |
||
| 33 | const MAX_RETRIES = 3; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @const array Default connections parameters |
||
| 37 | */ |
||
| 38 | const DEFAULTS = [ |
||
| 39 | // Whether to check if it exists or to verify existance using argument types (Throws PRECONDITION_FAILED) |
||
| 40 | 'passive' => false, |
||
| 41 | // Entities with durable will be re-created uppon server restart |
||
| 42 | 'durable' => false, |
||
| 43 | // whether to use it by only one channel, then it gets deleted |
||
| 44 | 'exclusive' => false, |
||
| 45 | // Whether to delete it when the queue has no event on it |
||
| 46 | 'auto_delete' => false, |
||
| 47 | // Whether to receive a Declare confirmation |
||
| 48 | 'nowait' => false, |
||
| 49 | // Additional arguments for queue creation |
||
| 50 | 'arguments' => [], |
||
| 51 | // Whether to auto create the entity before publishing/consuming it |
||
| 52 | 'auto_create' => false, |
||
| 53 | // whether to "hide" the exception on re-declare. |
||
| 54 | // if the `passive` filter is set true, this is redundant |
||
| 55 | 'throw_exception_on_redeclare' => true, |
||
| 56 | // whether to throw on exception when trying to |
||
| 57 | // bind to an in-existent queue/exchange |
||
| 58 | 'throw_exception_on_bind_fail' => true, |
||
| 59 | // no ideea what it represents - @todo - find a documentation that states it's role |
||
| 60 | 'ticket' => null |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var AMQPConnection |
||
| 65 | */ |
||
| 66 | protected $connection; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $aliasName; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $attributes; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var int |
||
| 80 | */ |
||
| 81 | protected $prefetchCount = 1; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var null|string|MessageProcessorInterface |
||
| 85 | */ |
||
| 86 | protected $messageProcessor = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | protected $limitMessageCount; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | protected $limitSecondsUptime; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | protected $limitMemoryConsumption; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var double |
||
| 105 | */ |
||
| 106 | protected $startTime = 0; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var int |
||
| 110 | */ |
||
| 111 | protected $retryCount = 0; |
||
| 112 | /** |
||
| 113 | * @var bool |
||
| 114 | */ |
||
| 115 | protected $globalPrefetch = true; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param AMQPConnection $connection |
||
| 119 | * @param string $aliasName |
||
| 120 | * @param array $queueDetails |
||
| 121 | * @return QueueEntity |
||
| 122 | */ |
||
| 123 | public static function createQueue(AMQPConnection $connection, string $aliasName, array $queueDetails) |
||
| 124 | { |
||
| 125 | return new static( |
||
| 126 | $connection, |
||
| 127 | $aliasName, |
||
| 128 | array_merge(self::DEFAULTS, $queueDetails) |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getAliasName(): string |
||
| 136 | { |
||
| 137 | return $this->aliasName; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * ExchangeEntity constructor. |
||
| 142 | * |
||
| 143 | * @param AMQPConnection $connection |
||
| 144 | * @param string $aliasName |
||
| 145 | * @param array $attributes |
||
| 146 | */ |
||
| 147 | public function __construct(AMQPConnection $connection, string $aliasName, array $attributes = []) |
||
| 148 | { |
||
| 149 | $this->connection = $connection; |
||
| 150 | $this->aliasName = $aliasName; |
||
| 151 | $this->attributes = $attributes; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param int $prefetchCount |
||
| 156 | * @return ConsumerInterface |
||
| 157 | */ |
||
| 158 | public function setPrefetchCount(int $prefetchCount): ConsumerInterface |
||
| 159 | { |
||
| 160 | $this->prefetchCount = $prefetchCount; |
||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $messageProcessor |
||
| 166 | * @return ConsumerInterface |
||
| 167 | */ |
||
| 168 | public function setMessageProcessor(string $messageProcessor): ConsumerInterface |
||
| 169 | { |
||
| 170 | $this->messageProcessor = $messageProcessor; |
||
| 171 | return $this; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param bool $globalPrefetch |
||
| 176 | * @return ConsumerInterface |
||
| 177 | */ |
||
| 178 | public function setGlobalPrefetch(bool $globalPrefetch): ConsumerInterface |
||
| 179 | { |
||
| 180 | $this->globalPrefetch = $globalPrefetch; |
||
| 181 | |||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return AMQPConnection |
||
| 187 | */ |
||
| 188 | protected function getConnection(): AMQPConnection |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return AMQPChannel |
||
| 195 | */ |
||
| 196 | protected function getChannel(): AMQPChannel |
||
| 197 | { |
||
| 198 | return $this->getConnection()->getChannel(); |
||
|
|
|||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Create the Queue |
||
| 203 | */ |
||
| 204 | public function create() |
||
| 205 | { |
||
| 206 | try { |
||
| 207 | $this->getChannel() |
||
| 208 | ->queue_declare( |
||
| 209 | $this->attributes['name'], |
||
| 210 | $this->attributes['passive'], |
||
| 211 | $this->attributes['durable'], |
||
| 212 | $this->attributes['exclusive'], |
||
| 213 | $this->attributes['auto_delete'], |
||
| 214 | $this->attributes['nowait'], |
||
| 215 | EntityArgumentsInterpreter::interpretArguments( |
||
| 216 | $this->attributes['arguments'] |
||
| 217 | ), |
||
| 218 | $this->attributes['ticket'] |
||
| 219 | ); |
||
| 220 | } catch (AMQPProtocolChannelException $e) { |
||
| 221 | // 406 is a soft error triggered for precondition failure (when redeclaring with different parameters) |
||
| 222 | if (true === $this->attributes['throw_exception_on_redeclare'] || $e->amqp_reply_code !== 406) { |
||
| 223 | throw $e; |
||
| 224 | } |
||
| 225 | // a failure trigger channels closing process |
||
| 226 | $this->reconnect(); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | public function bind() |
||
| 231 | { |
||
| 232 | if (!isset($this->attributes['bind']) || empty($this->attributes['bind'])) { |
||
| 233 | return; |
||
| 234 | } |
||
| 235 | foreach ($this->attributes['bind'] as $bindItem) { |
||
| 236 | try { |
||
| 237 | $this->getChannel() |
||
| 238 | ->queue_bind( |
||
| 239 | $this->attributes['name'], |
||
| 240 | $bindItem['exchange'], |
||
| 241 | $bindItem['routing_key'] ?? '' |
||
| 242 | ); |
||
| 243 | } catch (AMQPProtocolChannelException $e) { |
||
| 244 | // 404 is the code for trying to bind to an non-existing entity |
||
| 245 | if (true === $this->attributes['throw_exception_on_bind_fail'] || $e->amqp_reply_code !== 404) { |
||
| 246 | throw $e; |
||
| 247 | } |
||
| 248 | $this->reconnect(); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Delete the queue |
||
| 255 | */ |
||
| 256 | public function delete() |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | public function reconnect() |
||
| 265 | { |
||
| 266 | $this->getConnection()->reconnect(); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Publish a message |
||
| 271 | * |
||
| 272 | * @param string $message |
||
| 273 | * @param string $routingKey |
||
| 274 | * @param array $properties |
||
| 275 | * @return mixed|void |
||
| 276 | * @throws AMQPProtocolChannelException |
||
| 277 | */ |
||
| 278 | public function publish(string $message, string $routingKey = '', array $properties = []) |
||
| 279 | { |
||
| 280 | if ($this->attributes['auto_create'] === true) { |
||
| 281 | $this->create(); |
||
| 282 | $this->bind(); |
||
| 283 | } |
||
| 284 | |||
| 285 | try { |
||
| 286 | $this->getChannel() |
||
| 287 | ->basic_publish( |
||
| 288 | new AMQPMessage( |
||
| 289 | $message, |
||
| 290 | EntityArgumentsInterpreter::interpretProperties( |
||
| 291 | $this->attributes, |
||
| 292 | $properties |
||
| 293 | ) |
||
| 294 | ), |
||
| 295 | '', |
||
| 296 | $this->attributes['name'], |
||
| 297 | true |
||
| 298 | ); |
||
| 299 | $this->retryCount = 0; |
||
| 300 | } catch (AMQPChannelClosedException $exception) { |
||
| 301 | $this->retryCount++; |
||
| 302 | // Retry publishing with re-connect |
||
| 303 | if ($this->retryCount < self::MAX_RETRIES) { |
||
| 304 | $this->getConnection()->reconnect(); |
||
| 305 | $this->publish($message, $routingKey); |
||
| 306 | |||
| 307 | return; |
||
| 308 | } |
||
| 309 | throw $exception; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | * |
||
| 316 | * @param int $messages |
||
| 317 | * @param int $seconds |
||
| 318 | * @param int $maxMemory |
||
| 319 | * @return int |
||
| 320 | */ |
||
| 321 | public function startConsuming(int $messages, int $seconds, int $maxMemory) |
||
| 322 | { |
||
| 323 | $this->setupConsumer($messages, $seconds, $maxMemory); |
||
| 324 | while (false === $this->shouldStopConsuming()) { |
||
| 325 | try { |
||
| 326 | $this->getChannel()->wait(null, false, $seconds); |
||
| 327 | } catch (AMQPTimeoutException $e) { |
||
| 328 | if ($this->shouldStopConsuming()) { |
||
| 329 | break; |
||
| 330 | } |
||
| 331 | usleep(1000); |
||
| 332 | $this->getConnection()->reconnect(); |
||
| 333 | $this->setupChannelConsumer(); |
||
| 334 | } catch (\Throwable $e) { |
||
| 335 | // stop the consumer |
||
| 336 | $this->stopConsuming(); |
||
| 337 | $this->logger->notice(sprintf( |
||
| 338 | "Stopped consuming: %s in %s:%d", |
||
| 339 | get_class($e) . ' - ' . $e->getMessage(), |
||
| 340 | (string)$e->getFile(), |
||
| 341 | (int)$e->getLine() |
||
| 342 | )); |
||
| 343 | return 1; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | return 0; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | protected function shouldStopConsuming(): bool |
||
| 353 | { |
||
| 354 | if ((microtime(true) - $this->startTime) > $this->limitSecondsUptime) { |
||
| 355 | $this->logger->debug( |
||
| 356 | "Stopped consumer", |
||
| 357 | [ |
||
| 358 | 'limit' => 'time_limit', |
||
| 359 | 'value' => sprintf("%.2f", microtime(true) - $this->startTime) |
||
| 360 | ] |
||
| 361 | ); |
||
| 362 | return true; |
||
| 363 | } |
||
| 364 | if (memory_get_peak_usage(true) >= ($this->limitMemoryConsumption * 1048576)) { |
||
| 365 | $this->logger->debug( |
||
| 366 | "Stopped consumer", |
||
| 367 | [ |
||
| 368 | 'limit' => 'memory_limit', |
||
| 369 | 'value' => (int)round(memory_get_peak_usage(true) / 1048576, 2) |
||
| 370 | ] |
||
| 371 | ); |
||
| 372 | return true; |
||
| 373 | } |
||
| 374 | |||
| 375 | if ($this->getMessageProcessor()->getProcessedMessages() >= $this->limitMessageCount) { |
||
| 376 | $this->logger->debug( |
||
| 377 | "Stopped consumer", |
||
| 378 | ['limit' => 'message_count', 'value' => (int)$this->getMessageProcessor()->getProcessedMessages()] |
||
| 379 | ); |
||
| 380 | return true; |
||
| 381 | } |
||
| 382 | return false; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Stop the consumer |
||
| 387 | */ |
||
| 388 | public function stopConsuming() |
||
| 389 | { |
||
| 390 | try { |
||
| 391 | $this->getChannel()->basic_cancel($this->getConsumerTag(), false, true); |
||
| 392 | } catch (\Throwable $e) { |
||
| 393 | $this->logger->notice("Got " . $e->getMessage() . " of type " . get_class($e)); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Setup the consumer |
||
| 399 | * |
||
| 400 | * @param int $messages |
||
| 401 | * @param int $seconds |
||
| 402 | * @param int $maxMemory |
||
| 403 | */ |
||
| 404 | protected function setupConsumer(int $messages, int $seconds, int $maxMemory) |
||
| 405 | { |
||
| 406 | $this->limitMessageCount = $messages; |
||
| 407 | $this->limitSecondsUptime = $seconds; |
||
| 408 | $this->limitMemoryConsumption = $maxMemory; |
||
| 409 | |||
| 410 | $this->startTime = microtime(true); |
||
| 411 | |||
| 412 | $this->setupChannelConsumer(); |
||
| 413 | |||
| 414 | $this->registerShutdownHandler(); |
||
| 415 | $this->handleKillSignals(); |
||
| 416 | } |
||
| 417 | |||
| 418 | private function setupChannelConsumer() |
||
| 419 | { |
||
| 420 | if ($this->attributes['auto_create'] === true) { |
||
| 421 | $this->create(); |
||
| 422 | $this->bind(); |
||
| 423 | } |
||
| 424 | |||
| 425 | $this->getChannel() |
||
| 426 | ->basic_qos(null, $this->prefetchCount, $this->globalPrefetch); |
||
| 427 | |||
| 428 | $this->getChannel() |
||
| 429 | ->basic_consume( |
||
| 430 | $this->attributes['name'], |
||
| 431 | $this->getConsumerTag(), |
||
| 432 | false, |
||
| 433 | false, |
||
| 434 | false, |
||
| 435 | false, |
||
| 436 | [ |
||
| 437 | $this, |
||
| 438 | 'consume' |
||
| 439 | ] |
||
| 440 | ); |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Handle shutdown - Usually in case "Allowed memory size of x bytes exhausted" |
||
| 445 | */ |
||
| 446 | private function registerShutdownHandler() |
||
| 447 | { |
||
| 448 | $consumer = $this; |
||
| 449 | register_shutdown_function(function () use ($consumer) { |
||
| 450 | $consumer->stopConsuming(); |
||
| 451 | }); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Register signals |
||
| 456 | */ |
||
| 457 | protected function handleKillSignals() |
||
| 458 | { |
||
| 459 | if (extension_loaded('pcntl')) { |
||
| 460 | pcntl_signal(SIGTERM, [$this, 'catchKillSignal']); |
||
| 461 | pcntl_signal(SIGINT, [$this, 'catchKillSignal']); |
||
| 462 | |||
| 463 | if (function_exists('pcntl_signal_dispatch')) { |
||
| 464 | // let the signal go forward |
||
| 465 | pcntl_signal_dispatch(); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Handle Kill Signals |
||
| 472 | * @param int $signalNumber |
||
| 473 | */ |
||
| 474 | public function catchKillSignal(int $signalNumber) |
||
| 475 | { |
||
| 476 | $this->stopConsuming(); |
||
| 477 | $this->logger->debug(sprintf("Caught signal %d", $signalNumber)); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * It is the tag that is listed in RabbitMQ UI as the consumer "name" |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | private function getConsumerTag(): string |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @return MessageProcessorInterface |
||
| 492 | */ |
||
| 493 | private function getMessageProcessor(): MessageProcessorInterface |
||
| 494 | { |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param AMQPMessage $message |
||
| 506 | * @throws \Throwable |
||
| 507 | */ |
||
| 508 | public function consume(AMQPMessage $message) |
||
| 509 | { |
||
| 510 | try { |
||
| 511 | $this->getMessageProcessor()->consume($message); |
||
| 526 | } |
||
| 527 | } |
||
| 528 | } |
||
| 529 |