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

ConnectionRegistry::__construct()   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 0
crap 1
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