Completed
Push — master ( 03d9da...2dac9a )
by Tom
13s
created

PimpleContainerAdapter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 5
dl 0
loc 77
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 4 1
A addApplicationConfig() 0 12 3
A addServiceConfig() 0 6 2
A addInflectorConfig() 0 4 1
A addServiceToContainer() 0 19 3
A resolveArguments() 0 13 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 = function () use ($definition) {
60
            $className = $definition->getClass();
61
            $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...
62
63
            foreach ($definition->getMethods() as $name => $args) {
64
                $instance->$name(...$this->resolveArguments($args));
65
            }
66
67
            return $instance;
68
        };
69
70
        if (!$definition->isSingleton()) {
71
            $factory = $this->container->factory($factory);
72
        }
73
74
        $this->container[$definition->getName()] = $factory;
75
    }
76
77
    private function resolveArguments(array $arguments)
78
    {
79
        return array_map(
80
            function ($argument) {
81
                if (isset($this->container[$argument])) {
82
                    return $this->container[$argument];
83
                }
84
85
                return $argument;
86
            },
87
            $arguments
88
        );
89
    }
90
}
91