Completed
Pull Request — master (#12)
by Thierry
02:37
created

Consumer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
c 4
b 0
f 0
lcom 1
cbo 3
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 11 1
C execute() 0 24 7
A getConsumerInstance() 0 5 1
1
<?php
2
3
namespace ETNA\Silex\Provider\RabbitMQ;
4
5
use Saxulum\Console\Command\AbstractPimpleCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class Consumer extends AbstractPimpleCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('rabbitmq:consumer')
17
            ->addArgument('name', InputArgument::REQUIRED, 'Consumer Name')
18
            ->addOption('messages', 'm', InputOption::VALUE_OPTIONAL, 'Messages to consume', 0)
19
            ->addOption('route', 'r', InputOption::VALUE_OPTIONAL, 'Routing Key', '')
20
            ->addOption('memory-limit', 'l', InputOption::VALUE_OPTIONAL, 'Allowed memory for this process', null)
21
            ->addOption('debug', 'd', InputOption::VALUE_NONE, 'Enable Debugging')
22
        ;
23
    }
24
25
    /**
26
     * Executes the current command.
27
     *
28
     * @param InputInterface  $input  An InputInterface instance
29
     * @param OutputInterface $output An OutputInterface instance
30
     *
31
     * @return integer|null 0 if everything went fine, or an error code
32
     *
33
     * @throws \InvalidArgumentException When the number of messages to consume is less than 0
34
     * @throws \BadFunctionCallException When the pcntl is not installed and option -s is true
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        if (defined('AMQP_DEBUG') === false) {
39
            define('AMQP_DEBUG', (bool) $input->getOption('debug'));
40
        }
41
42
        $this->amount = $input->getOption('messages');
0 ignored issues
show
Bug introduced by
The property amount does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
44
        if (!is_int($this->amount) || 0 > $this->amount) {
45
            throw new \InvalidArgumentException("The -m option should be null or greater than 0");
46
        }
47
48
        $this->consumer = $this->getConsumerInstance($input);
0 ignored issues
show
Bug introduced by
The property consumer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
50
        if (
51
            !is_null($input->getOption('memory-limit')) &&
52
            ctype_digit((string)$input->getOption('memory-limit')) &&
53
            $input->getOption('memory-limit') > 0
54
        ) {
55
            $this->consumer->setMemoryLimit($input->getOption('memory-limit'));
56
        }
57
        $this->consumer->setRoutingKey($input->getOption('route'));
58
        $this->consumer->consume($this->amount);
59
    }
60
61
    /**
62
     * @param InputInterface $input
63
     */
64
    protected function getConsumerInstance($input)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
65
    {
66
        $app = $this->container;
67
        return $app['rabbit.consumer'][$input->getArgument('name')];
68
    }
69
}
70