Completed
Pull Request — master (#2)
by Alessandro
02:37
created

ConnectionRegistry::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 3
cts 4
cp 0.75
rs 9.6666
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Facile\MongoDbBundle\Services;
6
7
use MongoDB\Client;
8
use Facile\MongoDbBundle\Exceptions\ClientNotFoundException;
9
use Facile\MongoDbBundle\Exceptions\ExistantClientException;
10
11
/**
12
 * Class ConnectionRegistry.
13
 */
14
final class ConnectionRegistry
15
{
16
    /** @var Client[] */
17
    private $connections;
18
19
    /**
20
     * ConnectionRegistry constructor.
21
     */
22 2
    public function __construct()
23
    {
24 2
        $this->connections = [];
25 2
    }
26
27
    /**
28
     * @param string $clientIdentifier
29
     *
30
     * @return Client
31
     *
32
     * @throws ClientNotFoundException
33
     */
34 1
    public function getClient(string $clientIdentifier): Client
35
    {
36 1
        if (isset($this->connections[$clientIdentifier])) {
37
38 1
            return $this->connections[$clientIdentifier];
39
        }
40
41
        throw new ClientNotFoundException(sprintf('Client for key %s not found', $clientIdentifier));
42
    }
43
44
    /**
45
     * @param string $clientIdentifier
46
     * @param Client $client
47
     *
48
     * @throws ExistantClientException
49
     */
50 2
    public function addClient(string $clientIdentifier, Client $client)
51
    {
52 2
        if (isset($this->connections[$clientIdentifier])) {
53
            throw new ExistantClientException('Client for key %s already exists');
54
        }
55
56 2
        $this->connections[$clientIdentifier] = $client;
57 2
    }
58
59
    /**
60
     * @param string $clientIdentifier
61
     *
62
     * @return bool
63
     */
64 2
    public function hasClient(string $clientIdentifier): bool
65
    {
66 2
        return array_key_exists($clientIdentifier, $this->connections);
67
    }
68
}
69