Completed
Push — master ( 693288...ff001c )
by Andreas
10s
created

PimpleContainerAdapter::addApplicationConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
1
<?php
2
3
namespace TomPHP\ContainerConfigurator\Pimple;
4
5
use Assert\Assertion;
6
use Pimple\Container;
7
use TomPHP\ContainerConfigurator\ApplicationConfig;
8
use TomPHP\ContainerConfigurator\ContainerAdapter;
9
use TomPHP\ContainerConfigurator\Exception\UnsupportedFeatureException;
10
use TomPHP\ContainerConfigurator\InflectorConfig;
11
use TomPHP\ContainerConfigurator\ServiceConfig;
12
use TomPHP\ContainerConfigurator\ServiceDefinition;
13
14
final class PimpleContainerAdapter implements ContainerAdapter
15
{
16
    /**
17
     * @var Container
18
     */
19
    private $container;
20
21
    /**
22
     * @param Container $container
23
     */
24
    public function setContainer($container)
25
    {
26
        $this->container = $container;
27
    }
28
29
    public function addApplicationConfig(ApplicationConfig $config, $prefix = 'config')
30
    {
31
        Assertion::string($prefix);
32
33
        if (!empty($prefix)) {
34
            $prefix .= $config->getSeparator();
35
        }
36
37
        foreach ($config as $key => $value) {
38
            $this->container[$prefix . $key] = $value;
39
        }
40
    }
41
42
    public function addServiceConfig(ServiceConfig $config)
43
    {
44
        foreach ($config as $definition) {
45
            $this->addServiceToContainer($definition);
46
        }
47
    }
48
49
    /**
50
     * @throws UnsupportedFeatureException
51
     */
52
    public function addInflectorConfig(InflectorConfig $config)
53
    {
54
        throw UnsupportedFeatureException::forInflectors('Pimple');
55
    }
56
57
    private function addServiceToContainer(ServiceDefinition $definition)
58
    {
59
        $factory = $this->createFactory($definition);
60
61
        if (!$definition->isSingleton()) {
62
            $factory = $this->container->factory($factory);
63
        }
64
65
        $this->container[$definition->getName()] = $factory;
66
    }
67
68
    /**
69
     * @param ServiceDefinition $definition
70
     *
71
     * @return \Closure
72
     */
73
    private function createFactory(ServiceDefinition $definition)
74
    {
75
        return $definition->isFactory()
76
            ? $this->createFactoryFactory($definition)
77
            : $this->createInstanceFactory($definition);
78
    }
79
80
    /**
81
     * @param ServiceDefinition $definition
82
     *
83
     * @return \Closure
84
     */
85 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...
86
    {
87
        return function () use ($definition) {
88
            $className = $definition->getClass();
89
            $factory = new $className();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
90
91
            return $factory(...$this->resolveArguments($definition->getArguments()));
92
        };
93
    }
94
95
    /**
96
     * @param ServiceDefinition $definition
97
     *
98
     * @return \Closure
99
     */
100
    private function createInstanceFactory(ServiceDefinition $definition)
101
    {
102
        return function () use ($definition) {
103
            $className = $definition->getClass();
104
            $instance = new $className(...$this->resolveArguments($definition->getArguments()));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
105
106
            foreach ($definition->getMethods() as $name => $args) {
107
                $instance->$name(...$this->resolveArguments($args));
108
            }
109
110
            return $instance;
111
        };
112
    }
113
114
    /**
115
     * @param array $arguments
116
     *
117
     * @return array
118
     */
119 View Code Duplication
    private function resolveArguments(array $arguments)
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...
120
    {
121
        return array_map(
122
            function ($argument) {
123
                if (isset($this->container[$argument])) {
124
                    return $this->container[$argument];
125
                }
126
127
                return $argument;
128
            },
129
            $arguments
130
        );
131
    }
132
}
133