Completed
Push — master ( 393d4d...c94d62 )
by Lusseau
03:46
created

VhostManager::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

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 2
eloc 6
nc 2
nop 4
1
<?php
2
namespace Metfan\RabbitSetup\Manager\RabbitMq;
3
4
use Metfan\RabbitSetup\Http\ClientInterface;
5
use Metfan\RabbitSetup\Http\ClientPool;
6
use Psr\Log\LoggerInterface;
7
8
/**
9
 * Manager in charge of create Exchange and manage exchange, queue under this  vhost
10
 *
11
 * @author Ulrich
12
 * @package Metfan\RabbitSetup\Manager
13
 */
14
class VhostManager
15
{
16
    /**
17
     * @var ParameterManager
18
     */
19
    private $parameterManager;
20
21
    /**
22
     * @var ExchangeManager
23
     */
24
    private $exchangeManager;
25
26
    /**
27
     * @var QueueManager
28
     */
29
    private $queueManager;
30
31
    /**
32
     * @var ClientPool
33
     */
34
    private $clientPool;
35
36
    /**
37
     * @var LoggerInterface
38
     */
39
    private $logger;
40
41
    /**
42
     * @var PolicyManager
43
     */
44
    private $policyManager;
45
46
    /**
47
     * @param ClientPool $clientPool
48
     * @param ParameterManager $parameterManager
49
     * @param ExchangeManager $exchangeManager
50
     * @param QueueManager $queueManager
51
     * @param PolicyManager $policyManager
52
     * @param LoggerInterface $logger
53
     */
54
    public function __construct(
55
        ClientPool $clientPool,
56
        ParameterManager $parameterManager,
57
        ExchangeManager $exchangeManager,
58
        QueueManager $queueManager,
59
        PolicyManager $policyManager,
60
        LoggerInterface $logger)
61
    {
62
63
        $this->parameterManager = $parameterManager;
64
        $this->exchangeManager = $exchangeManager;
65
        $this->queueManager = $queueManager;
66
        $this->clientPool = $clientPool;
67
        $this->logger = $logger;
68
        $this->policyManager = $policyManager;
69
    }
70
71
    /**
72
     * check $configuration, get client related to connection config, create vhost, then process exchange and queue
73
     * @param $vhostName
74
     * @param array $configuration
75
     */
76
    public function processVhost($vhostName, array $configuration)
77
    {
78
        $client = $this->clientPool->getClientByName($configuration['connection']);
79
80
        $vhostName = urlencode($vhostName);
81
82
        $client->query(
83
            ClientInterface::METHOD_PUT,
84
            sprintf('/api/vhosts/%s', $vhostName),
85
            []);
86
        $this->logger->info(sprintf('Create vhost: <info>%s</info>', urldecode($vhostName)));
87
88
        //process parameters
89
        if (isset($configuration['parameters']) && count($configuration['parameters'])) {
90
91
            $this->parameterManager
92
                ->setClient($client)
93
                ->setVhost($vhostName);
94
            foreach ($configuration['parameters'] as $paramType => $parameters) {
95
                foreach ($parameters as $paramName => $paramOptions) {
96
                    $this->parameterManager->create($paramType, $paramName, $paramOptions);
97
                }
98
            }
99
        }
100
101
        //process exchanges
102
        if (isset($configuration['exchanges']) && count($configuration['exchanges'])) {
103
            $this->process($this->exchangeManager, $client, $vhostName, $configuration['exchanges']);
104
        }
105
106
        //process queues
107
        if (isset($configuration['queues']) && count($configuration['queues'])) {
108
            $this->process($this->queueManager, $client, $vhostName, $configuration['queues']);
109
        }
110
111
        //process policies
112
        if (isset($configuration['policies']) && count($configuration['policies'])) {
113
            $this->process($this->policyManager, $client, $vhostName, $configuration['policies']);
114
        }
115
    }
116
117
    private function process($manager, $client, $vhostName, $elements)
118
    {
119
        $manager
120
            ->setClient($client)
121
            ->setVhost($vhostName);
122
        foreach ($elements as $name => $element) {
123
            $manager->create($name, $element);
124
        }
125
    }
126
}
127