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

CompositeContainer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addContainer() 0 4 1
A get() 0 9 3
A has() 0 9 3
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