Passed
Pull Request — master (#14)
by Pavel
03:16
created

ClientRegistry::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2.032
1
<?php
2
3
namespace Bankiru\Api\Doctrine;
4
5
use ScayTrase\Api\Rpc\RpcClientInterface;
6
7
final class ClientRegistry implements ClientRegistryInterface
8
{
9
    /** @var  RpcClientInterface[] */
10
    private $clients = [];
11
12
    /**
13
     * @param string $name
14
     *
15
     * @return RpcClientInterface
16
     * @throws \OutOfBoundsException
17
     */
18 24
    public function get($name)
19
    {
20 24
        if (!$this->has($name)) {
21
            throw new \OutOfBoundsException(sprintf('Client "%s" not registered', $name));
22
        }
23
24 24
        return $this->clients[$name];
25
    }
26
27
    /**
28
     * @param string $name
29
     *
30
     * @return bool
31
     */
32 25
    public function has($name)
33
    {
34 25
        return array_key_exists($name, $this->clients);
35
    }
36
37
    /**
38
     * @param string             $name
39
     * @param RpcClientInterface $client
40
     *
41
     * @return void
42
     * @throws \InvalidArgumentException
43
     */
44 25
    public function add($name, RpcClientInterface $client)
45
    {
46 25
        if ($this->has($name)) {
47
            throw new \InvalidArgumentException();
48
        }
49
50 25
        $this->replace($name, $client);
51 25
    }
52
53
    /**
54
     * @param string             $name
55
     * @param RpcClientInterface $client
56
     *
57
     * @return void
58
     */
59 25
    public function replace($name, RpcClientInterface $client)
60
    {
61 25
        $this->clients[$name] = $client;
62 25
    }
63
64
    /**
65
     * @return RpcClientInterface[]
66
     */
67
    public function all()
68
    {
69
        return $this->clients;
70
    }
71
}
72