Completed
Pull Request — master (#5)
by Alessandro
04:33
created

ClientRegistry::addClientConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Services;
6
7
use Facile\MongoDbBundle\Capsule\Client;
8
use Facile\MongoDbBundle\Models\ConnectionConfiguration;
9
10
/**
11
 * Class ClientRegistry.
12
 */
13
class ClientRegistry
14
{
15
    /** @var Client[] */
16
    private $clients;
17
18
    /** @var ConnectionConfiguration[] */
19
    private $configurations;
20
21 2
    public function __construct()
22
    {
23 2
        $this->clients = [];
24 2
        $this->configurations = [];
25 2
    }
26
27
    /**
28
     * @param string                  $name
29
     * @param ConnectionConfiguration $configuration
30
     */
31 2
    public function addClientConfiguration(string $name, ConnectionConfiguration $configuration)
32
    {
33 2
        $this->configurations[$name] = $configuration;
34 2
    }
35
36
    /**
37
     * @param string $name
38
     *
39
     * @return Client
40
     */
41 2
    public function getClient(string $name): Client
42
    {
43 2
        if (!isset($this->clients[$name])) {
44 2
            $conf = $this->configurations[$name];
45 2
            $this->clients[$name] = new Client($conf->getConnectionUri());
46
        }
47
48 2
        return $this->clients[$name];
49
    }
50
}
51