Completed
Push — master ( c02769...b03948 )
by Phil
01:48
created

ServiceProviderAggregate::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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