Completed
Pull Request — master (#28)
by Tom
03:24
created

DIConfigServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 7
c 5
b 1
f 3
lcom 1
cbo 6
dl 0
loc 58
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A register() 0 6 2
A registerService() 0 18 2
A addMethodCalls() 0 6 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
    public function register()
29
    {
30
        foreach ($this->config as $config) {
31
            $this->registerService($config);
32
        }
33
    }
34
35
    /**
36
     * @param ServiceDefinition  $definition
37
     */
38
    private function registerService(ServiceDefinition $definition)
39
    {
40
        $service = $this->getContainer()->add(
41
            $definition->getName(),
42
            $definition->getClass(),
43
            $definition->isSingleton()
44
        );
45
46
        if (!$service instanceof ClassDefinition) {
47
            throw new NotClassDefinitionException(sprintf(
48
                'DI definition for %s does not create a class definition',
49
                $definition->getName()
50
            ));
51
        }
52
53
        $service->withArguments($definition->getArguments());
54
        $this->addMethodCalls($service, $definition);
55
    }
56
57
    /**
58
     * @param ClassDefinition   $service
59
     * @param ServiceDefinition $definition
60
     */
61
    private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition)
62
    {
63
        foreach ($definition->getMethods() as $method => $args) {
64
            $service->withMethodCall($method, $args);
65
        }
66
    }
67
}
68