Completed
Pull Request — master (#126)
by Phil
19:36 queued 04:37
created

DefinitionAggregate   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 14 2
A resolve() 0 10 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\Argument\RawArgument;
7
use League\Container\ContainerAwareTrait;
8
use League\Container\Exception\NotFoundException;
9
10
class DefinitionAggregate implements DefinitionAggregateInterface
11
{
12
    use ContainerAwareTrait;
13
14
    /**
15
     * @var \League\Container\Definition\DefinitionInterface[]
16
     */
17
    protected $definitions = [];
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function add(string $id, $definition, bool $shared = false): DefinitionInterface
23
    {
24
        if (! $definition instanceof DefinitionInterface) {
25
            $definition = (new Definition($id, $definition));
26
        }
27
28
        $this->definitions[] = $definition
29
            ->setContainer($this->getContainer())
30
            ->setAlias($id)
31
            ->setShared($shared)
32
        ;
33
34
        return $definition;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function resolve(string $id, array $args = [], bool $new = false)
41
    {
42
        foreach ($this->getIterator() as $definition) {
43
            if ($id === $definition->getAlias()) {
44
                return $definition->resolve($args, $new);
45
            }
46
        }
47
48
        throw new NotFoundException(sprintf('Alias (%s) is not being handled by the container', $id));
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getIterator(): Generator
55
    {
56
        $count = count($this->definitions);
57
58
        for ($i = 0; $i < $count; $i++) {
59
            yield $this->definitions[$i];
60
        }
61
    }
62
}
63