Passed
Push — master ( 441efe...590e53 )
by Ramūnas
02:30
created

BaseConsumerCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 20.93%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 97
ccs 9
cts 43
cp 0.2093
rs 10
c 7
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 11 1
A initConsumer() 0 12 3
A execute() 0 23 6
A stopConsumer() 0 10 3
A restartConsumer() 0 2 1
A initialize() 0 5 2
1
<?php
2
3
namespace OldSound\RabbitMqBundle\Command;
4
5
use OldSound\RabbitMqBundle\RabbitMq\BaseConsumer as Consumer;
6
use PhpAmqpLib\Exception\AMQPTimeoutException;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
abstract class BaseConsumerCommand extends BaseRabbitMqCommand
13
{
14
    protected $consumer;
15
16
    /** @var int */
17
    protected $amount;
18
19
    abstract protected function getConsumerService();
20
21
    public function stopConsumer()
22
    {
23
        if ($this->consumer instanceof Consumer) {
24
            // Process current message, then halt consumer
25
            $this->consumer->forceStopConsumer();
26
27
            // Halt consumer if waiting for a new message from the queue
28
            try {
29
                $this->consumer->stopConsuming();
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
34
    public function restartConsumer()
35
    {
36
        // TODO: Implement restarting of consumer
37
    }
38
39 3
    protected function configure()
40
    {
41 3
        parent::configure();
42
43
        $this
44 3
            ->addArgument('name', InputArgument::REQUIRED, 'Consumer Name')
45 3
            ->addOption('messages', 'm', InputOption::VALUE_OPTIONAL, 'Messages to consume', '0')
46 3
            ->addOption('route', 'r', InputOption::VALUE_OPTIONAL, 'Routing Key', '')
47 3
            ->addOption('memory-limit', 'l', InputOption::VALUE_OPTIONAL, 'Allowed memory for this process (MB)', null)
48 3
            ->addOption('debug', 'd', InputOption::VALUE_NONE, 'Enable Debugging')
49 3
            ->addOption('without-signals', 'w', InputOption::VALUE_NONE, 'Disable catching of system signals')
50
        ;
51 3
    }
52
53
    protected function initialize(InputInterface $input, OutputInterface $output)
54
    {
55
        $this->amount = (int)$input->getOption('messages');
56
        if (0 > $this->amount) {
57
            throw new \InvalidArgumentException("The -m option should be null or greater than 0");
58
        }
59
    }
60
61
    /**
62
     * Executes the current command.
63
     *
64
     * @param InputInterface  $input  An InputInterface instance
65
     * @param OutputInterface $output An OutputInterface instance
66
     *
67
     * @return integer 0 if everything went fine, or an error code
68
     *
69
     * @throws \InvalidArgumentException When the number of messages to consume is less than 0
70
     * @throws \BadFunctionCallException When the pcntl is not installed and option -s is true
71
     */
72
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        if (defined('AMQP_WITHOUT_SIGNALS') === false) {
75
            define('AMQP_WITHOUT_SIGNALS', $input->getOption('without-signals'));
76
        }
77
78
        if (!AMQP_WITHOUT_SIGNALS && extension_loaded('pcntl')) {
79
            if (!function_exists('pcntl_signal')) {
80
                throw new \BadFunctionCallException("Function 'pcntl_signal' is referenced in the php.ini 'disable_functions' and can't be called.");
81
            }
82
83
            pcntl_signal(SIGTERM, array(&$this, 'stopConsumer'));
84
            pcntl_signal(SIGINT, array(&$this, 'stopConsumer'));
85
            pcntl_signal(SIGHUP, array(&$this, 'restartConsumer'));
86
        }
87
88
        if (defined('AMQP_DEBUG') === false) {
89
            define('AMQP_DEBUG', (bool) $input->getOption('debug'));
90
        }
91
92
        $this->initConsumer($input);
93
94
        return $this->consumer->consume($this->amount);
0 ignored issues
show
Bug introduced by
The method consume() does not exist on null. ( Ignorable by Annotation )

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

94
        return $this->consumer->/** @scrutinizer ignore-call */ consume($this->amount);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
    }
96
97
    protected function initConsumer(InputInterface $input)
98
    {
99
        $this->consumer = $this->getContainer()
100
                ->get(sprintf($this->getConsumerService(), $input->getArgument('name')));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('name') can also be of type string[]; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

100
                ->get(sprintf($this->getConsumerService(), /** @scrutinizer ignore-type */ $input->getArgument('name')));
Loading history...
101
102
        if ($input->hasOption('memory-limit')) {
103
            $memoryLimit = (int)$input->getOption('memory-limit');
104
            if ($memoryLimit > 0) {
105
                $this->consumer->setMemoryLimit($memoryLimit);
106
            }
107
        }
108
        $this->consumer->setRoutingKey($input->getOption('route'));
109
    }
110
}
111