DefinitionAggregate   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
eloc 38
dl 0
loc 109
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0

11 Methods

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