DefinitionAggregate   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 116
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add() 0 13 2
A has() 0 10 3
A hasTag() 0 10 3
A getDefinition() 0 10 3
A resolve() 0 4 1
A resolveTagged() 0 12 3
A getIterator() 0 8 2
1
<?php declare(strict_types=1);
2
3
namespace League\Container\Definition;
4
5
use Generator;
6
use League\Container\ContainerAwareTrait;
7
use League\Container\Exception\NotFoundException;
8
9
class DefinitionAggregate implements DefinitionAggregateInterface
10
{
11
    use ContainerAwareTrait;
12
13
    /**
14
     * @var \League\Container\Definition\DefinitionInterface[]
15
     */
16
    protected $definitions = [];
17
18
    /**
19
     * Construct.
20
     *
21
     * @param \League\Container\Definition\DefinitionInterface[] $definitions
22
     */
23
    public function __construct(array $definitions = [])
24
    {
25 69
        $this->definitions = array_filter($definitions, function ($definition) {
26 3
            return ($definition instanceof DefinitionInterface);
27 69
        });
28 69
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 45
    public function add(string $id, $definition, bool $shared = false) : DefinitionInterface
34
    {
35 45
        if (! $definition instanceof DefinitionInterface) {
36 36
            $definition = (new Definition($id, $definition));
37
        }
38
39 45
        $this->definitions[] = $definition
40 45
            ->setAlias($id)
41 45
            ->setShared($shared)
42
        ;
43
44 45
        return $definition;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 39
    public function has(string $id) : bool
51
    {
52 39
        foreach ($this->getIterator() as $definition) {
53 30
            if ($id === $definition->getAlias()) {
54 30
                return true;
55
            }
56
        }
57
58 18
        return false;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 18
    public function hasTag(string $tag) : bool
65
    {
66 18
        foreach ($this->getIterator() as $definition) {
67 6
            if ($definition->hasTag($tag)) {
68 6
                return true;
69
            }
70
        }
71
72 12
        return false;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 30
    public function getDefinition(string $id) : DefinitionInterface
79
    {
80 30
        foreach ($this->getIterator() as $definition) {
81 30
            if ($id === $definition->getAlias()) {
82 30
                return $definition->setContainer($this->getContainer());
83
            }
84
        }
85
86 3
        throw new NotFoundException(sprintf('Alias (%s) is not being handled as a definition.', $id));
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 24
    public function resolve(string $id, bool $new = false)
93
    {
94 24
        return $this->getDefinition($id)->resolve($new);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 6
    public function resolveTagged(string $tag, bool $new = false) : array
101
    {
102 6
        $arrayOf = [];
103
104 6
        foreach ($this->getIterator() as $definition) {
105 6
            if ($definition->hasTag($tag)) {
106 6
                $arrayOf[] = $definition->setContainer($this->getContainer())->resolve($new);
107
            }
108
        }
109
110 6
        return $arrayOf;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 51
    public function getIterator() : Generator
117
    {
118 51
        $count = count($this->definitions);
119
120 51
        for ($i = 0; $i < $count; $i++) {
121 42
            yield $this->definitions[$i];
122
        }
123 27
    }
124
}
125