Completed
Pull Request — master (#39)
by Aleksandr
06:46
created

PcntlSignalDispatchSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 5 1
A afterProcessingMessages() 0 3 1
A __construct() 0 13 2
A onConsume() 0 3 1
1
<?php
2
3
4
namespace OldSound\RabbitMqBundle\EventListener;
5
6
use OldSound\RabbitMqBundle\Event\AfterProcessingMessagesEvent;
7
use OldSound\RabbitMqBundle\Event\OnConsumeEvent;
8
use OldSound\RabbitMqBundle\RabbitMq\Consumer;
9
use PhpAmqpLib\Exception\AMQPTimeoutException;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
class PcntlSignalDispatchSubscriber implements EventSubscriberInterface
13
{
14
    public static function getSubscribedEvents()
15
    {
16
        return [
17
            OnConsumeEvent::NAME => 'onConsume',
18
            AfterProcessingMessagesEvent::NAME => 'afterProcessingMessages'
19
        ];
20
    }
21
22
    public function __construct(Consumer $consumer)
23
    {
24
        $stopConsumer = function () use ($consumer) {
25
            // Process current message, then halt consumer
26
            $consumer->forceStopConsumer();
27
            // Halt consumer if waiting for a new message from the queue
28
            try {
29
                $consumer->stopConsuming(true);
30
            } catch (AMQPTimeoutException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
31
        };
32
33
        pcntl_signal(SIGTERM, $stopConsumer);
34
        pcntl_signal(SIGINT, $stopConsumer);
35
        // TODO pcntl_signal(SIGHUP, $restartConsumer);
36
    }
37
38
    public function onConsume(OnConsumeEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
    public function onConsume(/** @scrutinizer ignore-unused */ OnConsumeEvent $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        pcntl_signal_dispatch();
41
    }
42
43
    public function afterProcessingMessages(AfterProcessingMessagesEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
    public function afterProcessingMessages(/** @scrutinizer ignore-unused */ AfterProcessingMessagesEvent $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        pcntl_signal_dispatch();
46
    }
47
}