Completed
Pull Request — master (#63)
by .
03:06
created

ClientRegistry::buildConnectionUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A ClientRegistry::getClientNames() 0 4 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 30
    public function __construct(EventDispatcherInterface $eventDispatcher, string $environment)
35
    {
36 30
        $this->clients = [];
37 30
        $this->configurations = [];
38 30
        $this->environment = $environment;
39 30
        $this->eventDispatcher = $eventDispatcher;
40 30
    }
41
42
    /**
43
     * @param array $configurations
44
     */
45 30
    public function addClientsConfigurations(array $configurations)
46
    {
47 30
        foreach ($configurations as $name => $conf) {
48 30
            $this->addClientConfiguration($name, $conf);
49
        }
50 30
    }
51
52
    /**
53
     * @param string $name
54
     * @param array $conf
55
     */
56 30
    private function addClientConfiguration(string $name, array $conf)
57
    {
58 30
        $this->configurations[$name] = $this->buildClientConfiguration($conf);
59 30
    }
60
61
    /**
62
     * @param array $conf
63
     *
64
     * @return ClientConfiguration
65
     */
66 30
    private function buildClientConfiguration(array $conf): ClientConfiguration
67
    {
68 30
        if  (!array_key_exists('options', $conf)) {
69 28
            $conf['options'] = [];
70
        }
71
72 30
        return new ClientConfiguration(
73 30
            $conf['uri'],
74 30
            $conf['options']
75
        );
76
    }
77
78
    /**
79
     * @param string $name
80
     * @param string $databaseName
81
     *
82
     * @return Client
83
     */
84 11
    public function getClientForDatabase(string $name, string $databaseName): Client
85
    {
86 11
        return $this->getClient($name, $databaseName);
87
    }
88
89
    /**
90
     * @return array
91
     */
92 1
    public function getClientNames(): array
93
    {
94 1
        return array_keys($this->clients);
95
    }
96
97
    /**
98
     * @param string $name
99
     * @param string $databaseName
100
     *
101
     * @return Client
102
     */
103 28
    public function getClient(string $name, string $databaseName = null): Client
104
    {
105 28
        $clientKey = null !== $databaseName ? $name . '.' . $databaseName : $name;
106
107 28
        if (! isset($this->clients[$clientKey])) {
108 28
            $conf = $this->configurations[$name];
109 28
            $options = array_merge(
110
                [
111 28
                    'database' => $databaseName,
112
                ],
113 28
                $conf->getOptions()
114
            );
115 28
            $this->clients[$clientKey] = $this->buildClient($name, $conf->getUri(), $options, []);
116
117 28
            $this->eventDispatcher->dispatch(
118 28
                ConnectionEvent::CLIENT_CREATED,
119 28
                new ConnectionEvent($clientKey)
120
            );
121
        }
122
123 28
        return $this->clients[$clientKey];
124
    }
125
126
    /**
127
     * @param string $clientName
128
     * @param string $uri
129
     * @param array $options
130
     * @param array $driverOptions
131
     *
132
     * @return Client
133
     */
134 28
    private function buildClient(string $clientName, string $uri, array $options, array $driverOptions): Client
135
    {
136 28
        if ('dev' === $this->environment) {
137 5
            return new BundleClient($uri, $options, $driverOptions, $clientName, $this->eventDispatcher);
138
        }
139
140 23
        return new Client($uri, $options, $driverOptions);
141
    }
142
}
143