Completed
Push — master ( 03d9da...2dac9a )
by Tom
13s
created

ServiceServiceProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace TomPHP\ContainerConfigurator\League;
4
5
use League\Container\Definition\ClassDefinition;
6
use League\Container\ServiceProvider\AbstractServiceProvider;
7
use TomPHP\ContainerConfigurator\Exception\NotClassDefinitionException;
8
use TomPHP\ContainerConfigurator\ServiceConfig;
9
use TomPHP\ContainerConfigurator\ServiceDefinition;
10
11
final class ServiceServiceProvider extends AbstractServiceProvider
12
{
13
    /**
14
     * @var array
15
     */
16
    private $config;
17
18
    /**
19
     * @api
20
     *
21
     * @param ServiceConfig $config
22
     */
23
    public function __construct(ServiceConfig $config)
24
    {
25
        $this->config   = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<TomPHP\ContainerC...igurator\ServiceConfig> is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
        $this->provides = $config->getKeys();
27
    }
28
29
    public function register()
30
    {
31
        foreach ($this->config as $config) {
32
            $this->registerService($config);
33
        }
34
    }
35
36
    /**
37
     * @param ServiceDefinition $definition
38
     *
39
     * @return void
40
     *
41
     * @throws NotClassDefinitionException
42
     */
43
    private function registerService(ServiceDefinition $definition)
44
    {
45
        $service = $this->getContainer()->add(
46
            $definition->getName(),
47
            $definition->getClass(),
48
            $definition->isSingleton()
49
        );
50
51
        if (!$service instanceof ClassDefinition) {
52
            throw NotClassDefinitionException::fromServiceName($definition->getName());
53
        }
54
55
        $service->withArguments($definition->getArguments());
56
        $this->addMethodCalls($service, $definition);
57
    }
58
59
    /**
60
     * @param ClassDefinition   $service
61
     * @param ServiceDefinition $definition
62
     */
63
    private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition)
64
    {
65
        foreach ($definition->getMethods() as $method => $args) {
66
            $service->withMethodCall($method, $args);
67
        }
68
    }
69
}
70