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

VhostDefineCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 134
Duplicated Lines 14.93 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 15
c 5
b 0
f 3
lcom 1
cbo 6
dl 20
loc 134
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
B execute() 0 25 4
A getVhost() 0 9 2
A getVhostConfiguration() 0 18 2
A comment() 10 10 2
A success() 10 10 2
A getIO() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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