DeleteManager   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 130
Duplicated Lines 36.92 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 3
cbo 4
dl 48
loc 130
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B deleteQueues() 24 24 5
D deleteExchanges() 0 31 10
B deletePolicies() 24 24 5

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
namespace Metfan\RabbitSetup\Manager\Command;
3
4
use Metfan\RabbitSetup\Manager\RabbitMq\ExchangeManager;
5
use Metfan\RabbitSetup\Manager\RabbitMq\PolicyManager;
6
use Metfan\RabbitSetup\Manager\RabbitMq\QueueManager;
7
use Psr\Log\LoggerInterface;
8
9
10
/**
11
 * Manager of rsetup:delete Command
12
 *
13
 * @author Ulrich
14
 * @package Metfan\RabbitSetup\Manager\Command
15
 */
16
class DeleteManager
17
{
18
    /**
19
     * @var ExchangeManager
20
     */
21
    private $exchangeManager;
22
23
    /**
24
     * @var QueueManager
25
     */
26
    private $queueManager;
27
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * @var PolicyManager
35
     */
36
    private $policyManager;
37
38
    public function __construct(
39
        ExchangeManager $exchangeManager,
40
        QueueManager $queueManager,
41
        PolicyManager $policyManager,
42
        LoggerInterface $logger)
43
    {
44
        $this->exchangeManager = $exchangeManager;
45
        $this->queueManager = $queueManager;
46
        $this->policyManager = $policyManager;
47
        $this->logger = $logger;
48
    }
49
50
    /**
51
     * Delete selected queues.
52
     * If queues == "*" get list of queue from rabbitmq API
53
     *
54
     * @param $vhost
55
     * @param $queues
56
     */
57 View Code Duplication
    public function deleteQueues($vhost, $queues)
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...
58
    {
59
        if ('*' === $queues) {
60
            //so you want to destroy all queues, as you whish, let me time to list' em all
61
            $queues = $this->queueManager->getAll(urlencode($vhost));
62
        } elseif (!$vhost) {
63
            throw new \InvalidArgumentException('If you want to delete certain queue(s) you have to provide vhost');
64
        } else {
65
            //few queues to delete, let stack them in array
66
            $queues = explode(',', $queues);
67
            array_walk($queues, function(&$item, $key, $vhost){
68
                $item = ['vhost' => $vhost, 'name' => trim($item)];
69
            }, $vhost);
70
        }
71
72
        if (0 == count($queues)) {
73
            $this->logger->error('No queue found.');
74
            return;
75
        }
76
77
        foreach ($queues as $queue) {
78
            $this->queueManager->delete(urlencode($queue['vhost']), $queue['name']);
79
        }
80
    }
81
82
    /**
83
     * Delete selected exchanges.
84
     * If exchanges == "*" get list of exchange from rabbitmq API
85
     *
86
     * @param $vhost
87
     * @param $exchanges
88
     */
89
    public function deleteExchanges($vhost, $exchanges)
90
    {
91
        if ('*' === $exchanges) {
92
            //so you want to destroy all exchanges, as you whish, let me time to list'em all
93
            $exchanges = $this->exchangeManager->getAll(urlencode($vhost));
94
            foreach ($exchanges as $key => $exchange) {
95
                if (0 === strpos($exchange['name'], 'amq') || !$exchange['name']) {
96
                    unset($exchanges[$key]);
97
                }
98
            }
99
        } elseif (!$vhost) {
100
            throw new \InvalidArgumentException('If you want to delete certain exchange(s) you have to provide vhost');
101
        } else {
102
            //few exchanges to delete, let stack them in array
103
            $exchanges = explode(',', $exchanges);
104
            array_walk($exchanges, function(&$item, $key, $vhost){
105
                $item = ['vhost' => $vhost, 'name' => trim($item)];
106
            }, $vhost);
107
        }
108
109
        if (0 == count($exchanges)) {
110
            $this->logger->error('No exchange found.');
111
            return;
112
        }
113
114
        foreach ($exchanges as $exchange) {
115
            if (false === strpos($exchange['name'], 'amq') && $exchange['name']) {
116
                $this->exchangeManager->delete(urlencode($exchange['vhost']), $exchange['name']);
117
            }
118
        }
119
    }
120
121 View Code Duplication
    public function deletePolicies($vhost, $policies)
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
        if ('*' === $policies) {
124
            //so you want to destroy all policies, as you whish, let me time to list'em all
125
            $policies = $this->policyManager->getAll(urlencode($vhost));
126
        } elseif (!$vhost) {
127
            throw new \InvalidArgumentException('If you want to delete certain policie(s) you have to provide vhost');
128
        } else {
129
            //few policies to delete, let stack them in array
130
            $policies = explode(',', $policies);
131
            array_walk($policies, function(&$item, $key, $vhost){
132
                $item = ['vhost' => $vhost, 'name' => trim($item)];
133
            }, $vhost);
134
        }
135
136
        if (0 == count($policies)) {
137
            $this->logger->error('No policy found.');
138
            return;
139
        }
140
141
        foreach ($policies as $policy) {
142
            $this->policyManager->delete(urlencode($policy['vhost']), $policy['name']);
143
        }
144
    }
145
}
146