Completed
Pull Request — master (#4)
by Alexpts
04:08
created

Cascader::getReflectionClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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 12
    public function create(string $className, array $options)
26
    {
27 12
        $reflectionClass = $this->getReflectionClass($className);
28 10
        $options = Options::fromArray($options);
29
30 9
        $arguments = $this->marshalArguments($reflectionClass, $options);
31
32 8
        return new $className(...$arguments);
33
    }
34
35 12
    protected function getReflectionClass(string $className) : ReflectionClass
36
    {
37
        try {
38 12
            $reflectionClass = new ReflectionClass($className);
39 1
        } catch (\ReflectionException $ex) {
40 1
            throw InvalidClassException::forNonExistingClass($className);
41
        }
42
43 11
        if (! $reflectionClass->isInstantiable()) {
44 1
            throw InvalidClassException::forNonInstantiableClass($className);
45
        }
46
47 10
        return $reflectionClass;
48
    }
49
50 9
    protected function marshalArguments(ReflectionClass $reflectionClass, Options $options) : array
51
    {
52 9
        $constructor = $reflectionClass->getConstructor();
53
54 9
        if (null === $constructor) {
55 1
            return [];
56
        }
57
58 8
        $arguments = [];
59
60 8
        $constructorParameters = $constructor->getParameters();
61
62 8
        foreach ($constructorParameters as $parameter) {
63 8
            $arguments[] = $this->resolveArgument($parameter, $options);
64
        }
65
66 7
        return $arguments;
67
    }
68
69 8
    protected function resolveArgument(ReflectionParameter $parameter, Options $options)
70
    {
71
        try {
72 8
            $argument = $options->get($parameter->name);
73
74 7
            if ((string)$parameter->getType() === 'array') {
75 3
                return $this->resoleArrayArgument($argument);
76
            }
77
78 7
            if (null !== ($parameterType = $parameter->getType()) && $this->shouldResolveObjectArgument($parameterType, $argument)) {
79 2
                $argument = $this->resolveObjectArgument((string) $parameterType, $argument);
80
            }
81
82 7
            return $argument;
83 6
        } catch (OptionNotSetException $ex) {
84 6
            if (! $parameter->isOptional()) {
85 1
                throw InvalidOptionsException::forMissingMandatoryParameter($parameter);
86
            }
87
88 5
            return $parameter->getDefaultValue();
89
        }
90
    }
91
92 3
    protected function resoleArrayArgument(array $argument = []): array
93
    {
94 3
        foreach ($argument as $k => $value) {
95 3
            if (\is_array($value) && isset($value['__class__'])) {
96 3
                $argument[$k] = $this->resolveObjectArgument('', $value);
97
            }
98
        }
99
100 3
        return $argument;
101
    }
102
103 7
    protected function shouldResolveObjectArgument(ReflectionType $parameterType, $argument) : bool
104
    {
105 7
        return !$parameterType->isBuiltin() && \is_array($argument);
106
    }
107
108 3
    protected function resolveObjectArgument(string $className, array $arguments)
109
    {
110 3
        if (isset($arguments['__class__'])) {
111 2
            $className = $arguments['__class__'];
112 2
            unset($arguments['__class__']);
113
        }
114
115 3
        return $this->create($className, $arguments);
116
    }
117
}
118