ServiceProviderAggregate::add()   B
last analyzed

Complexity

Conditions 8
Paths 24

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.1867

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 12
cts 14
cp 0.8571
rs 8.4444
c 0
b 0
f 0
cc 8
nc 24
nop 1
crap 8.1867
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 15
    public function add($provider) : ServiceProviderAggregateInterface
27
    {
28 15
        if (is_string($provider) && $this->getContainer()->has($provider)) {
29
            $provider = $this->getContainer()->get($provider);
30 15
        } elseif (is_string($provider) && class_exists($provider)) {
31
            $provider = new $provider;
32
        }
33
34 15
        if ($provider instanceof ContainerAwareInterface) {
35 12
            $provider->setContainer($this->getContainer());
36
        }
37
38 15
        if ($provider instanceof BootableServiceProviderInterface) {
39 6
            $provider->boot();
40
        }
41
42 15
        if ($provider instanceof ServiceProviderInterface) {
43 12
            $this->providers[] = $provider;
44
45 12
            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 27
    public function provides(string $service) : bool
58
    {
59 27
        foreach ($this->getIterator() as $provider) {
60 12
            if ($provider->provides($service)) {
61 12
                return true;
62
            }
63
        }
64
65 15
        return false;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 27
    public function getIterator() : Generator
72
    {
73 27
        $count = count($this->providers);
74
75 27
        for ($i = 0; $i < $count; $i++) {
76 12
            yield $this->providers[$i];
77
        }
78 24
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 12
    public function register(string $service)
84
    {
85 12
        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 9
        foreach ($this->getIterator() as $provider) {
92 9
            if (in_array($provider->getIdentifier(), $this->registered)) {
93 3
                return;
94
            }
95
96 9
            $provider->register();
97
98 9
            $this->registered[] = $provider->getIdentifier();
99
        }
100 9
    }
101
}
102