Conditions | 7 |
Paths | 28 |
Total Lines | 104 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
195 | public function consume(): int |
||
196 | { |
||
197 | $qos = $this->config['qos']; |
||
198 | $receiveTimeout = (int) ($this->config['receive_timeout'] * 1e3); |
||
199 | |||
200 | $this->logger->debug( |
||
201 | vsprintf( |
||
202 | 'Consumer "%s" start consuming queue "%s" ("%s")', |
||
203 | [$this->id(), $this->queue->id(), $this->queue->name()] |
||
204 | ) |
||
205 | ); |
||
206 | |||
207 | $this->prepare(); |
||
208 | |||
209 | $context = $this->queue->context(true); |
||
210 | |||
211 | if ($qos['enabled']) { |
||
212 | $context->setQos((int) $qos['prefetch_size'], (int) $qos['prefetch_count'], (bool) $qos['global']); |
||
213 | } |
||
214 | |||
215 | $consumer = $context->createConsumer($this->queue->model()); |
||
216 | |||
217 | /** |
||
218 | * @var Amqp\AmqpSubscriptionConsumer $subscription |
||
219 | */ |
||
220 | $subscription = $context->createSubscriptionConsumer(); |
||
221 | |||
222 | /* |
||
223 | |-------------------------------------------------------------------------- |
||
224 | | Consumer tag |
||
225 | |-------------------------------------------------------------------------- |
||
226 | | |
||
227 | */ |
||
228 | $consumer->setConsumerTag($this->tag); |
||
229 | |||
230 | /* |
||
231 | |-------------------------------------------------------------------------- |
||
232 | | Mounting the processor. |
||
233 | |-------------------------------------------------------------------------- |
||
234 | | |
||
235 | */ |
||
236 | $this->processor->mount($this->queue, $context, $consumer, $this->container, $this->logger); |
||
237 | |||
238 | /* |
||
239 | |-------------------------------------------------------------------------- |
||
240 | | Message Receiver |
||
241 | |-------------------------------------------------------------------------- |
||
242 | | |
||
243 | */ |
||
244 | $subscription->subscribe( |
||
245 | $consumer, |
||
246 | function ($message, $consumer) use ($context) { |
||
247 | return $this->handle($message, $consumer, $context); |
||
248 | } |
||
249 | ); |
||
250 | |||
251 | $startTime = microtime(true); |
||
252 | |||
253 | /* |
||
254 | |-------------------------------------------------------------------------- |
||
255 | | Start Event |
||
256 | |-------------------------------------------------------------------------- |
||
257 | | |
||
258 | */ |
||
259 | $startEvent = new Event\Consumer\Start($this->queue, $context, $startTime); |
||
|
|||
260 | |||
261 | $this->dispatcher->dispatch($startEvent, Event\Consumer\Start::name()); |
||
262 | |||
263 | if ($startEvent->isExecutionInterrupted()) { |
||
264 | $this->end($this->queue, $context, $startTime, $startEvent->exitStatus(), $subscription); |
||
265 | |||
266 | return 0; |
||
267 | } |
||
268 | |||
269 | while (true) { |
||
270 | try { |
||
271 | $subscription->consume($receiveTimeout); |
||
272 | |||
273 | /* |
||
274 | |-------------------------------------------------------------------------- |
||
275 | | Idle Event. |
||
276 | |-------------------------------------------------------------------------- |
||
277 | | |
||
278 | */ |
||
279 | $idleEvent = new Event\Consumer\Idle($this->queue, $context); |
||
280 | |||
281 | $this->dispatcher->dispatch($idleEvent, Event\Consumer\Idle::name()); |
||
282 | |||
283 | if ($idleEvent->isExecutionInterrupted()) { |
||
284 | $this->end($this->queue, $context, $startTime, $idleEvent->exitStatus(), $subscription); |
||
285 | break; |
||
286 | } |
||
287 | // |
||
288 | } catch (StopConsumerException $exception) { |
||
289 | $this->end($this->queue, $context, $startTime, null, $subscription); |
||
290 | break; |
||
291 | } catch (Throwable $exception) { |
||
292 | $this->end($this->queue, $context, $startTime, 0, $subscription); |
||
293 | throw $exception; |
||
294 | }//end try |
||
295 | //end try |
||
296 | }//end while |
||
297 | |||
298 | return 0; |
||
299 | } |
||
416 |