CompositeContainer::addContainer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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
     * @return mixed
21
     */
22
    public function get(string $serviceId)
23
    {
24
        foreach ($this->containers as $container) {
25
            if ($container->has($serviceId)) {
26
                return $container->get($serviceId);
27
            }
28
        }
29
        throw ServiceNotFound::createFromServiceId($serviceId);
30
    }
31
32
    /**
33
     * @return bool
34
     */
35
    public function has(string $serviceId)
36
    {
37
        foreach ($this->containers as $container) {
38
            if ($container->has($serviceId)) {
39
                return true;
40
            }
41
        }
42
        return false;
43
    }
44
}
45