Passed
Push — master ( df4ff7...e740a4 )
by Divine Niiquaye
01:54
created

ClassValueResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 43
ccs 16
cts 16
cp 1
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B resolve() 0 30 9
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of PHP Invoker.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace DivineNii\Invoker\ArgumentResolver;
19
20
use ArgumentCountError;
21
use DivineNii\Invoker\Interfaces\ArgumentValueResolverInterface;
22
use Psr\Container\ContainerInterface;
23
use Psr\Container\NotFoundExceptionInterface;
24
use ReflectionParameter;
25
26
/**
27
 * Inject or create a class instance from a DI container or return existing instance
28
 * from $providedParameters using the type-hints.
29
 *
30
 * @author Divine Niiquaye Ibok <[email protected]>
31
 */
32
class ClassValueResolver implements ArgumentValueResolverInterface
33
{
34
    /** @var ContainerInterface|null */
35
    private $container;
36
37 53
    public function __construct(?ContainerInterface $container = null)
38
    {
39 53
        $this->container = $container;
40 53
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 27
    public function resolve(ReflectionParameter $parameter, array $providedParameters)
46
    {
47 27
        $parameterType = $parameter->getType();
48
49 27
        if (!$parameterType instanceof \ReflectionNamedType || $parameterType->isBuiltin()) {
50
            // No type, Primitive types and Union types are not supported
51 17
            return;
52
        }
53
54
        // Inject entries from a DI container using the type-hints.
55 11
        if (null !== $this->container) {
56
            try {
57 3
                return $this->container->get($parameterType->getName());
58 1
            } catch (NotFoundExceptionInterface $e) {
59
                // We need no exception thrown here
60
            }
61
        }
62
63
        // If an instance is detected
64 9
        foreach ($providedParameters as $key => $value) {
65 4
            if (\is_a($value, $parameterType->getName(), true)) {
66 4
                return $providedParameters[$key];
67
            }
68
        }
69
70
        try {
71 5
            if (\class_exists($class = $parameterType->getName())) {
72 5
                return new $class();
73
            }
74 1
        } catch (ArgumentCountError $e) {
75
            // Throw no exception ...
76
        }
77 2
    }
78
}
79