Completed
Push — master ( a83045...68c206 )
by Lanre
02:46
created

Cfar::doesRouteHaveAValidDeclaration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
ccs 3
cts 4
cp 0.75
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Adelowo\Cfar;
4
5
use Aura\Router\Route;
6
7
/**
8
 * CFAR -- Controller for Aura.Router
9
 * @author Adelowo Lanre <[email protected]>
10
 * Allows you to use controllers and methods (laravel/symfony style) for `Aura.Router` 3.x.
11
 * If you still make use of `Aura.Router` 2.x, please see the <= 0.2.* releases
12
 */
13
class Cfar
14
{
15
16
    /**
17
     * This is here to bail the current request if a "listener" was not defined on the class.
18
     * @var string
19
     */
20
    const CFAR_DEFAULT_METHOD = 'indexAction';
21
22
    /**
23
     * Aura.Router Instance
24
     * @var Route
25
     */
26
    protected $matchedRoute;
27
28
    /**
29
     * The name of the called controller class (plus it's fully quantified namespace).
30
     * @var string
31
     */
32
    protected $controller;
33
34
    /**
35
     * The name of the method invoked on the called class .
36
     * @var string
37
     */
38
    protected $method;
39
40
    /**
41
     * The parameters to be passed unto the method.
42
     * @var array
43
     */
44
    protected $parameters;
45
46
    /**
47
     * @param Route $router
48
     */
49 3
    public function __construct(Route $router)
50
    {
51 3
        $this->matchedRoute = $router;
52 3
    }
53
54
    /**
55
     * Returns an array of parameters for the matched route.
56
     * @return array
57
     */
58 1
    public function getParameters()
59
    {
60 1
        return $this->parameters;
61
    }
62
63
    /**
64
     * Returns a string that denotes the called class plus it's namespace
65
     * @return string
66
     */
67 1
    public function getController()
68
    {
69 1
        return $this->controller;
70
    }
71
72
    /**
73
     * Returns a string that denotes the method that was invoked.
74
     * @return string
75
     */
76 2
    public function getMethod()
77
    {
78 2
        return $this->method;
79
    }
80
81
    /**
82
     * Calls the controller associated with the route and invoke the specified method.
83
     * @return void
84
     */
85 3
    public function dispatch()
86
    {
87 3
        $this->doesRouteHaveAValidDeclaration();
88
89 2
        list($this->controller, $this->method) = $this->getRegisteredControllerAndMethod();
90
91 2
        $this->parameters = $this->matchedRoute->attributes;
92
93 2
        $this->getReflectionClass($this->controller)
94 2
            ->getMethod($this->method)
95 2
            ->invokeArgs(new $this->controller, $this->parameters);
96
97 2
    }
98
99
    /**
100
     * @return array
101
     */
102 2
    protected function getRegisteredControllerAndMethod()
103
    {
104
        return [
105 2
            $this->matchedRoute->handler,
106 2
            array_key_exists('listener',
107 2
                $this->matchedRoute->extras) ? $this->matchedRoute->extras['listener'] : self::CFAR_DEFAULT_METHOD
108 2
        ];
109
    }
110
111
    /**
112
     * @return bool
113
     * @throws CfarException if the class can't be called
114
     */
115 3
    protected function doesRouteHaveAValidDeclaration()
116
    {
117
118 3
        if ($this->getReflectionClass($this->matchedRoute->handler)) {
119 2
            return true;
120
        }
121
122
        throw new CfarException("Invalid Route Declaration");
123
    }
124
125
126
    /**
127
     * @param $class string|object The class to resolve
128
     * @return \ReflectionClass
129
     */
130 3
    protected function getReflectionClass($class)
131
    {
132 3
        return new \ReflectionClass($class);
133
    }
134
}
135