Completed
Push — master ( 82b02f...a3991b )
by Olivier
03:49
created

QueueManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 4.55 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.94%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 13
c 2
b 0
f 2
lcom 1
cbo 5
dl 4
loc 88
rs 10
ccs 31
cts 33
cp 0.9394

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B define() 0 35 6
B createQueue() 4 15 5
A getModulusName() 0 4 1

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\Manager;
4
5
use Guzzle\Http\Exception\ClientErrorResponseException;
6
use Ola\RabbitMqAdminToolkitBundle\VhostConfiguration;
7
8
class QueueManager extends AbstractManager
9
{
10
    const MODULUS_PLACEHOLDER = '{modulus}';
11
12
    /**
13
     * @var BindingManager
14
     */
15
    private $bindingManager;
16
17
    /**
18
     * @param BindingManager $bindingManager
19
     */
20 4
    public function __construct(BindingManager $bindingManager)
21
    {
22 4
        $this->bindingManager = $bindingManager;
23 4
    }
24
25
    /**
26
     * @param VhostConfiguration $configuration
27
     */
28 4
    public function define(VhostConfiguration $configuration)
29
    {
30 4
        foreach ($configuration->getConfiguration('queues') as $name => $queue) {
31
32 4
            $name = isset($queue['name']) ? $queue['name'] : $name;
33 4
            unset($queue['name']);
34
35 4
            $bindings = $queue['bindings'];
36 4
            unset($queue['bindings']);
37
38 4
            $modulus = $queue['modulus'];
39 4
            unset($queue['modulus']);
40
41 4
            if (null !== $modulus) {
42 1
                for ($i = 0; $i < $modulus; $i++) {
43 1
                    $this->createQueue($configuration, $this->getModulusName($name, $i), $queue);
44
45 1
                    $modulusBindings = $bindings;
46 1
                    foreach ($modulusBindings as $key => $binding) {
47 1
                        $modulusBindings[$key]['routing_key'] = $this->getModulusName($binding['routing_key'], $i);
48
                    }
49
50
                    $this->bindingManager->define(
51
                        $configuration,
52
                        $this->getModulusName($name, $i),
53
                        $modulusBindings
54
                    );
55
                }
56
            } else {
57 4
                $this->createQueue($configuration, $name, $queue);
58
59 4
                $this->bindingManager->define($configuration, $name, $bindings);
60
            }
61
        }
62 3
    }
63
64
    /**
65
     * @param VhostConfiguration $configuration
66
     * @param $name
67
     * @param $queue
68
     */
69 4
    private function createQueue(VhostConfiguration $configuration, $name, $queue)
70
    {
71
        try {
72 4
            $remoteQueue = $configuration->getClient()->queues()->get($configuration->getName(), $name);
73 2
        } catch (ClientErrorResponseException $e) {
74 2
            $this->handleNotFoundException($e);
75
76 2
            $configuration->getClient()->queues()->create($configuration->getName(), $name, $queue);
77
        }
78
79 4 View Code Duplication
        if ($configuration->isDeleteAllowed() && isset($remoteQueue) && !$this->isUpToDate($queue, $remoteQueue)) {
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...
80 1
            $configuration->getClient()->queues()->delete($configuration->getName(), $name);
81 1
            $configuration->getClient()->queues()->create($configuration->getName(), $name, $queue);
82
        }
83 4
    }
84
85
    /**
86
     * @param $name
87
     * @param $modulus
88
     *
89
     * @return string
90
     */
91 1
    private function getModulusName($name, $modulus)
92
    {
93 1
        return str_replace(self::MODULUS_PLACEHOLDER, $modulus, $name);
94
    }
95
}
96