Completed
Push — master ( 5079eb...82b02f )
by Olivier
03:59
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\Component\Console\Command\Command;
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\ContainerAwareInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
class VhostDefineCommand extends Command implements ContainerAwareInterface
14
{
15
    /**
16
     * @var ContainerInterface
17
     */
18
    private $container;
19
20
    protected function configure()
21
    {
22
        parent::configure();
23
        $this
24
            ->setName('rabbitmq:vhost:define')
25
            ->setDescription('Create or update a vhost')
26
            ->addArgument('vhost', InputArgument::OPTIONAL, 'Which vhost should be configured ?')
27
        ;
28
    }
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        try {
32
            $vhost = $input->getArgument('vhost');
33
            if (empty($vhost)) {
34
                $vhost = $this->container->getParameter('ola_rabbit_mq_admin_toolkit.default_vhost');
35
            }
36
37
            $serviceName = sprintf(
38
                OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE,
39
                $vhost
40
            );
41
42
            $output->write(sprintf('Looking for service [<info>%s</info>]...', $serviceName));
43
44
            if (!$this->container->has($serviceName)) {
45
                throw new \InvalidArgumentException(sprintf(
46
                    'No service found for vhost : "%s"',
47
                    $vhost
48
                ));
49
            }
50
            $output->writeln(' service found !');
51
52
            $vhostConfiguration = $this->container->get($serviceName);
53
            $vhostHandler = $this->container->get('ola_rabbit_mq_admin_toolkit.handler.vhost');
54
            $creation = !$vhostHandler->exists($vhostConfiguration);
55
            $output->write(sprintf('%s vhost configuration...', $creation ? 'Creating' : 'Updating'));
56
57
            $vhostHandler->define($vhostConfiguration);
58
59
            $output->writeln(sprintf(' %s !', $creation ? 'created' : 'updated'));
60
        } catch (\Exception $e) {
61
            if (!$this->container->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')) {
62
                throw $e;
63
            }
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function setContainer(ContainerInterface $container = null)
71
    {
72
        $this->container = $container;
73
    }
74
}
75