InjectionParameterResolver::resolve()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 20
rs 9.6111
1
<?php
2
declare(strict_types=1);
3
4
namespace Habemus\Autowiring\Parameter;
5
6
use Habemus\Autowiring\Attributes\AttributesInjection;
7
use Habemus\Container;
8
use Habemus\Exception\UnresolvableParameterException;
9
use Psr\Container\ContainerInterface;
10
use ReflectionParameter;
11
12
class InjectionParameterResolver implements ParameterResolver
13
{
14
    /**
15
     * @var ContainerInterface
16
     */
17
    protected $container;
18
19
    /**
20
     * @var AttributesInjection
21
     */
22
    protected $injection;
23
24
    public function __construct(ContainerInterface $container, AttributesInjection $injection)
25
    {
26
        $this->container = $container;
27
        $this->injection = $injection;
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function resolve(ReflectionParameter $parameter, array $arguments, array &$result): bool
34
    {
35
        if ($this->container instanceof Container && !$this->container->attributesEnabled()) {
36
            return false;
37
        }
38
39
        $inject = $this->injection->getInjection($parameter);
40
        if (empty($inject)) {
41
            return false;
42
        }
43
44
        if (!$this->container->has($inject)) {
45
            throw UnresolvableParameterException::createForFunction(
46
                $parameter->getDeclaringFunction(),
47
                $parameter->getName()
48
            );
49
        }
50
51
        $value = $this->container->get($inject);
52
        return (new VariadicParameterResolver($value))->resolve($parameter, $arguments, $result);
53
    }
54
}
55