Completed
Pull Request — master (#8)
by Olivier
10:02
created

VhostDefineCommand::getVhostList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ola\RabbitMqAdminToolkitBundle\Command;
4
5
use Ola\RabbitMqAdminToolkitBundle\DependencyInjection\OlaRabbitMqAdminToolkitExtension;
6
use Ola\RabbitMqAdminToolkitBundle\VhostConfiguration;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class VhostDefineCommand extends ContainerAwareCommand
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17 6
    protected function configure()
18
    {
19 6
        parent::configure();
20 6
        $this
21 6
            ->setName('rabbitmq:vhost:define')
22 6
            ->setDescription('Create or update a vhost')
23 6
            ->addArgument('vhost', InputArgument::OPTIONAL, 'Which vhost should be configured ?')
24
        ;
25 6
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30 6
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        $vhostList = $this->getVhostList($input);
33 6
34 6
        foreach ($vhostList as $vhost) {
35 6
            try {
36 6
                $this->comment($input, $output, sprintf(
37
                    'Define rabbitmq <info>%s</info> vhost configuration',
38 6
                    $vhost
39 4
                ));
40 4
41
                $vhostConfiguration = $this->getVhostConfiguration($vhost);
42 4
                $vhostHandler = $this->getContainer()->get('ola_rabbit_mq_admin_toolkit.handler.vhost');
43
                $creation = !$vhostHandler->exists($vhostConfiguration);
44 4
45 4
                $vhostHandler->define($vhostConfiguration);
46 4
47 4
                $this->success($input, $output, sprintf(
48 4
                    'Rabbitmq "%s" vhost configuration successfully %s !',
49 6
                    $vhost,
50 2
                    $creation ? 'created' : 'updated'
51 1
                ));
52
            } catch (\Exception $e) {
53
                if (!$this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')) {
54 5
                    throw $e;
55
                }
56
            }
57
        }
58
    }
59
60
    /**
61
     * Return Vhosts to process
62
     *
63 6
     * @param InputInterface $input
64
     *
65 6
     * @return array
66 6
     */
67 4
    private function getVhostList(InputInterface $input)
68 4
    {
69
        $vhostList = array($input->getArgument('vhost'));
70 6
        if (empty($input->getArgument('vhost'))) {
71
            $vhostList = $this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.vhost_list');
72
        }
73
74
        return $vhostList;
75
    }
76
77
    /**
78
     * @param string $vhost
79
     *
80
     * @return VhostConfiguration
81 6
     *
82
     * @throws \InvalidArgumentException
83 6
     */
84
    private function getVhostConfiguration($vhost)
85 6
    {
86 6
        $serviceName = sprintf(
87
            OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE,
88 6
            $vhost
89
        );
90 6
91 2
        if (!$this->getContainer()->has($serviceName)) {
92 2
            throw new \InvalidArgumentException(sprintf(
93
                'No configuration service found for vhost : "%s"',
94 2
                $vhost
95
            ));
96
        }
97 4
98
        return $this->getContainer()->get($serviceName);
99
    }
100
101
    /**
102
     * @param InputInterface $input
103
     * @param OutputInterface $output
104
     * @param string $message
105 6
     */
106
    private function comment(InputInterface $input, OutputInterface $output, $message)
107 6
    {
108
        $io = $this->getIO($input, $output);
109 6
110 6
        if (null !== $io && method_exists($io, 'comment')) {
111 6
            $io->comment($message);
112
        } else {
113
            $output->writeln(sprintf('<comment>%s</comment>', $message));
114 6
        }
115
    }
116
117
    /**
118
     * @param InputInterface $input
119
     * @param OutputInterface $output
120
     * @param string $message
121 4
     */
122
    private function success(InputInterface $input, OutputInterface $output, $message)
123 4
    {
124
        $io = $this->getIO($input, $output);
125 4
126 4
        if (null !== $io) {
127 4
            $io->success($message);
128
        } else {
129
            $output->writeln(sprintf('<info>%s</info>', $message));
130 4
        }
131
    }
132
133
    /**
134
     * @param InputInterface $input
135
     * @param OutputInterface $output
136
     * @return null|\Symfony\Component\Console\Style\SymfonyStyle
137 6
     */
138
    private function getIO(InputInterface $input, OutputInterface $output)
139 6
    {
140 6
        if (class_exists('Symfony\Component\Console\Style\SymfonyStyle')) {
141
            return new \Symfony\Component\Console\Style\SymfonyStyle($input, $output);
142
        }
143
144
        return null;
145
    }
146
}
147