Test Failed
Pull Request — master (#39)
by Aleksandr
06:37
created

StdInProducerCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
3
namespace OldSound\RabbitMqBundle\Command;
4
5
use OldSound\RabbitMqBundle\RabbitMq\Producer;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Exception\InvalidArgumentException;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
13
14
class StdInProducerCommand extends Command
15
{
16
    use ContainerAwareTrait;
17
18
    const FORMAT_PHP = 'php';
19
    const FORMAT_RAW = 'raw';
20
21
    protected function configure()
22
    {
23
        parent::configure();
24
25
        $this
26
            ->setName('rabbitmq:stdin-producer')
27
            ->addArgument('name', InputArgument::REQUIRED, 'Producer Name')
28
            ->setDescription('Executes a producer that reads data from STDIN')
29
            ->addOption('route', 'r', InputOption::VALUE_OPTIONAL, 'Routing Key', '')
30
            ->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Payload Format', self::FORMAT_PHP)
31
            ->addOption('debug', 'd', InputOption::VALUE_OPTIONAL, 'Enable Debugging', false)
32
        ;
33
    }
34
35
    /**
36
     * Executes the current command.
37
     *
38
     * @param InputInterface  $input  An InputInterface instance
39
     * @param OutputInterface $output An OutputInterface instance
40
     *
41
     * @return integer 0 if everything went fine, or an error code
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        define('AMQP_DEBUG', (bool) $input->getOption('debug'));
46
47
        $producerName = $input->getArgument('name');
48
        $alias = sprintf('old_sound_rabbit_mq.producer.%s', $producerName);
0 ignored issues
show
Bug introduced by
It seems like $producerName can also be of type string[]; however, parameter $args of sprintf() does only seem to accept 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

48
        $alias = sprintf('old_sound_rabbit_mq.producer.%s', /** @scrutinizer ignore-type */ $producerName);
Loading history...
49
        if (!$this->container->has($alias)) {
50
            $producerNames = []; // TODO $this->container->getParameter('old_sound_rabbit_mq.allowed_producer_names');
51
            throw new InvalidArgumentException(sprintf('Producer %s is undefined. Allowed ones: %s', $producerName, join(', ', $producerNames)));
52
        }
53
54
        /** @var Producer $producer */
55
        $producer = $this->container->get($alias);
56
57
        $data = '';
58
        while (!feof(STDIN)) {
59
            $data .= fread(STDIN, 8192);
60
        }
61
62
        $route = $input->getOption('route');
63
        $format = $input->getOption('format');
64
65
        switch ($format) {
66
            case self::FORMAT_RAW:
67
                break; // data as is
68
            case self::FORMAT_PHP:
69
                $data = serialize($data);
70
                break;
71
            default:
72
                throw new \InvalidArgumentException(sprintf('Invalid payload format "%s", expecting one of: %s.',
73
                    $format, implode(', ', array(self::FORMAT_PHP, self::FORMAT_RAW))));
74
        }
75
76
        $producer->publish($data, $route);
77
78
        return 0;
79
    }
80
}
81