Registry::getContainer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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