Completed
Pull Request — master (#14)
by Pavel
04:42
created

ChainApiFactoryRegistry::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 3
crap 3.0416
1
<?php
2
3
namespace Bankiru\Api\Doctrine\ApiFactory;
4
5
use Bankiru\Api\Doctrine\ApiFactoryRegistryInterface;
6
use Bankiru\Api\Doctrine\Exception\MappingException;
7
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
8
use ScayTrase\Api\Rpc\RpcClientInterface;
9
10
final class ChainApiFactoryRegistry implements ApiFactoryRegistryInterface
11
{
12
    /** @var ApiFactoryRegistryInterface[] */
13
    private $delegates = [];
14
15
    /**
16
     * ChainApiFactory constructor.
17
     *
18
     * @param array $delegates
19
     */
20 20
    public function __construct(array $delegates = [])
21
    {
22 20
        $this->delegates = $delegates;
23 20
    }
24
25
    /** {@inheritdoc} */
26 19
    public function create($alias, RpcClientInterface $client, ApiMetadata $metadata)
27
    {
28 19
        foreach ($this->delegates as $delegate) {
29 19
            if ($delegate->has($alias)) {
30 19
                return $delegate->create($alias, $client, $metadata);
31
            }
32 19
        }
33
34
        throw MappingException::unknownApiFactory($alias);
35
    }
36
37
    /** {@inheritdoc} */
38
    public function has($alias)
39
    {
40
        foreach ($this->delegates as $delegate) {
41
            if ($delegate->has($alias)) {
42
                return true;
43
            }
44
        }
45
46
        return false;
47
    }
48
49
    /**
50
     * @param ApiFactoryRegistryInterface $registry
51
     */
52 20
    public function add(ApiFactoryRegistryInterface $registry)
53
    {
54 20
        $this->delegates[] = $registry;
55 20
    }
56
}
57