Completed
Push — master ( 6c7efd...3c9059 )
by Phil
03:11
created

DefinitionAggregate   A

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 60
        $this->definitions = array_filter($definitions, function ($definition) {
26 3
            return ($definition instanceof DefinitionInterface);
27 60
        });
28 60
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 36
    public function add(string $id, $definition, bool $shared = false): DefinitionInterface
34
    {
35 36
        if (! $definition instanceof DefinitionInterface) {
36 27
            $definition = (new Definition($id, $definition));
37
        }
38
39 36
        $this->definitions[] = $definition
40 36
            ->setAlias($id)
41 36
            ->setShared($shared)
42
        ;
43
44 36
        return $definition;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 30
    public function has(string $id): bool
51
    {
52 30
        foreach ($this->getIterator() as $definition) {
53 21
            if ($id === $definition->getAlias()) {
54 21
                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 21
    public function getDefinition(string $id): DefinitionInterface
79
    {
80 21
        foreach ($this->getIterator() as $definition) {
81 21
            if ($id === $definition->getAlias()) {
82 21
                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 15
    public function resolve(string $id, bool $new = false)
93
    {
94 15
        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 42
    public function getIterator(): Generator
117
    {
118 42
        $count = count($this->definitions);
119
120 42
        for ($i = 0; $i < $count; $i++) {
121 33
            yield $this->definitions[$i];
122
        }
123 27
    }
124
}
125