DeleteCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 6
dl 0
loc 74
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 14 1
C execute() 0 45 8
1
<?php
2
namespace Metfan\RabbitSetup\Command;
3
4
use Metfan\RabbitSetup\Container\LoggerProvider;
5
use Metfan\RabbitSetup\Manager\Command\DeleteManager;
6
use Pimple\Container;
7
use Psr\Log\LoggerInterface;
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
 * This command allow to delete queues and exchanges in different way:
17
 * - all queues or exchanges: ./rabbit-setup rsetup:delete --exchanges "*" --queues "*"
18
 * - all queues or exhanges from one vhost: ./rabbit-setup rsetup:delete --exchanges "*" --queues "*" --vhost "myvhots"
19
 * - some queues or exchanges from one vhost ./rabbit-setup rsetup:delete --exchanges "firstExchange" --queues "queue1,queue2" --vhost "myvhots"
20
 *
21
 * @author Ulrich
22
 * @package Metfan\RabbitSetup\Command
23
 */
24
class DeleteCommand extends Command
25
{
26
    /**
27
     * @var Container
28
     */
29
    private $container;
30
31
    public function __construct(Container $container)
32
    {
33
        parent::__construct();
34
        $this->container = $container;
35
    }
36
37
    protected function configure()
38
    {
39
        $this
40
            ->setName('rsetup:delete')
41
            ->setDescription('Delete exchanges and queues from vhosts, deletion can\'t be undo')
42
            ->addArgument('username', InputArgument::OPTIONAL, 'Username to connect RabbitMQ', 'guest')
43
            ->addArgument('password', InputArgument::OPTIONAL, 'Password to connect RabbitMQ', 'guest')
44
            ->addArgument('host', InputArgument::OPTIONAL, 'Host to connect RabbitMQ', '127.0.0.1')
45
            ->addArgument('port', InputArgument::OPTIONAL, 'Port to connect RabbitMQ', 15672)
46
            ->addOption('vhost', 'VH', InputOption::VALUE_REQUIRED, 'Which vhost to use (not mandatory if "*" == queues == exchanges)?', null)
47
            ->addOption('queues', 'Q', InputOption::VALUE_REQUIRED, 'Which queues will be deleted (coma separated, "*" for all)?', null)
48
            ->addOption('exchanges', 'E', InputOption::VALUE_REQUIRED, 'Which exchanges will be deleted(coma separated, "*" for all)?', null)
49
            ->addOption('policies', 'P', InputOption::VALUE_REQUIRED, 'Which policies will be deleted(coma separated, "*" for all)?', null);
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        /**
55
         * set context inside container
56
         */
57
        $this->container->register(new LoggerProvider($output));
58
59
        try {
60
            /** @var LoggerInterface $logger */
61
            $logger = $this->container['logger'];
62
63
            $connection = [
64
                'host' => $input->getArgument('host'),
65
                'port' => $input->getArgument('port'),
66
                'user' => $input->getArgument('username'),
67
                'password' => $input->getArgument('password')];
68
69
            $client = $this->container['curl_client_factory']->createClient($connection);
70
            $queueManager = $this->container['manager_rabbitmq_queue'];
71
            $exchangeManager = $this->container['manager_rabbitmq_exchange'];
72
            $policygeManager = $this->container['manager_rabbitmq_policy'];
73
74
            $manager = new DeleteManager($exchangeManager, $queueManager, $policygeManager, $logger);
75
76
            if ($input->hasOption('queues') && null !== $input->getOption('queues')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77
                $queueManager->setClient($client);
78
                $manager->deleteQueues($input->getOption('vhost'), $input->getOption('queues'));
79
            }
80
81
            if ($input->hasOption('exchanges') && null !== $input->getOption('exchanges')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82
                $exchangeManager->setClient($client);
83
                $manager->deleteExchanges($input->getOption('vhost'), $input->getOption('exchanges'));
84
            }
85
86
            if ($input->hasOption('policies') && null !== $input->getOption('policies')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
87
                $policygeManager->setClient($client);
88
                $manager->deletePolicies($input->getOption('vhost'), $input->getOption('policies'));
89
            }
90
        } catch (\Exception $e) {
91
            $logger->critical($e->getMessage());
92
            return 1;
93
        }
94
95
        return 0;
96
    }
97
}
98