Completed
Push — master ( 82b02f...a3991b )
by Olivier
03:49
created

VhostDefineCommand::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ola\RabbitMqAdminToolkitBundle\Command;
4
5
use Ola\RabbitMqAdminToolkitBundle\DependencyInjection\OlaRabbitMqAdminToolkitExtension;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
class VhostDefineCommand extends ContainerAwareCommand
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17
    protected function configure()
18
    {
19
        parent::configure();
20
        $this
21
            ->setName('rabbitmq:vhost:define')
22
            ->setDescription('Create or update a vhost')
23
            ->addArgument('vhost', InputArgument::OPTIONAL, 'Which vhost should be configured ?')
24
        ;
25
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        try {
33
            $vhost = $input->getArgument('vhost');
34
            if (empty($vhost)) {
35
                $vhost = $this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.default_vhost');
36
            }
37
38
            $serviceName = sprintf(
39
                OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE,
40
                $vhost
41
            );
42
43
            $output->write(sprintf('Looking for service [<info>%s</info>]...', $serviceName));
44
45
            if (!$this->getContainer()->has($serviceName)) {
46
                throw new \InvalidArgumentException(sprintf(
47
                    'No service found for vhost : "%s"',
48
                    $vhost
49
                ));
50
            }
51
            $output->writeln(' service found !');
52
53
            $vhostConfiguration = $this->getContainer()->get($serviceName);
54
            $vhostHandler = $this->getContainer()->get('ola_rabbit_mq_admin_toolkit.handler.vhost');
55
            $creation = !$vhostHandler->exists($vhostConfiguration);
56
            $output->write(sprintf('%s vhost configuration...', $creation ? 'Creating' : 'Updating'));
57
58
            $vhostHandler->define($vhostConfiguration);
59
60
            $output->writeln(sprintf(' %s !', $creation ? 'created' : 'updated'));
61
        } catch (\Exception $e) {
62
            if (!$this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')) {
63
                throw $e;
64
            }
65
        }
66
    }
67
}
68