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

QueueManager::getModulusName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 2
crap 1
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