Completed
Push64fa5b...9845d4
passed — Build
created

FactoryParameterResolver::getParameters()   C

↳ Parent: FactoryParameterResolver

Complexity

Conditions 7
Paths 12

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 31
Code Lines 18

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 7
eloc 18
c 4
b 1
f 0
nc 12
nop 3
dl 0
loc 31
rs 6.7272
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
 * [email protected] This class is similar to TypeHintingResolver and TypeHintingContainerResolver,
13
 *            we use this instead for performance reasons}
14
 *
15
 * @author Quim Calpe <[email protected]>
16
 * @author Matthieu Napoli <[email protected]>
17
 */
18
class FactoryParameterResolver implements ParameterResolver
19
{
20
    /**
21
     * @var ContainerInterface
22
     */
23
    private $container;
24
25
    public function __construct(ContainerInterface $container)
26
    {
27
        $this->container = $container;
28
    }
29
30
    public function getParameters(
31
        ReflectionFunctionAbstract $reflection,
32
        array $providedParameters,
33
        array $resolvedParameters
34
    ) {
35
        $parameters = $reflection->getParameters();
36
37
        // Skip parameters already resolved
38
        if (! empty($resolvedParameters)) {
39
            $parameters = array_diff_key($parameters, $resolvedParameters);
40
        }
41
42
        foreach ($parameters as $index => $parameter) {
43
            $parameterClass = $parameter->getClass();
44
45
            if (!$parameterClass) {
46
                continue;
47
            }
48
49
            if ($parameterClass->name === 'Interop\Container\ContainerInterface') {
50
                $resolvedParameters[$index] = $this->container;
51
            } elseif ($parameterClass->name === 'DI\Factory\RequestedEntry') {
52
                // By convention the second parameter is the definition
53
                $resolvedParameters[$index] = $providedParameters[1];
54
            } elseif ($this->container->has($parameterClass->name)) {
55
                $resolvedParameters[$index] = $this->container->get($parameterClass->name);
56
            }
57
        }
58
59
        return $resolvedParameters;
60
    }
61
}
62