Completed
Push — master ( 65fb1b...623f3e )
by Olivier
06:32
created

VhostDefineCommand::getIO()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 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
    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
            $this->comment($input, $output, sprintf(
34
                'Define rabbitmq <info>%s</info> vhost configuration',
35
                $this->getVhost($input)
36
            ));
37
38
            $vhostConfiguration = $this->getVhostConfiguration($input, $output);
39
            $vhostHandler = $this->getContainer()->get('ola_rabbit_mq_admin_toolkit.handler.vhost');
40
            $creation = !$vhostHandler->exists($vhostConfiguration);
41
42
            $vhostHandler->define($vhostConfiguration);
43
44
            $this->success($input, $output, sprintf(
45
                'Rabbitmq "%s" vhost configuration successfully %s !',
46
                $this->getVhost($input),
47
                $creation ? 'created' : 'updated'
48
            ));
49
        } catch (\Exception $e) {
50
            if (!$this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')) {
51
                throw $e;
52
            }
53
        }
54
    }
55
56
    /**
57
     * Retrieve vhost's name to process
58
     *
59
     * @param InputInterface $input
60
     *
61
     * @return string
62
     */
63
    private function getVhost(InputInterface $input)
64
    {
65
        $vhost = $input->getArgument('vhost');
66
        if (empty($vhost)) {
67
            $vhost = $this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.default_vhost');
68
        }
69
70
        return $vhost;
71
    }
72
73
    /**
74
     * @param InputInterface $input
75
     * @param OutputInterface $output
76
     *
77
     * @return VhostConfiguration
78
     *
79
     * @throws \InvalidArgumentException
80
     */
81
    private function getVhostConfiguration(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $vhost = $this->getVhost($input);
84
85
        $serviceName = sprintf(
86
            OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE,
87
            $vhost
88
        );
89
90
        if (!$this->getContainer()->has($serviceName)) {
91
            throw new \InvalidArgumentException(sprintf(
92
                'No configuration service found for vhost : "%s"',
93
                $vhost
94
            ));
95
        }
96
97
        return $this->getContainer()->get($serviceName);
98
    }
99
100
    /**
101
     * @param InputInterface $input
102
     * @param OutputInterface $output
103
     * @param $message
104
     */
105 View Code Duplication
    private function comment(InputInterface $input, OutputInterface $output, $message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $io = $this->getIO($input, $output);
108
109
        if (null !== $io) {
110
            $io->comment($message);
111
        } else {
112
            $output->writeln(sprintf('<comment>%s</comment>', $message));
113
        }
114
    }
115
116
    /**
117
     * @param InputInterface $input
118
     * @param OutputInterface $output
119
     * @param $message
120
     */
121 View Code Duplication
    private function success(InputInterface $input, OutputInterface $output, $message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123
        $io = $this->getIO($input, $output);
124
125
        if (null !== $io) {
126
            $io->success($message);
127
        } else {
128
            $output->writeln(sprintf('<info>%s</info>', $message));
129
        }
130
    }
131
132
    /**
133
     * @param InputInterface $input
134
     * @param OutputInterface $output
135
     * @return null|\Symfony\Component\Console\Style\SymfonyStyle
136
     */
137
    private function getIO(InputInterface $input, OutputInterface $output)
138
    {
139
        if(class_exists('Symfony\Component\Console\Style\SymfonyStyle')) {
140
            return new \Symfony\Component\Console\Style\SymfonyStyle($input, $output);
141
        }
142
143
        return null;
144
    }
145
}
146