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

ChainApiFactoryRegistry::has()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Bankiru\Api\Doctrine\ApiFactory;
4
5
use Bankiru\Api\Doctrine\ApiFactoryRegistryInterface;
6
use Bankiru\Api\Doctrine\Exception\ApiFactoryRegistryException;
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 18
    public function __construct(array $delegates = [])
21
    {
22 18
        $this->delegates = $delegates;
23 18
    }
24
25
    /** {@inheritdoc} */
26 17
    public function create($alias, RpcClientInterface $client, ApiMetadata $metadata)
27
    {
28 17
        foreach ($this->delegates as $delegate) {
29 17
            if ($delegate->has($alias)) {
30 17
                return $delegate->create($alias, $client, $metadata);
31
            }
32 17
        }
33
34
        throw ApiFactoryRegistryException::unknown($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 18
    public function add(ApiFactoryRegistryInterface $registry)
53
    {
54 18
        $this->delegates[] = $registry;
55 18
    }
56
}
57