Completed
Push — master ( 60f2d5...c26c1c )
by Alessandro
03:01
created

ClientRegistry::getClientNames()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 12
                'readPreference' => $conf['readPreference'],
77
            ]
78
        );
79
    }
80
81
    /**
82
     * @param array $hosts
83
     *
84
     * @return string
85
     */
86 12
    private function buildConnectionUri(array $hosts): string
87
    {
88 12
        return implode(
89 12
            ',',
90
            array_map(
91 12
                function (array $host) {
92 12
                    return sprintf("%s:%d", $host['host'], $host['port']);
93 12
                },
94
                $hosts
95
            )
96
        );
97
    }
98
99
    /**
100
     * @param string $name
101
     * @param string $databaseName
102
     *
103
     * @return Client
104
     */
105 10
    public function getClientForDatabase(string $name, string $databaseName): Client
106
    {
107 10
        return $this->getClient($name, $databaseName);
108
    }
109
110
    /**
111
     * @return array
112
     */
113 1
    public function getClientNames(): array
114
    {
115 1
        return array_keys($this->clients);
116
    }
117
118
    /**
119
     * @param string $name
120
     * @param string $databaseName
121
     *
122
     * @return Client
123
     */
124 12
    public function getClient(string $name, string $databaseName = null): Client
125
    {
126 12
        $clientKey = !is_null($databaseName) ? $name.'.'.$databaseName : $name;
127
128 12
        if (!isset($this->clients[$clientKey])) {
129 12
            $conf = $this->configurations[$name];
130 12
            $uri = sprintf('mongodb://%s', $conf->getHosts());
131 12
            $options = array_merge(['database' => $databaseName], $conf->getOptions());
132 12
            $this->clients[$clientKey] = $this->buildClient($uri, $options, []);
133
134 12
            $this->eventDispatcher->dispatch(
135 12
                ConnectionEvent::CLIENT_CREATED,
136 12
                new ConnectionEvent($clientKey)
137
            );
138
        }
139
140 12
        return $this->clients[$clientKey];
141
    }
142
143
    /**
144
     * @param                              $uri
145
     * @param array                        $options
146
     * @param array                        $driverOptions
147
     *
148
     * @return Client
149
     */
150 12
    private function buildClient($uri, array $options, array $driverOptions): Client
151
    {
152 12
        if ('dev' === $this->environment) {
153 3
            return new BundleClient($uri, $options, $driverOptions, $this->eventDispatcher);
154
        }
155
156 9
        return new Client($uri, $options, $driverOptions);
157
    }
158
}
159