Completed
Push — master ( 613d98...fa04c9 )
by Rémi
20:40
created

DeclareExchangeCommand::execute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 2
1
<?php
2
3
namespace Burrow\CLI;
4
5
use Assert\Assertion;
6
use Burrow\Driver;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * @codeCoverageIgnore
14
 */
15
class DeclareExchangeCommand extends Command
16
{
17
    /** @var Driver */
18
    private $driver;
19
20
    /**
21
     * DeclareQueueCommand constructor.
22
     *
23
     * @param Driver $driver
24
     */
25
    public function __construct(Driver $driver)
26
    {
27
        parent::__construct();
28
29
        $this->driver = $driver;
30
    }
31
32
    protected function configure()
33
    {
34
        $this->setName('admin:declare:exchange')
35
            ->setDescription('Declares an exchange in RabbitMQ.')
36
            ->addArgument(
37
                'name',
38
                InputArgument::REQUIRED,
39
                'The name of the exchange to declare.'
40
            )
41
            ->addArgument(
42
                'type',
43
                InputArgument::OPTIONAL,
44
                'The type of the exchange. Can be any of ' .
45
                '"' . Driver::EXCHANGE_TYPE_DIRECT . '", ' .
46
                '"' . Driver::EXCHANGE_TYPE_TOPIC . '", ' .
47
                '"' . Driver::EXCHANGE_TYPE_FANOUT . '", ' .
48
                '"' . Driver::EXCHANGE_TYPE_HEADERS . '".',
49
                Driver::EXCHANGE_TYPE_FANOUT
50
            );
51
    }
52
53
    /**
54
     * @param InputInterface  $input
55
     * @param OutputInterface $output
56
     *
57
     * @return int|null|void
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $name = $input->getArgument('name');
62
        $type = $input->getArgument('type');
63
        Assertion::choice(
64
            $type,
65
            [
66
                Driver::EXCHANGE_TYPE_DIRECT,
67
                Driver::EXCHANGE_TYPE_TOPIC,
68
                Driver::EXCHANGE_TYPE_FANOUT,
69
                Driver::EXCHANGE_TYPE_HEADERS
70
            ],
71
            'The type of the exchange must be one of the four valid values.'
72
        );
73
74
        $this->driver->declareExchange($name, $type);
75
        $output->writeln(
76
            sprintf(
77
                '<info>Declare exchange <comment>%s</comment> [<comment>%s</comment>]</info>',
78
                $name,
79
                $type
80
            )
81
        );
82
    }
83
}
84