Completed
Push — master ( fd7d0f...a46373 )
by Nikola
01:14
created

Cascader::shouldResolveObjectArgument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cascader;
6
7
use Cascader\Exception\InvalidClassException;
8
use Cascader\Exception\InvalidOptionsException;
9
use Cascader\Exception\OptionNotSetException;
10
use ReflectionClass;
11
use ReflectionParameter;
12
use ReflectionType;
13
14
class Cascader
15
{
16
    /**
17
     * @param string $className
18
     * @param array $options
19
     *
20
     * @throws InvalidClassException
21
     * @throws InvalidOptionsException
22
     *
23
     * @return object
24
     */
25 11
    public function create(string $className, array $options)
26
    {
27 11
        $reflectionClass = $this->getReflectionClass($className);
28 9
        $options = Options::fromArray($options);
29
30 8
        $arguments = $this->marshalArguments($reflectionClass, $options);
31
32 7
        return new $className(...$arguments);
33
    }
34
35 11
    protected function getReflectionClass(string $className) : ReflectionClass
36
    {
37
        try {
38 11
            $reflectionClass = new ReflectionClass($className);
39 1
        } catch (\ReflectionException $ex) {
40 1
            throw InvalidClassException::forNonExistingClass($className);
41
        }
42
43 10
        if (! $reflectionClass->isInstantiable()) {
44 1
            throw InvalidClassException::forNonInstantiableClass($className);
45
        }
46
47 9
        return $reflectionClass;
48
    }
49
50 8
    protected function marshalArguments(ReflectionClass $reflectionClass, Options $options) : array
51
    {
52 8
        $constructor = $reflectionClass->getConstructor();
53
54 8
        if (null === $constructor) {
55 1
            return [];
56
        }
57
58 7
        $arguments = [];
59
60 7
        $constructorParameters = $constructor->getParameters();
61
62 7
        foreach ($constructorParameters as $parameter) {
63 7
            $arguments[] = $this->resolveArgument($parameter, $options);
64
        }
65
66 6
        return $arguments;
67
    }
68
69 7
    protected function resolveArgument(ReflectionParameter $parameter, Options $options)
70
    {
71
        try {
72 7
            $argument = $options->get($parameter->name);
73
74 6
            if (null !== ($parameterType = $parameter->getType()) && $this->shouldResolveObjectArgument($parameterType, $argument)) {
75 2
                $argument = $this->resolveObjectArgument((string) $parameterType, $argument);
76
            }
77
78 6
            return $argument;
79 5
        } catch (OptionNotSetException $ex) {
80 5
            if (! $parameter->isOptional()) {
81 1
                throw InvalidOptionsException::forMissingMandatoryParameter($parameter);
82
            }
83
84 4
            return $parameter->getDefaultValue();
85
        }
86
    }
87
88 6
    protected function shouldResolveObjectArgument(ReflectionType $parameterType, $argument) : bool
89
    {
90 6
        return !$parameterType->isBuiltin() && \is_array($argument);
91
    }
92
93 2
    protected function resolveObjectArgument(string $className, array $arguments)
94
    {
95 2
        if (isset($arguments['__class__'])) {
96 1
            $className = $arguments['__class__'];
97 1
            unset($arguments['__class__']);
98
        }
99
100 2
        return $this->create($className, $arguments);
101
    }
102
}
103