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

ClientRegistry   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 38
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addClientConfiguration() 0 4 1
A getClient() 0 9 2
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