Completed
Push — master ( 5e4c69...6bf127 )
by Tom
02:18
created

ServiceServiceProvider::addMethodCalls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider\League;
4
5
use League\Container\Definition\ClassDefinition;
6
use League\Container\ServiceProvider\AbstractServiceProvider;
7
use TomPHP\ConfigServiceProvider\Exception\NotClassDefinitionException;
8
use TomPHP\ConfigServiceProvider\ServiceConfig;
9
use TomPHP\ConfigServiceProvider\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\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...
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
    private function registerService(ServiceDefinition $definition)
40
    {
41
        $service = $this->getContainer()->add(
42
            $definition->getName(),
43
            $definition->getClass(),
44
            $definition->isSingleton()
45
        );
46
47
        if (!$service instanceof ClassDefinition) {
48
            throw new NotClassDefinitionException(sprintf(
49
                'DI definition for %s does not create a class definition',
50
                $definition->getName()
51
            ));
52
        }
53
54
        $service->withArguments($definition->getArguments());
55
        $this->addMethodCalls($service, $definition);
56
    }
57
58
    /**
59
     * @param ClassDefinition   $service
60
     * @param ServiceDefinition $definition
61
     */
62
    private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition)
63
    {
64
        foreach ($definition->getMethods() as $method => $args) {
65
            $service->withMethodCall($method, $args);
66
        }
67
    }
68
}
69