Completed
Pull Request — master (#40)
by Tom
04:05
created

PimpleContainerAdapter::addApplicationConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 4
nop 2
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider\Pimple;
4
5
use Pimple\Container;
6
use TomPHP\ConfigServiceProvider\ApplicationConfig;
7
use TomPHP\ConfigServiceProvider\ContainerAdapter;
8
use TomPHP\ConfigServiceProvider\Exception\UnsupportedFeatureException;
9
use TomPHP\ConfigServiceProvider\InflectorConfig;
10
use TomPHP\ConfigServiceProvider\ServiceConfig;
11
use TomPHP\ConfigServiceProvider\ServiceDefinition;
12
13
final class PimpleContainerAdapter implements ContainerAdapter
14
{
15
    /**
16
     * @var Container
17
     */
18
    private $container;
19
20
    /**
21
     * @param Container $container
22
     */
23
    public function setContainer($container)
24
    {
25
        $this->container = $container;
26
    }
27
28
    public function addApplicationConfig(ApplicationConfig $config, $prefix = 'config')
29
    {
30
        if (!empty($prefix)) {
31
            $prefix .= $config->getSeparator();
32
        }
33
34
        foreach ($config as $key => $value) {
35
            $this->container[$prefix . $key] = $value;
36
        }
37
    }
38
39
    public function addServiceConfig(ServiceConfig $config)
40
    {
41
        foreach ($config as $definition) {
42
            $this->addServiceToContainer($definition);
43
        }
44
    }
45
46
    public function addInflectorConfig(InflectorConfig $config)
47
    {
48
        throw UnsupportedFeatureException::forInflectors('Pimple');
49
    }
50
51
    private function addServiceToContainer(ServiceDefinition $definition)
52
    {
53
        $factory = function () use ($definition) {
54
            $className = $definition->getClass();
55
            $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...
56
57
            foreach ($definition->getMethods() as $name => $args) {
58
                $instance->$name(...$this->resolveArguments($args));
59
            }
60
61
            return $instance;
62
        };
63
64
        if (!$definition->isSingleton()) {
65
            $factory = $this->container->factory($factory);
66
        }
67
68
        $this->container[$definition->getName()] = $factory;
69
    }
70
71
    private function resolveArguments(array $arguments)
72
    {
73
        return array_map(
74
            function ($argument) {
75
                if (isset($this->container[$argument])) {
76
                    return $this->container[$argument];
77
                }
78
79
                return $argument;
80
            },
81
            $arguments
82
        );
83
    }
84
}
85