DelegateContainerTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 62
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addDelegateContainer() 0 4 1
A getDelegateContainers() 0 4 1
A delegateHas() 0 10 3
A delegateGet() 0 10 3
1
<?php
2
/*
3
* The MIT License (MIT)
4
* Copyright © 2017 Marcos Lois Bermudez <[email protected]>
5
* *
6
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
* *
11
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
* of the Software.
13
* *
14
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
* DEALINGS IN THE SOFTWARE.
19
*/
20
21
namespace TTIC\Interop\Container;
22
23
use Interop\Container\ContainerInterface;
24
use Interop\Container\Exception\NotFoundException;
25
use TTIC\Interop\Container\Exception\ContainerException;
26
27
/**
28
 * Utility trait to add delegate container feature.
29
 *
30
 * @package TTIC\Container
31
 * @author Marcos Lois Bermúdez <[email protected]>
32
 */
33
trait DelegateContainerTrait
34
{
35
    /**
36
     * @var ContainerInterface[]
37
     */
38
    protected $delegateContainers = [];
39
40
    /**
41
     * @param ContainerInterface $container
42
     */
43 3
    public function addDelegateContainer(ContainerInterface $container)
44
    {
45 3
        $this->delegateContainers[] = $container;
46 3
    }
47
48
    /**
49
     * @return ContainerInterface[]
50
     */
51 3
    public function getDelegateContainers()
52
    {
53 3
        return $this->delegateContainers;
54
    }
55
56
    /**
57
     * Check if any delegated containers has a entry.
58
     *
59
     * @param string $id Identifier of the entry to look for.
60
     *
61
     * @return bool TRUE if any delegated container has the key, FALSE otherwise.
62
     */
63 3
    protected function delegateHas($id)
64
    {
65 3
        foreach ($this->delegateContainers as $container) {
66 3
            if ($container->has($id)) {
67 3
                return true;
68
            }
69 1
        }
70
71 3
        return false;
72
    }
73
74
    /**
75
     * Finds an entry in the delegated container by its identifier and returns it.
76
     *
77
     * @param string $id Identifier of the entry to look for.
78
     *
79
     * @throws NotFoundException  No entry was found for this identifier.
80
     * @throws ContainerException Error while retrieving the entry.
81
     *
82
     * @return mixed Entry.
83
     */
84 6
    protected function delegateGet($id)
85
    {
86 6
        foreach ($this->delegateContainers as $container) {
87 3
            if ($container->has($id)) {
88 3
                return $container->get($id);
89
            }
90 1
        }
91
92 3
        throw ContainerException::fromPrevious($id, null);
93
    }
94
}
95