Passed
Push — 4.x ( 7477c3...10716b )
by Phil
13:00
created

DefinitionAggregate::resolveTaggedNew()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 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 126
    public function __construct(array $definitions = [])
21
    {
22
        $this->definitions = array_filter($definitions, static function ($definition) {
23 3
            return ($definition instanceof DefinitionInterface);
24 126
        });
25 126
    }
26
27 57
    public function add(string $id, $definition): DefinitionInterface
28
    {
29 57
        if (false === ($definition instanceof DefinitionInterface)) {
30 48
            $definition = new Definition($id, $definition);
31
        }
32
33 57
        $this->definitions[] = $definition->setAlias($id);
34
35 57
        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
        foreach ($this->getIterator() as $definition) {
47 42
            if ($id === $definition->getAlias()) {
48 42
                return true;
49
            }
50
        }
51
52 33
        return false;
53
    }
54
55 33
    public function hasTag(string $tag): bool
56
    {
57 33
        foreach ($this->getIterator() as $definition) {
58 15
            if ($definition->hasTag($tag)) {
59 9
                return true;
60
            }
61
        }
62
63 24
        return false;
64
    }
65
66 39
    public function getDefinition(string $id): DefinitionInterface
67
    {
68 39
        foreach ($this->getIterator() as $definition) {
69 39
            if ($id === $definition->getAlias()) {
70 36
                return $definition->setContainer($this->getContainer());
71
            }
72
        }
73
74 3
        throw new NotFoundException(sprintf('Alias (%s) is not being handled as a definition.', $id));
75
    }
76
77 30
    public function resolve(string $id)
78
    {
79 30
        return $this->getDefinition($id)->resolve();
80
    }
81
82 3
    public function resolveNew(string $id)
83
    {
84 3
        return $this->getDefinition($id)->resolveNew();
85
    }
86
87 9
    public function resolveTagged(string $tag): array
88
    {
89 9
        $arrayOf = [];
90
91 9
        foreach ($this->getIterator() as $definition) {
92 9
            if ($definition->hasTag($tag)) {
93 9
                $arrayOf[] = $definition->setContainer($this->getContainer())->resolve();
94
            }
95
        }
96
97 9
        return $arrayOf;
98
    }
99
100 3
    public function resolveTaggedNew(string $tag): array
101
    {
102 3
        $arrayOf = [];
103
104 3
        foreach ($this->getIterator() as $definition) {
105 3
            if ($definition->hasTag($tag)) {
106 3
                $arrayOf[] = $definition->setContainer($this->getContainer())->resolveNew();
107
            }
108
        }
109
110 3
        return $arrayOf;
111
    }
112
113 69
    public function getIterator(): Generator
114
    {
115 69
        yield from $this->definitions;
116 42
    }
117
}
118