Completed
Pull Request — master (#126)
by Phil
02:35
created

ServiceProviderAggregate::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace League\Container\ServiceProvider;
4
5
use Generator;
6
use League\Container\{ContainerAwareInterface, ContainerAwareTrait};
7
use League\Container\Exception\ContainerException;
8
9
class ServiceProviderAggregate implements ServiceProviderAggregateInterface
10
{
11
    use ContainerAwareTrait;
12
13
    /**
14
     * @var \League\Container\ServiceProvider\ServiceProviderInterface[]
15
     */
16
    protected $providers = [];
17
18
    /**
19
     * @var array
20
     */
21
    protected $registered = [];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 9
    public function add($provider): ServiceProviderAggregateInterface
27
    {
28 9
        if (is_string($provider) && $this->getContainer()->has($provider)) {
29
            $provider = $this->getContainer()->get($provider);
30 9
        } elseif (is_string($provider) && class_exists($provider)) {
31
            $provider = new $provider;
32
        }
33
34 9
        if ($provider instanceof ContainerAwareInterface) {
35 6
            $provider->setContainer($this->getContainer());
36
        }
37
38 9
        if ($provider instanceof BootableServiceProviderInterface) {
39 6
            $provider->boot();
40
        }
41
42 9
        if ($provider instanceof ServiceProviderInterface) {
43 6
            $this->providers[] = $provider;
44
45 6
            return $this;
46
        }
47
48 3
        throw new ContainerException(
49
            'A service provider must be a fully qualified class name or instance ' .
50 3
            'of (\League\Container\ServiceProvider\ServiceProviderInterface)'
51
        );
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 9
    public function provides(string $service): bool
58
    {
59 9
        foreach ($this->getIterator() as $provider) {
60 6
            if ($provider->provides($service)) {
61 6
                return true;
62
            }
63
        }
64
65 3
        return false;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 9
    public function getIterator(): Generator
72
    {
73 9
        $count = count($this->providers);
74
75 9
        for ($i = 0; $i < $count; $i++) {
76 6
            yield $this->providers[$i];
77
        }
78 6
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 6
    public function register(string $service)
84
    {
85 6
        if (! $this->provides($service)) {
86 3
            throw new ContainerException(
87 3
                sprintf('(%s) is not provided by a service provider', $service)
88
            );
89
        }
90
91 3
        foreach ($this->getIterator() as $provider) {
92 3
            if (in_array($provider->getSignature(), $this->registered)) {
93 3
                return;
94
            }
95
96 3
            $provider->register();
97
98 3
            $this->registered[] = $provider->getSignature();
99
        }
100 3
    }
101
}
102