ConfigExpertCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
namespace Metfan\RabbitSetup\Command;
3
4
use Metfan\RabbitSetup\Container\LoggerProvider;
5
use Metfan\RabbitSetup\Manager\Command\ConfigExpertManager;
6
use Metfan\RabbitSetup\Parser\ParseExpertConfig;
7
use Pimple\Container;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
15
/**
16
 * Command to apply config file to RabbitMQ instance
17
 *
18
 * @author Ulrich 
19
 * @package Metfan\RabbitSetup\Command
20
 */
21
class ConfigExpertCommand extends Command
22
{
23
    /**
24
     * @var Container
25
     */
26
    private $container;
27
28
    public function __construct(Container $container)
29
    {
30
        parent::__construct();
31
        $this->container = $container;
32
    }
33
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('rsetup:config:expert')
38
            ->setDescription('Apply configuration file to RabbitMQ')
39
            ->addArgument('configFile', InputArgument::REQUIRED, 'Configuration file')
40
            ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Override user config', null)
41
            ->addOption('password', 'p', InputOption::VALUE_REQUIRED, 'Override password config', null);
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $this->container->register(new LoggerProvider($output));
47
        $this->container['http_client_pool']
48
            ->overrideUser($input->getOption('user'))
49
            ->overridePassword($input->getOption('password'));
50
51
        try {
52
            $logger = $this->container['logger'];
53
            $parser = new ParseExpertConfig();
54
            $config = $parser->parse($input->getArgument('configFile'));
55
56
            $manager = new ConfigExpertManager(
57
                $this->container['http_client_pool'],
58
                $this->container['manager_rabbitmq_vhost'],
59
                $logger);
60
            $manager->manageConfig($config);
61
62
        } catch (\Exception $e) {
63
            $logger->critical($e->getMessage());
64
            return 1;
65
        }
66
67
        return 0;
68
    }
69
}
70