Completed
Pull Request — master (#13)
by Alessandro
03:52
created

ClientRegistry::buildClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Facile\MongoDbBundle\Services;
6
7
use Facile\MongoDbBundle\Capsule\Client as LoggerClient;
8
use Facile\MongoDbBundle\Models\ClientConfiguration;
9
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface;
10
use MongoDB\Client;
11
12
/**
13
 * Class ClientRegistry.
14
 */
15
class ClientRegistry
16
{
17
    /** @var Client[] */
18
    private $clients;
19
    /** @var ClientConfiguration[] */
20
    private $configurations;
21
    /** @var DataCollectorLoggerInterface */
22
    private $logger;
23
    /** @var string */
24
    private $environment;
25
26
    /**
27
     * ClientRegistry constructor.
28
     *
29
     * @param DataCollectorLoggerInterface $logger
30
     * @param string                       $environment
31
     */
32 7
    public function __construct(DataCollectorLoggerInterface $logger, string $environment)
33
    {
34 7
        $this->clients = [];
35 7
        $this->configurations = [];
36 7
        $this->logger = $logger;
37 7
        $this->environment = $environment;
38 7
    }
39
40
    /**
41
     * @param string $name
42
     * @param array  $conf
43
     */
44 7
    public function addClientConfiguration(string $name, array $conf)
45
    {
46 7
        $this->configurations[$name] = $this->buildClientConfiguration($conf);
47 7
    }
48
49
    /**
50
     * @param string $name
51
     * @param string $databaseName
52
     *
53
     * @return Client
54
     */
55 7
    public function getClientForDatabase(string $name, string $databaseName): Client
56
    {
57 7
        return $this->getClient($name, $databaseName);
58
    }
59
60
    /**
61
     * @param string $name
62
     * @param string $databaseName
63
     *
64
     * @return Client
65
     */
66 7
    public function getClient(string $name, string $databaseName = null): Client
67
    {
68 7
        $clientKey = !is_null($databaseName) ? $name.'.'.$databaseName : $name;
69
70 7
        if (!isset($this->clients[$clientKey])) {
71 7
            $conf = $this->configurations[$name];
72 7
            $uri = sprintf('mongodb://%s:%d', $conf->getHost(), $conf->getPort());
73 7
            $options = array_merge(['database' => $databaseName], $conf->getOptions());
74 7
            $this->clients[$clientKey] = $this->buildClient($uri, $options, []);
75 7
            $this->logger->addConnection($clientKey);
76
        }
77
78 7
        return $this->clients[$clientKey];
79
    }
80
81
    /**
82
     * @param                              $uri
83
     * @param array                        $options
84
     * @param array                        $driverOptions
85
     *
86
     * @return Client
87
     */
88 7
    private function buildClient($uri, array $options, array $driverOptions): Client
89
    {
90 7
        if ('dev' === $this->environment) {
91 2
            return new LoggerClient($uri, $options, $driverOptions, $this->logger);
92
        }
93
94 5
        return new Client($uri, $options, $driverOptions);
95
    }
96
97
    /**
98
     * @param array $conf
99
     *
100
     * @return ClientConfiguration
101
     */
102 7
    private function buildClientConfiguration(array $conf): ClientConfiguration
103
    {
104 7
        return new ClientConfiguration(
105 7
            $conf['host'],
106 7
            $conf['port'],
107 7
            $conf['username'],
108 7
            $conf['password'],
109
            [
110 7
                'replicaSet' => $conf['replicaSet'],
111 7
                'ssl' => $conf['ssl'],
112 7
                'connectTimeoutMS' => $conf['connectTimeoutMS'],
113
            ]
114
        );
115
    }
116
}
117