Completed
Pull Request — master (#14)
by Pavel
03:32
created

ClientRegistry::replace()   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
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
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 18
    public function get($name)
19
    {
20 18
        if (!$this->has($name)) {
21
            throw new \OutOfBoundsException(sprintf('Client "%s" not registered', $name));
22
        }
23
24 18
        return $this->clients[$name];
25
    }
26
27
    /**
28
     * @param string $name
29
     *
30
     * @return bool
31
     */
32 18
    public function has($name)
33
    {
34 18
        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 18
    public function add($name, RpcClientInterface $client)
45
    {
46 18
        if ($this->has($name)) {
47
            throw new \InvalidArgumentException();
48
        }
49
50 18
        $this->replace($name, $client);
51 18
    }
52
53
    /**
54
     * @param string             $name
55
     * @param RpcClientInterface $client
56
     *
57
     * @return void
58
     */
59 18
    public function replace($name, RpcClientInterface $client)
60
    {
61 18
        $this->clients[$name] = $client;
62 18
    }
63
64
    /**
65
     * @return RpcClientInterface[]
66
     */
67
    public function all()
68
    {
69
        return $this->clients;
70
    }
71
}
72