Completed
Push — 5.0 ( 9430f0...7305a8 )
by Marc André
03:05 queued 01:41
created

ControllerReflection::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 10
nc 8
nop 2
1
<?php
2
3
4
/**
5
 *
6
 * Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <[email protected]>. All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without modification, are
9
 * permitted provided that the following conditions are met:
10
 *
11
 *   1. Redistributions of source code must retain the above copyright notice, this list of
12
 *       conditions and the following disclaimer.
13
 *
14
 *   2. Redistributions in binary form must reproduce the above copyright notice, this list
15
 *       of conditions and the following disclaimer in the documentation and/or other materials
16
 *       provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL NEVRAXE INC. & MARC ANDRÉ AUDET BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 */
30
31
32
namespace Cervo;
33
34
35
use Cervo\Exceptions\ControllerReflection\InvalidControllerException;
36
use Cervo\Interfaces\ControllerInterface;
37
use Cervo\Utils\ClassUtils;
38
39
40
/**
41
 * Class ControllerReflection
42
 * @package Cervo
43
 */
44
final class ControllerReflection
45
{
46
    /** @var Context */
47
    private $context;
48
49
    /** @var Route */
50
    private $route;
51
52
    /** @var array */
53
    private $parameters = [];
54
55
    /**
56
     * ControllerReflection constructor.
57
     *
58
     * @param Context $context
59
     * @param Route $route
60
     */
61
    public function __construct(Context $context, Route $route)
62
    {
63
        if (!ClassUtils::implements($route->getControllerClass(), ControllerInterface::class)) {
64
            throw new InvalidControllerException;
65
        }
66
67
        $this->context = $context;
68
        $this->route = $route;
69
70
        try {
71
72
            $reflection = new \ReflectionClass($this->route->getControllerClass());
73
74
            foreach ($reflection->getConstructor()->getParameters() as $parameter) {
75
                $this->parameters[] = $this->getParameterValue($parameter);
76
            }
77
78
        } catch (\ReflectionException $e) {
79
            // The contructor isn't defined, so we ignore the exception and move on
80
        }
81
    }
82
83
    public function __invoke() : void
84
    {
85
        $controller_class = $this->route->getControllerClass();
86
        (new $controller_class(...$this->parameters))();
87
    }
88
89
    private function getParameterValue(\ReflectionParameter $parameter)
90
    {
91
        if ($parameter->getClass() === null) {
92
93
            if ($parameter->isArray()) {
94
95
                if ($parameter->getName() == 'parameters') {
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
96
                    return $this->route->getParameters();
97
                } elseif ($parameter->getName() == 'arguments') {
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
98
                    return $this->route->getArguments();
99
                } else {
100
                    return $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : [];
101
                }
102
103
            } else {
104
                return $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null;
105
            }
106
107
        } else {
108
            return $this->context->getSingletons()->get($parameter->getClass()->getName());
0 ignored issues
show
Bug introduced by
Consider using $parameter->getClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
109
        }
110
    }
111
}
112