Completed
Push — master ( 20bd8e...513dcb )
by Olivier
10s
created

VhostDefineCommand::getVhostList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
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 6
        $vhostList = $this->getVhostList($input);
33
34 6
        foreach ($vhostList as $vhost) {
35
            try {
36 6
                $this->comment($input, $output, sprintf(
37 6
                    'Define rabbitmq <info>%s</info> vhost configuration',
38
                    $vhost
39 6
                ));
40
41 6
                $vhostConfiguration = $this->getVhostConfiguration($vhost);
42 4
                $vhostHandler = $this->getContainer()->get('ola_rabbit_mq_admin_toolkit.handler.vhost');
43 4
                $creation = !$vhostHandler->exists($vhostConfiguration);
44
45 4
                $vhostHandler->define($vhostConfiguration);
46
47 4
                $this->success($input, $output, sprintf(
48 4
                    'Rabbitmq "%s" vhost configuration successfully %s !',
49 4
                    $vhost,
50 5
                    $creation ? 'created' : 'updated'
51 4
                ));
52 6
            } catch (\Exception $e) {
53 2
                if (!$this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')) {
54 1
                    throw $e;
55
                }
56
            }
57 5
        }
58 5
    }
59
60
    /**
61
     * Return Vhosts to process
62
     *
63
     * @param InputInterface $input
64
     *
65
     * @return array
66
     */
67 6
    private function getVhostList(InputInterface $input)
68
    {
69 6
        $inputVhost = $input->getArgument('vhost');
70 6
        $vhostList = array($inputVhost);
71 6
        if (empty($inputVhost)) {
72 4
            $vhostList = $this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.vhost_list');
73 4
        }
74
75 6
        return $vhostList;
76
    }
77
78
    /**
79
     * @param string $vhost
80
     *
81
     * @return VhostConfiguration
82
     *
83
     * @throws \InvalidArgumentException
84
     */
85 6
    private function getVhostConfiguration($vhost)
86
    {
87 6
        $serviceName = sprintf(
88 6
            OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE,
89
            $vhost
90 6
        );
91
92 6
        if (!$this->getContainer()->has($serviceName)) {
93 2
            throw new \InvalidArgumentException(sprintf(
94 2
                'No configuration service found for vhost : "%s"',
95
                $vhost
96 2
            ));
97
        }
98
99 4
        return $this->getContainer()->get($serviceName);
100
    }
101
102
    /**
103
     * @param InputInterface $input
104
     * @param OutputInterface $output
105
     * @param string $message
106
     */
107 6
    private function comment(InputInterface $input, OutputInterface $output, $message)
108
    {
109 6
        $io = $this->getIO($input, $output);
110
111 6
        if (null !== $io && method_exists($io, 'comment')) {
112 6
            $io->comment($message);
113 6
        } else {
114
            $output->writeln(sprintf('<comment>%s</comment>', $message));
115
        }
116 6
    }
117
118
    /**
119
     * @param InputInterface $input
120
     * @param OutputInterface $output
121
     * @param string $message
122
     */
123 4
    private function success(InputInterface $input, OutputInterface $output, $message)
124
    {
125 4
        $io = $this->getIO($input, $output);
126
127 4
        if (null !== $io) {
128 4
            $io->success($message);
129 4
        } else {
130
            $output->writeln(sprintf('<info>%s</info>', $message));
131
        }
132 4
    }
133
134
    /**
135
     * @param InputInterface $input
136
     * @param OutputInterface $output
137
     * @return null|\Symfony\Component\Console\Style\SymfonyStyle
138
     */
139 6
    private function getIO(InputInterface $input, OutputInterface $output)
140
    {
141 6
        if (class_exists('Symfony\Component\Console\Style\SymfonyStyle')) {
142 6
            return new \Symfony\Component\Console\Style\SymfonyStyle($input, $output);
143
        }
144
145
        return null;
146
    }
147
}
148