Completed
Pull Request — master (#5)
by Alessandro
03:10
created

ClientRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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 array  $conf
30
     */
31 2
    public function addClientConfiguration(string $name, array $conf)
32
    {
33 2
        $this->configurations[$name] = new ConnectionConfiguration($conf['host'], $conf['port'], $conf['username'], $conf['password']);
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