forked from olaurendeau/RabbitMqAdminToolkitBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVhostConfigurationFactory.php
61 lines (48 loc) · 2.1 KB
/
VhostConfigurationFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace Ola\RabbitMqAdminToolkitBundle;
use Ola\RabbitMqAdminToolkitBundle\Exception\ConfigurationNotFoundException;
use Ola\RabbitMqAdminToolkitBundle\Exception\ConnectionNotFoundException;
/**
* This factory will be used by the VhostDefineCommand to create all the VhostConfiguration services.
* Those services used to be created as container definitions at bundle's injection but have been removed so that
* the bundle remains as discrete as possible when non used, and doesn't add unnecessary processing during the
* dependency injection phase, as its purpose is to be used as a one-shot to define the vhosts
*/
class VhostConfigurationFactory
{
private ClientFactory $clientFactory;
private bool $deleteAllowed;
private array $connections;
private array $vhosts;
public function __construct(ClientFactory $clientFactory, bool $deleteAllowed, array $connections, array $vhosts)
{
$this->clientFactory = $clientFactory;
$this->deleteAllowed = $deleteAllowed;
$this->connections = $connections;
$this->vhosts = $vhosts;
}
public function getVhostConfiguration(string $vhostName): VhostConfiguration
{
$vhostConfiguration = $this->vhosts[$vhostName] ?? null;
if (null === $vhostConfiguration) {
throw new ConfigurationNotFoundException(sprintf('No vhost configuration found for vhost %s', $vhostName));
}
$connectionUri = $this->connections[$vhostConfiguration['connection']] ?? null;
if (null === $connectionUri) {
throw new ConnectionNotFoundException(sprintf('No connection found for vhost %s', $vhostName));
}
$parsedUri = parse_url($connectionUri);
return new VhostConfiguration(
$this->clientFactory->getClient(
$parsedUri['scheme'],
$parsedUri['host'],
$parsedUri['user'],
$parsedUri['pass'],
$parsedUri['port'] ?? 80
),
$vhostConfiguration['name'] ?? $vhostName,
$vhostConfiguration,
$this->deleteAllowed
);
}
}