Passed
Branch master (6982d9)
by Ivan
02:35
created

Registry::getContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
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
    /**
35
     * @param RegistrableItemInterface $item
36
     *
37
     * @throws ContainerIsNotRegisteredException
38
     */
39
    public function register(RegistrableItemInterface $item): void
40
    {
41
        foreach ($item->getRegisteredContainerNames() as $containerName) {
42
            if (false === array_key_exists($containerName, $this->registry)) {
43
                throw new ContainerIsNotRegisteredException($containerName);
44
            }
45
46
            $this->registry[$containerName]->add($item);
47
        }
48
    }
49
50
    /**
51
     * @param string $containerName
52
     *
53
     * @return MutableContainerInterface
54
     *
55
     * @throws ContainerIsNotRegisteredException
56
     */
57
    public function getContainer(string $containerName): MutableContainerInterface
58
    {
59
        if (false === array_key_exists($containerName, $this->registry)) {
60
            throw new ContainerIsNotRegisteredException($containerName);
61
        }
62
63
        return $this->registry[$containerName];
64
    }
65
}
66