Completed
Push — 4.x ( a2ec53...8fa263 )
by Phil
13:00 queued 10:07
created

ServiceProviderAggregate   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.59%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 83
ccs 35
cts 37
cp 0.9459
rs 10
c 0
b 0
f 0

4 Methods

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