Completed
Push — 4.x ( 8fa263...613328 )
by Phil
03:02
created

DefinitionAggregate::resolveTaggedNew()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
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 114
    public function __construct(array $definitions = [])
21
    {
22
        $this->definitions = array_filter($definitions, static function ($definition) {
23 3
            return ($definition instanceof DefinitionInterface);
24 114
        });
25 114
    }
26
27 48
    public function add(string $id, $definition): DefinitionInterface
28
    {
29 48
        if (false === ($definition instanceof DefinitionInterface)) {
30 39
            $definition = new Definition($id, $definition);
31
        }
32
33 48
        $this->definitions[] = $definition->setAlias($id);
34
35 48
        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 45
    public function has(string $id): bool
45
    {
46 45
        foreach ($this->getIterator() as $definition) {
47 33
            if ($id === $definition->getAlias()) {
48 33
                return true;
49
            }
50
        }
51
52 24
        return false;
53
    }
54
55 24
    public function hasTag(string $tag): bool
56
    {
57 24
        foreach ($this->getIterator() as $definition) {
58 9
            if ($definition->hasTag($tag)) {
59 9
                return true;
60
            }
61
        }
62
63 15
        return false;
64
    }
65
66 30
    public function getDefinition(string $id): DefinitionInterface
67
    {
68 30
        foreach ($this->getIterator() as $definition) {
69 30
            if ($id === $definition->getAlias()) {
70 27
                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 21
    public function resolve(string $id)
78
    {
79 21
        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 57
    public function getIterator(): Generator
114
    {
115 57
        yield from $this->definitions;
116 33
    }
117
}
118