Passed
Pull Request — master (#6)
by
unknown
02:09
created

Registry   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 3
A getContainer() 0 7 2
A addContainer() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\Navigation\Container;
6
7
use Everlution\Navigation\Container\ContainerAlreadyRegisteredException;
8
use Everlution\Navigation\Container\ContainerIsNotRegisteredException;
9
use Everlution\Navigation\Container\MutableContainerInterface;
10
use Everlution\Navigation\Item\RegistrableItemInterface;
11
12
/**
13
 * Class Registry.
14
 *
15
 * @author Ivan Barlog <[email protected]>
16
 */
17
class Registry
18
{
19
    /** @var MutableContainerInterface[] */
20
    private $registry = [];
21
22
    /**
23
     * @param MutableContainerInterface $container
24
     *
25
     * @throws ContainerAlreadyRegisteredException
26
     */
27
    public function addContainer(MutableContainerInterface $container): void
28
    {
29
        $containerName = get_class($container);
30
        if (array_key_exists($containerName, $this->registry)) {
31
            throw new ContainerAlreadyRegisteredException($containerName);
32
        }
33
34
        $this->registry[$containerName] = $container;
35
    }
36
37
    public function register(RegistrableItemInterface $item): void
38
    {
39
        foreach ($item->getRegisteredContainerNames() as $containerName) {
40
            if (false === array_key_exists($containerName, $this->registry)) {
41
                continue;
42
            }
43
44
            $this->registry[$containerName]->add($item);
45
        }
46
    }
47
48
    /**
49
     * @param string $containerName
50
     *
51
     * @return MutableContainerInterface
52
     *
53
     * @throws ContainerIsNotRegisteredException
54
     */
55
    public function getContainer(string $containerName): MutableContainerInterface
56
    {
57
        if (false === array_key_exists($containerName, $this->registry)) {
58
            throw new ContainerIsNotRegisteredException($containerName);
59
        }
60
61
        return $this->registry[$containerName];
62
    }
63
}
64