Passed
Push — main ( 2054fe...0c6c03 )
by Breno
02:23
created

InjectionParameterResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 22 5
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 ReflectionParameter;
10
11
class InjectionParameterResolver implements ParameterResolver
12
{
13
    /**
14
     * @var Container
15
     */
16
    protected $container;
17
18
    /**
19
     * @var AttributesInjection
20
     */
21
    protected $injection;
22
23
    public function __construct(Container $container, AttributesInjection $injection)
24
    {
25
        $this->container = $container;
26
        $this->injection = $injection;
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function resolve(ReflectionParameter $parameter, array $arguments, array &$resolved, array &$result): void
33
    {
34
        $name = $parameter->getName();
35
        if (array_key_exists($name, $resolved)) {
36
            return;
37
        }
38
39
        if (!$this->container->attributesEnabled()) {
40
            return;
41
        }
42
43
        $inject = $this->injection->getInjection($parameter);
44
        if ($inject === null) {
45
            return;
46
        }
47
48
        if (!$this->container->has($inject)) {
49
            throw UnresolvableParameterException::createForFunction($parameter->getDeclaringFunction(), $name);
50
        }
51
52
        $resolved[$name] = true;
53
        $result[] = $this->container->get($inject);
54
    }
55
}
56