Completed
Pushe95b0b...773145
passed — Build
created

FactoryParameterResolver::__construct()   A

↳ Parent: FactoryParameterResolver

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace DI\Invoker;
4
5
use Interop\Container\ContainerInterface;
6
use Invoker\ParameterResolver\ParameterResolver;
7
use ReflectionFunctionAbstract;
8
9
/**
10
 * Inject the container, the definition or any other service using type-hints.
11
 *
12
 * @author Quim Calpe <[email protected]>
13
 * @author Matthieu Napoli <[email protected]>
14
 */
15
class FactoryParameterResolver implements ParameterResolver
16
{
17
    /**
18
     * @var ContainerInterface
19
     */
20
    private $container;
21
22
    public function __construct(ContainerInterface $container)
23
    {
24
        $this->container = $container;
25
    }
26
27
    public function getParameters(
28
        ReflectionFunctionAbstract $reflection,
29
        array $providedParameters,
30
        array $resolvedParameters
31
    ) {
32
        foreach ($reflection->getParameters() as $index => $parameter) {
33
            $parameterClass = $parameter->getClass();
34
35
            if (!$parameterClass) {
36
                continue;
37
            }
38
39
            if ($parameterClass->name === 'Interop\Container\ContainerInterface') {
40
                $resolvedParameters[$index] = $this->container;
41
            } elseif ($parameterClass->name === 'DI\Factory\RequestedEntry') {
42
                // By convention the second parameter is the definition
43
                $resolvedParameters[$index] = $providedParameters[1];
44
            } elseif ($this->container->has($parameterClass->name)) {
45
                $resolvedParameters[$index] = $this->container->get($parameterClass->name);
46
            }
47
        }
48
49
        return $resolvedParameters;
50
    }
51
}
52