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

InjectionParameterResolver::resolve()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 5
nop 4
dl 0
loc 22
rs 9.5555
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

 

OSZAR »