Completed
Pull Request — master (#28)
by Tom
02:45
created

DIConfigServiceProvider::addConstuctorArguments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use League\Container\Definition\ClassDefinition;
6
use League\Container\ServiceProvider\AbstractServiceProvider;
7
use League\Container\ServiceProvider\BootableServiceProviderInterface;
8
use TomPHP\ConfigServiceProvider\Exception\NotClassDefinitionException;
9
10
final class DIConfigServiceProvider extends AbstractServiceProvider
11
{
12
    /**
13
     * @var array
14
     */
15
    private $config;
16
17
    /**
18
     * @api
19
     *
20
     * @param ServiceConfig $config
21
     */
22
    public function __construct(ServiceConfig $config)
23
    {
24
        $this->config   = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<TomPHP\ConfigServ...Provider\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...
25
        $this->provides = $config->getKeys();
26
    }
27
28
    /**
29
     * @param array  $config
30
     */
31
    public function configure(array $config)
32
    {
33
        $this->provides = array_keys($config);
34
        $this->config   = $config;
35
    }
36
37
    public function register()
38
    {
39
        foreach ($this->config as $config) {
40
            $this->registerService($config);
41
        }
42
    }
43
44
    /**
45
     * @param ServiceDefinition  $definition
46
     */
47
    private function registerService(ServiceDefinition $definition)
48
    {
49
        $service = $this->getContainer()->add(
50
            $definition->getKey(),
51
            $definition->getClass(),
52
            $definition->isSingleton()
53
        );
54
55
        if (!$service instanceof ClassDefinition) {
56
            throw new NotClassDefinitionException(sprintf(
57
                'DI definition for %s does not create a class definition',
58
                $definition->getKey()
59
            ));
60
        }
61
62
        $service->withArguments($definition->getArguments());
63
        $this->addMethodCalls($service, $definition);
64
    }
65
66
    /**
67
     * @param ClassDefinition   $service
68
     * @param ServiceDefinition $definition
69
     */
70
    private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition)
71
    {
72
        foreach ($definition->getMethods() as $method => $args) {
73
            $service->withMethodCall($method, $args);
74
        }
75
    }
76
}
77