Completed
Push — master ( 1af02f...31e012 )
by Arne
02:10
created

VaultContainer::getVault()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace Storeman;
4
5
use League\Container\Exception\NotFoundException;
6
use Psr\Container\ContainerInterface;
7
8
final class VaultContainer implements ContainerInterface
9
{
10
    /**
11
     * @var Vault[]
12
     */
13
    protected $vaults = [];
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function has($id)
19
    {
20
        return $this->getVault($id) !== null;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function get($id)
27
    {
28
        if ($vault = $this->getVault($id))
29
        {
30
            return $vault;
31
        }
32
33
        throw new NotFoundException();
34
    }
35
36
    public function register(Vault $vault): void
37
    {
38
        $title = $vault->getVaultConfiguration()->getTitle();
39
40
        if ($this->has($title))
41
        {
42
            throw new \RuntimeException("There is already a vault named {$title} registered.");
43
        }
44
45
        $this->vaults[$title] = $vault;
46
    }
47
48
    protected function getVault(string $title): ?Vault
49
    {
50
        foreach ($this->vaults as $vault)
51
        {
52
            if ($vault->getVaultConfiguration()->getTitle() === $title)
53
            {
54
                return $vault;
55
            }
56
        }
57
58
        return null;
59
    }
60
}
61