Registry   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 1
b 0
f 0
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addContainer() 0 8 2
A getContainer() 0 7 2
A register() 0 8 3
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