| Conditions | 5 |
| Paths | 4 |
| Total Lines | 55 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 136 | protected function publishBulk(array $messages, AMQPChannel $channel, string $routingKey = ''): void |
||
| 137 | { |
||
| 138 | if (count($messages) === 0) { |
||
| 139 | throw new RabbitMQException('No messages to publish to the exchange.'); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var RabbitMQExchange[] |
||
| 144 | */ |
||
| 145 | $uniqueExchanges = (new Collection($messages)) |
||
|
|
|||
| 146 | ->unique(function (RabbitMQMessage $message) { |
||
| 147 | return $message->getExchange()->getName(); |
||
| 148 | })->map(function (RabbitMQMessage $message) { |
||
| 149 | return $message->getExchange(); |
||
| 150 | }); |
||
| 151 | |||
| 152 | $uniqueExchanges->each(function (RabbitMQExchange $exchange) use ($channel) { |
||
| 153 | $exchangeConfig = $exchange->getConfig(); |
||
| 154 | |||
| 155 | if ($exchangeConfig->get('declare')) { |
||
| 156 | $channel->exchange_declare( |
||
| 157 | $exchange->getName(), |
||
| 158 | $exchangeConfig->get('type'), |
||
| 159 | $exchangeConfig->get('passive', false), |
||
| 160 | $exchangeConfig->get('durable', true), |
||
| 161 | $exchangeConfig->get('auto_delete', false), |
||
| 162 | $exchangeConfig->get('internal', false), |
||
| 163 | $exchangeConfig->get('nowait', false), |
||
| 164 | (new AMQPTable($exchangeConfig->get('properties', [])))->getNativeData() |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | }); |
||
| 168 | |||
| 169 | $max = $this->maxBatchSize; |
||
| 170 | |||
| 171 | foreach ($messages as $message) { |
||
| 172 | // Queue message for batch publish |
||
| 173 | $channel->batch_basic_publish( |
||
| 174 | new AMQPMessage($message->getStream(), $message->getConfig()->toArray()), |
||
| 175 | $message->getExchange()->getName(), |
||
| 176 | $routingKey, |
||
| 177 | ); |
||
| 178 | |||
| 179 | $batchReadyToBePublished = --$max <= 0; |
||
| 180 | |||
| 181 | if ($batchReadyToBePublished) { |
||
| 182 | // Publish all the messages in the batch |
||
| 183 | $channel->publish_batch(); |
||
| 184 | // Reset batch counter |
||
| 185 | $max = $this->maxBatchSize; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | // Publish all the remaining batches |
||
| 190 | $channel->publish_batch(); |
||
| 191 | } |
||
| 193 |