| Conditions | 5 |
| Paths | 1 |
| Total Lines | 56 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 12 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 111 | public function consume( |
||
| 112 | string $queueName, |
||
| 113 | int $sleepSeconds = 3 |
||
| 114 | ) { |
||
| 115 | $this->validateExecuteCallback(); |
||
| 116 | |||
| 117 | |||
| 118 | $this->loopConnection(function () use ($sleepSeconds, $queueName) { |
||
| 119 | |||
| 120 | $this->createQueue($queueName); |
||
| 121 | |||
| 122 | $this->channel->basic_qos(null, 1, null); |
||
| 123 | |||
| 124 | $this->channel->basic_consume( |
||
| 125 | $this->queueName, |
||
| 126 | $this->randomConsumer(), |
||
| 127 | false, |
||
| 128 | false, |
||
| 129 | false, |
||
| 130 | false, |
||
| 131 | function (AMQPMessage $message) { |
||
| 132 | pcntl_sigprocmask(SIG_BLOCK, [SIGTERM, SIGINT]); |
||
| 133 | |||
| 134 | $incomeData = json_decode($message->getBody(), true); |
||
| 135 | |||
| 136 | //se o status for negativo, não executa o consumo |
||
| 137 | $statusBoolean = $this->executeStatusCallback($message); |
||
| 138 | |||
| 139 | if (!$statusBoolean) { |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | |||
| 143 | $receiveBoolean = $this->executeReceiveCallback($message, $incomeData); |
||
| 144 | |||
| 145 | if (!$receiveBoolean) { |
||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | try { |
||
| 150 | $this->executeMessage($message, $incomeData); |
||
| 151 | } catch (Exception $e) { |
||
| 152 | $message->nack(true); |
||
| 153 | $this->executeErrorCallback($e, $incomeData); |
||
| 154 | } |
||
| 155 | |||
| 156 | pcntl_sigprocmask(SIG_UNBLOCK, [SIGTERM, SIGINT]); |
||
| 157 | } |
||
| 158 | ); |
||
| 159 | |||
| 160 | // Loop as long as the channel has callbacks registered |
||
| 161 | while ($this->channel->is_open()) { |
||
| 162 | $this->channel->wait(null, false); |
||
| 163 | sleep($sleepSeconds); |
||
| 164 | |||
| 165 | // Despachar eventos de "finalizar" script |
||
| 166 | pcntl_signal_dispatch(); |
||
| 167 | } |
||
| 256 |