Completed
Pull Request — master (#63)
by .
05:38
created

ClientRegistry::buildClientConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
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 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
        return new ClientConfiguration(
69 30
            $conf['uri'],
70 30
            $conf['options']
71 30
        );
72 30
    }
73
74 30
    /**
75 30
     * @param string $name
76 30
     * @param string $databaseName
77 30
     *
78
     * @return Client
79
     */
80
    public function getClientForDatabase(string $name, string $databaseName): Client
81
    {
82
        return $this->getClient($name, $databaseName);
83
    }
84
85
    /**
86
     * @return array
87 30
     */
88
    public function getClientNames(): array
89 30
    {
90 30
        return array_keys($this->clients);
91 30
    }
92 30
93 30
    /**
94 30
     * @param string $name
95 30
     * @param string $databaseName
96
     *
97
     * @return Client
98
     */
99
    public function getClient(string $name, string $databaseName = null): Client
100
    {
101
        $clientKey = null !== $databaseName ? $name . '.' . $databaseName : $name;
102
103
        if (! isset($this->clients[$clientKey])) {
104
            $conf = $this->configurations[$name];
105
            $options = array_merge(
106 11
                [
107
                    'database' => $databaseName,
108 11
                ],
109
                $conf->getOptions()
110
            );
111
            $this->clients[$clientKey] = $this->buildClient($name, $conf->getUri(), $options, []);
112
113
            $this->eventDispatcher->dispatch(
114 1
                ConnectionEvent::CLIENT_CREATED,
115
                new ConnectionEvent($clientKey)
116 1
            );
117
        }
118
119
        return $this->clients[$clientKey];
120
    }
121
122
    /**
123
     * @param string $clientName
124
     * @param string $uri
125 28
     * @param array $options
126
     * @param array $driverOptions
127 28
     *
128
     * @return Client
129 28
     */
130 28
    private function buildClient(string $clientName, string $uri, array $options, array $driverOptions): Client
131 28
    {
132 28
        if ('dev' === $this->environment) {
133
            return new BundleClient($uri, $options, $driverOptions, $clientName, $this->eventDispatcher);
134 28
        }
135 28
136
        return new Client($uri, $options, $driverOptions);
137 28
    }
138
}
139