Completed
Push — master ( acbd58...945a05 )
by Sam
01:50
created

CompositeContainer::addContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace UltraLite\CompositeContainer;
3
4
use Psr\Container\ContainerInterface;
5
use Psr\Container\NotFoundExceptionInterface;
6
7
class CompositeContainer implements ContainerInterface
8
{
9
    /** @var ContainerInterface[] */
10
    private $containers = [];
11
12
    public function addContainer(ContainerInterface $delegateContainer)
13
    {
14
        $this->containers[] = $delegateContainer;
15
    }
16
17
    /**
18
     * @throws NotFoundExceptionInterface
19
     *
20
     * @param string $serviceId
21
     * @return mixed
22
     */
23
    public function get($serviceId)
24
    {
25
        foreach ($this->containers as $container) {
26
            if ($container->has($serviceId)) {
27
                return $container->get($serviceId);
28
            }
29
        }
30
        throw ServiceNotFound::createFromServiceId($serviceId);
31
    }
32
33
    /**
34
     * @param string $serviceId
35
     * @return bool
36
     */
37
    public function has($serviceId)
38
    {
39
        foreach ($this->containers as $container) {
40
            if ($container->has($serviceId)) {
41
                return true;
42
            }
43
        }
44
        return false;
45
    }
46
}
47