Completed
Push — master ( 791473...20bd8e )
by Olivier
05:18
created

VhostDefineCommand::getVhostConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
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
        try {
33 6
            $this->comment($input, $output, sprintf(
34 6
                'Define rabbitmq <info>%s</info> vhost configuration',
35 6
                $this->getVhost($input)
36 6
            ));
37
38 6
            $vhostConfiguration = $this->getVhostConfiguration($input);
39 4
            $vhostHandler = $this->getContainer()->get('ola_rabbit_mq_admin_toolkit.handler.vhost');
40 4
            $creation = !$vhostHandler->exists($vhostConfiguration);
41
42 4
            $vhostHandler->define($vhostConfiguration);
43
44 4
            $this->success($input, $output, sprintf(
45 4
                'Rabbitmq "%s" vhost configuration successfully %s !',
46 4
                $this->getVhost($input),
47 4
                $creation ? 'created' : 'updated'
48 4
            ));
49 6
        } catch (\Exception $e) {
50 2
            if (!$this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')) {
51 1
                throw $e;
52
            }
53
        }
54 5
    }
55
56
    /**
57
     * Retrieve vhost's name to process
58
     *
59
     * @param InputInterface $input
60
     *
61
     * @return string
62
     */
63 6
    private function getVhost(InputInterface $input)
64
    {
65 6
        $vhost = $input->getArgument('vhost');
66 6
        if (empty($vhost)) {
67 4
            $vhost = $this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.default_vhost');
68 4
        }
69
70 6
        return $vhost;
71
    }
72
73
    /**
74
     * @param InputInterface $input
75
     * @param OutputInterface $output
0 ignored issues
show
Bug introduced by
There is no parameter named $output. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
     *
77
     * @return VhostConfiguration
78
     *
79
     * @throws \InvalidArgumentException
80
     */
81 6
    private function getVhostConfiguration(InputInterface $input)
82
    {
83 6
        $vhost = $this->getVhost($input);
84
85 6
        $serviceName = sprintf(
86 6
            OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE,
87
            $vhost
88 6
        );
89
90 6
        if (!$this->getContainer()->has($serviceName)) {
91 2
            throw new \InvalidArgumentException(sprintf(
92 2
                'No configuration service found for vhost : "%s"',
93
                $vhost
94 2
            ));
95
        }
96
97 4
        return $this->getContainer()->get($serviceName);
98
    }
99
100
    /**
101
     * @param InputInterface $input
102
     * @param OutputInterface $output
103
     * @param string $message
104
     */
105 6
    private function comment(InputInterface $input, OutputInterface $output, $message)
106
    {
107 6
        $io = $this->getIO($input, $output);
108
109 6
        if (null !== $io && method_exists($io, 'comment')) {
110 6
            $io->comment($message);
111 6
        } else {
112
            $output->writeln(sprintf('<comment>%s</comment>', $message));
113
        }
114 6
    }
115
116
    /**
117
     * @param InputInterface $input
118
     * @param OutputInterface $output
119
     * @param string $message
120
     */
121 4
    private function success(InputInterface $input, OutputInterface $output, $message)
122
    {
123 4
        $io = $this->getIO($input, $output);
124
125 4
        if (null !== $io) {
126 4
            $io->success($message);
127 4
        } else {
128
            $output->writeln(sprintf('<info>%s</info>', $message));
129
        }
130 4
    }
131
132
    /**
133
     * @param InputInterface $input
134
     * @param OutputInterface $output
135
     * @return null|\Symfony\Component\Console\Style\SymfonyStyle
136
     */
137 6
    private function getIO(InputInterface $input, OutputInterface $output)
138
    {
139 6
        if (class_exists('Symfony\Component\Console\Style\SymfonyStyle')) {
140 6
            return new \Symfony\Component\Console\Style\SymfonyStyle($input, $output);
141
        }
142
143
        return null;
144
    }
145
}
146