Completed
Push — master ( 564a11...d71bb8 )
by Alessandro
02:58
created

ClientRegistry::buildConnectionUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace Facile\MongoDbBundle\Services;
4
5
use Facile\MongoDbBundle\Capsule\Client as BundleClient;
6
use Facile\MongoDbBundle\Event\ConnectionEvent;
7
use Facile\MongoDbBundle\Models\ClientConfiguration;
8
use MongoDB\Client;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
11
/**
12
 * Class ClientRegistry.
13
 * @internal
14
 */
15
final class ClientRegistry
16
{
17
    /** @var Client[] */
18
    private $clients;
19
    /** @var ClientConfiguration[] */
20
    private $configurations;
21
    /** @var string */
22
    private $environment;
23
    /** @var EventDispatcherInterface */
24
    private $eventDispatcher;
25
26
    /**
27
     * ClientRegistry constructor.
28
     *
29
     * @param EventDispatcherInterface $eventDispatcher
30
     * @param string                   $environment
31
     *
32
     * @internal param DataCollectorLoggerInterface $logger
33
     */
34 12
    public function __construct(EventDispatcherInterface $eventDispatcher, string $environment)
35
    {
36 12
        $this->clients = [];
37 12
        $this->configurations = [];
38 12
        $this->environment = $environment;
39 12
        $this->eventDispatcher = $eventDispatcher;
40 12
    }
41
42
    /**
43
     * @param array $configurations
44
     */
45 12
    public function addClientsConfigurations(array $configurations)
46
    {
47 12
        foreach ($configurations as $name => $conf) {
48 12
            $this->addClientConfiguration($name, $conf);
49
        }
50 12
    }
51
52
    /**
53
     * @param string $name
54
     * @param array  $conf
55
     */
56 12
    private function addClientConfiguration(string $name, array $conf)
57
    {
58 12
        $this->configurations[$name] = $this->buildClientConfiguration($conf);
59 12
    }
60
61
    /**
62
     * @param array $conf
63
     *
64
     * @return ClientConfiguration
65
     */
66 12
    private function buildClientConfiguration(array $conf): ClientConfiguration
67
    {
68 12
        return new ClientConfiguration(
69 12
            $this->buildConnectionUri($conf['hosts']),
70 12
            $conf['username'],
71 12
            $conf['password'],
72
            [
73 12
                'replicaSet' => $conf['replicaSet'],
74 12
                'ssl' => $conf['ssl'],
75 12
                'connectTimeoutMS' => $conf['connectTimeoutMS'],
76
            ]
77
        );
78
    }
79
80
    /**
81
     * @param array $hosts
82
     *
83
     * @return string
84
     */
85 12
    private function buildConnectionUri(array $hosts): string
86
    {
87 12
        return implode(
88 12
            ',',
89
            array_map(
90 12
                function (array $host) {
91 12
                    return sprintf("%s:%d", $host['host'], $host['port']);
92 12
                },
93
                $hosts
94
            )
95
        );
96
    }
97
98
    /**
99
     * @param string $name
100
     * @param string $databaseName
101
     *
102
     * @return Client
103
     */
104 10
    public function getClientForDatabase(string $name, string $databaseName): Client
105
    {
106 10
        return $this->getClient($name, $databaseName);
107
    }
108
109
    /**
110
     * @param string $name
111
     * @param string $databaseName
112
     *
113
     * @return Client
114
     */
115 12
    public function getClient(string $name, string $databaseName = null): Client
116
    {
117 12
        $clientKey = !is_null($databaseName) ? $name.'.'.$databaseName : $name;
118
119 12
        if (!isset($this->clients[$clientKey])) {
120 12
            $conf = $this->configurations[$name];
121 12
            $uri = sprintf('mongodb://%s', $conf->getHosts());
122 12
            $options = array_merge(['database' => $databaseName], $conf->getOptions());
123 12
            $this->clients[$clientKey] = $this->buildClient($uri, $options, []);
124
125 12
            $this->eventDispatcher->dispatch(
126 12
                ConnectionEvent::CLIENT_CREATED,
127 12
                new ConnectionEvent($clientKey)
128
            );
129
        }
130
131 12
        return $this->clients[$clientKey];
132
    }
133
134
    /**
135
     * @param                              $uri
136
     * @param array                        $options
137
     * @param array                        $driverOptions
138
     *
139
     * @return Client
140
     */
141 12
    private function buildClient($uri, array $options, array $driverOptions): Client
142
    {
143 12
        if ('dev' === $this->environment) {
144 3
            return new BundleClient($uri, $options, $driverOptions, $this->eventDispatcher);
145
        }
146
147 9
        return new Client($uri, $options, $driverOptions);
148
    }
149
}
150