Completed
Pull Request — master (#2)
by Alessandro
03:52 queued 01:05
created

ConnectionRegistry::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
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 1
            return $this->connections[$clientIdentifier];
38
        }
39
40
        throw new ClientNotFoundException(sprintf('Client for key %s not found', $clientIdentifier));
41
    }
42
43
    /**
44
     * @param string $clientIdentifier
45
     * @param Client $client
46
     *
47
     * @throws ExistantClientException
48
     */
49 2
    public function addClient(string $clientIdentifier, Client $client)
50
    {
51 2
        if (isset($this->connections[$clientIdentifier])) {
52
            throw new ExistantClientException('Client for key %s already exists');
53
        }
54
55 2
        $this->connections[$clientIdentifier] = $client;
56 2
    }
57
58
    /**
59
     * @param string $clientIdentifier
60
     *
61
     * @return bool
62
     */
63 2
    public function hasClient(string $clientIdentifier): bool
64
    {
65 2
        return array_key_exists($clientIdentifier, $this->connections);
66
    }
67
}
68