| Conditions | 6 |
| Paths | 2 |
| Total Lines | 61 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 11 | ||
| 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 | |||
| 116 | if (empty($this->onExecutingCallback)) { |
||
| 117 | throw new \Exception("Define a onExecuting callback"); |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->loopConnection(function () use ($sleepSeconds, $queueName) { |
||
| 121 | |||
| 122 | $this->createQueue($queueName); |
||
| 123 | |||
| 124 | $this->channel->basic_qos(null, 1, null); |
||
| 125 | |||
| 126 | $this->channel->basic_consume( |
||
| 127 | $this->queueName, |
||
| 128 | $this->randomConsumer(), |
||
| 129 | false, |
||
| 130 | false, |
||
| 131 | false, |
||
| 132 | false, |
||
| 133 | function (AMQPMessage $message) { |
||
| 134 | pcntl_sigprocmask(SIG_BLOCK, [SIGTERM, SIGINT]); |
||
| 135 | |||
| 136 | |||
| 137 | //se o status for negativo, não executa o consumo |
||
| 138 | $statusBoolean = $this->executeStatusCallback($message); |
||
| 139 | |||
| 140 | if (!$statusBoolean) { |
||
| 141 | return false; |
||
| 142 | } |
||
| 143 | |||
| 144 | $incomeData = json_decode($message->getBody(), true); |
||
| 145 | |||
| 146 | |||
| 147 | $statusBoolean = $this->executeReceiveCallback($message, $incomeData); |
||
| 148 | |||
| 149 | if (!$statusBoolean) { |
||
| 150 | return false; |
||
| 151 | } |
||
| 152 | |||
| 153 | try { |
||
| 154 | $executingCallback = $this->onExecutingCallback; |
||
| 155 | $executingCallback($message, $incomeData); |
||
| 156 | } catch (Exception $e) { |
||
| 157 | $message->nack(true); |
||
| 158 | $this->executeErrorCallback($e, $incomeData); |
||
| 159 | } |
||
| 160 | |||
| 161 | pcntl_sigprocmask(SIG_UNBLOCK, [SIGTERM, SIGINT]); |
||
| 162 | } |
||
| 163 | ); |
||
| 164 | |||
| 165 | // Loop as long as the channel has callbacks registered |
||
| 166 | while ($this->channel->is_open()) { |
||
| 167 | $this->channel->wait(null, false); |
||
| 168 | sleep($sleepSeconds); |
||
| 169 | |||
| 170 | // Despachar eventos de "finalizar" script |
||
| 171 | pcntl_signal_dispatch(); |
||
| 172 | } |
||
| 245 |