Completed
Push — master ( 1bae27...fe1a3e )
by Andreas
02:24
created

ServiceServiceProvider::createAliasFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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
     * @throws NotClassDefinitionException
40
     *
41
     * @return void
42
     */
43
    private function registerService(ServiceDefinition $definition)
44
    {
45
        if ($definition->isFactory()) {
46
            $this->getContainer()->add(
47
                $definition->getName(),
48
                $this->createFactoryFactory($definition),
49
                $definition->isSingleton()
50
            );
51
52
            return;
53
        }
54
55
        if ($definition->isAlias()) {
56
            $this->getContainer()->add(
57
                $definition->getName(),
58
                $this->createAliasFactory($definition)
59
            );
60
61
            return;
62
        }
63
64
        $service = $this->getContainer()->add(
65
            $definition->getName(),
66
            $definition->getClass(),
67
            $definition->isSingleton()
68
        );
69
70
        if (!$service instanceof ClassDefinition) {
71
            throw NotClassDefinitionException::fromServiceName($definition->getName());
72
        }
73
74
        $service->withArguments($definition->getArguments());
75
        $this->addMethodCalls($service, $definition);
76
    }
77
78
    /**
79
     * @param ClassDefinition   $service
80
     * @param ServiceDefinition $definition
81
     */
82
    private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition)
83
    {
84
        foreach ($definition->getMethods() as $method => $args) {
85
            $service->withMethodCall($method, $args);
86
        }
87
    }
88
89
    /**
90
     * @param ServiceDefinition $definition
91
     *
92
     * @return \Closure
93
     */
94
    private function createAliasFactory(ServiceDefinition $definition)
95
    {
96
        return function () use ($definition) {
97
            return $this->getContainer()->get($definition->getClass());
98
        };
99
    }
100
101
    /**
102
     * @param ServiceDefinition $definition
103
     *
104
     * @return \Closure
105
     */
106 View Code Duplication
    private function createFactoryFactory(ServiceDefinition $definition)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        return function () use ($definition) {
109
            $className = $definition->getClass();
110
            $factory   = new $className();
111
112
            return $factory(...$this->resolveArguments($definition->getArguments()));
113
        };
114
    }
115
116
    /**
117
     * @param array $arguments
118
     *
119
     * @return array
120
     */
121
    private function resolveArguments(array $arguments)
122
    {
123
        return array_map(
124
            function ($argument) {
125
                if ($this->container->has($argument)) {
126
                    return $this->container->get($argument);
127
                }
128
129
                return $argument;
130
            },
131
            $arguments
132
        );
133
    }
134
}
135