Completed
Pull Request — master (#126)
by Phil
11:29
created

DefinitionAggregate::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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
     * {@inheritdoc}
20
     */
21
    public function add(string $id, $definition, bool $shared = false): DefinitionInterface
22
    {
23
        if (! $definition instanceof DefinitionInterface) {
24
            $definition = (new Definition($id, $definition));
25
        }
26
27
        $this->definitions[] = $definition
28
            ->setContainer($this->getContainer())
29
            ->setAlias($id)
30
            ->setShared($shared)
31
        ;
32
33
        return $definition;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function has(string $id): bool
40
    {
41
        foreach ($this->getIterator() as $definition) {
42
            if ($id === $definition->getAlias()) {
43
                return true;
44
            }
45
        }
46
47
        return false;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function hasTag(string $tag): bool
54
    {
55
        foreach ($this->getIterator() as $definition) {
56
            if ($definition->hasTag($tag)) {
57
                return true;
58
            }
59
        }
60
61
        return false;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getDefinition(string $id): DefinitionInterface
68
    {
69
        foreach ($this->getIterator() as $definition) {
70
            if ($id === $definition->getAlias()) {
71
                return $definition;
72
            }
73
        }
74
75
        throw new NotFoundException(sprintf('Alias (%s) is not being handled as a definition.', $id));
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function resolve(string $id, bool $new = false)
82
    {
83
        return $this->getDefinition($id)->resolve($new);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function resolveTagged(string $tag, bool $new = false): array
90
    {
91
        $arrayOf = [];
92
93
        foreach ($this->getIterator() as $definition) {
94
            if ($definition->hasTag($tag)) {
95
                $arrayOf[] = $definition->resolve($new);
96
            }
97
        }
98
99
        return $arrayOf;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getIterator(): Generator
106
    {
107
        $count = count($this->definitions);
108
109
        for ($i = 0; $i < $count; $i++) {
110
            yield $this->definitions[$i];
111
        }
112
    }
113
}
114