Completed
Push — master ( fea95a...cb6bda )
by Olivier
06:35
created

VhostDefineCommand::getVhostConfiguration()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 14
nc 4
nop 2
1
<?php
2
3
namespace Ola\RabbitMqAdminToolkitBundle\Command;
4
5
use Ola\RabbitMqAdminToolkitBundle\DependencyInjection\OlaRabbitMqAdminToolkitExtension;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
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
            $vhostConfiguration = $this->getVhostConfiguration($input, $output);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $vhostConfiguration is correct as $this->getVhostConfiguration($input, $output) (which targets Ola\RabbitMqAdminToolkit...getVhostConfiguration()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34
            $vhostHandler = $this->getContainer()->get('ola_rabbit_mq_admin_toolkit.handler.vhost');
35
            $creation = !$vhostHandler->exists($vhostConfiguration);
36
            $output->write(sprintf('%s vhost configuration...', $creation ? 'Creating' : 'Updating'));
37
38
            $vhostHandler->define($vhostConfiguration);
39
40
            $output->writeln(sprintf(' %s !', $creation ? 'created' : 'updated'));
41
        } catch (\Exception $e) {
42
            if (!$this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')) {
43
                throw $e;
44
            }
45
        }
46
    }
47
48
    /**
49
     * @param InputInterface $input
50
     * @param OutputInterface $output
51
     */
52
    private function getVhostConfiguration(InputInterface $input, OutputInterface $output)
53
    {
54
        $vhost = $input->getArgument('vhost');
55
        if (empty($vhost)) {
56
            $vhost = $this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.default_vhost');
57
        }
58
59
        $serviceName = sprintf(
60
            OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE,
61
            $vhost
62
        );
63
64
        $output->write(sprintf('Looking for service [<info>%s</info>]...', $serviceName));
65
66
        if (!$this->getContainer()->has($serviceName)) {
67
            throw new \InvalidArgumentException(sprintf(
68
                'No configuration service found for vhost : "%s"',
69
                $vhost
70
            ));
71
        }
72
        $output->writeln(' service found !');
73
74
        $this->getContainer()->get($serviceName);
75
    }
76
}
77