Completed
Pull Request — master (#39)
by Tom
10:47 queued 06:00
created

PimpleContainerAdapter::resolveArgument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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
    /**
72
     * @param array $arguments
73
     *
74
     * @return array
75
     */
76
    private function resolveArguments(array $arguments)
77
    {
78
        return array_map([$this, 'resolveArgument'], $arguments);
79
    }
80
81
    private function resolveArgument($argument)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
82
    {
83
        if (!isset($this->container[$argument])) {
84
            return $argument;
85
        }
86
87
        return $this->container[$argument];
88
    }
89
}
90