Completed
Push — master ( cf6896...85995a )
by maxime
02:03
created

CompositeContainer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Di;
6
7
use Psr\Container\ContainerInterface;
8
9
class CompositeContainer implements ContainerInterface
10
{
11
    protected $containers = [];
12
13 6
    public function __construct(array $containers = [])
14
    {
15 6
        foreach ($containers as $container)
16
        {
17 1
            $this->push($container);
18
        }
19 6
    }
20
21 4
    public function push(ContainerInterface $container): self
22
    {
23 4
        if ($container instanceof RootContainerAwareInterface)
24
        {
25 3
            $container->setRoot($this);
26
        }
27
28 4
        array_unshift($this->containers, $container);
29
30 4
        return $this;
31
    }
32
33 1
    public function pop(): ?ContainerInterface
34
    {
35 1
        $container = array_shift($this->containers);
36
37 1
        if ($container instanceof RootContainerAwareInterface)
38
        {
39 1
            $container->setRoot(null);
40
        }
41
42 1
        return $container;
43
    }
44
45 3
    public function get($id)
46
    {
47 3
        foreach ($this->containers as $container)
48
        {
49 2
            if ($container->has($id))
50
            {
51 2
                return $container->get($id);
52
            }
53
        }
54
55 1
        throw new NotFoundException($id);
56
    }
57
58 2
    public function has($id): bool
59
    {
60 2
        foreach ($this->containers as $container)
61
        {
62 2
            if ($container->has($id))
63
            {
64 2
                return true;
65
            }
66
        }
67
68 2
        return false;
69
    }
70
}