Completed
Push — master ( 8e8e44...d679b5 )
by Restu
13:25
created

RouteHandle::getRouteByPath()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 5
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 11
rs 9.2
1
<?php
2
namespace JayaCode\Framework\Core\Route;
3
4
use JayaCode\Framework\Core\Http\Request;
5
use JayaCode\Framework\Core\Http\Response;
6
7
class RouteHandle
8
{
9
    /**
10
     * @var array
11
     */
12
    public $routes = array();
13
14
    /**
15
     * @var Request
16
     */
17
    public $request;
18
19
    /**
20
     * @var Response
21
     */
22
    public $response;
23
24
    /**
25
     * Route constructor.
26
     * @param Request $request
27
     * @param Response $response
28
     */
29
    public function __construct(Request &$request = null, Response &$response = null)
30
    {
31
        $this->initialize($request, $response);
32
    }
33
34
    /**
35
     * initialize Route
36
     * @param Request $request
37
     * @param Response $response
38
     */
39
    public function initialize(Request &$request = null, Response &$response = null)
40
    {
41
        $this->request = &$request;
42
        $this->response = &$response;
43
    }
44
45
    /**
46
     * Create Route object from static function
47
     * @param Request $request
48
     * @param Response $response
49
     * @return Route
50
     */
51
    public static function create(Request &$request = null, Response &$response = null)
52
    {
53
        return new RouteHandle($request, $response);
54
    }
55
56
    /**
57
     * Handle request
58
     */
59
    public function handle()
60
    {
61
        $path = $this->request->path();
62
        $method = $this->request->method();
63
        $route = $this->getRouteByPath($path, $method);
64
65
        if (!is_null($route) && is_callable($route->action)) {
66
            // if use callback function
67
            $this->handleCallback($route->action);
68
        } elseif (!is_null($route) && $route->controller && is_string($route->action)) {
69
            // if use controller
70
            $this->handleController($route);
0 ignored issues
show
Documentation introduced by
$route is of type array, but the function expects a object<JayaCode\Framework\Core\Route\Route>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
        } else {
72
            $this->response->setNotFound($path);
73
        }
74
75
        return $this->response;
76
    }
77
78
    /**
79
     * Handle route with action callback
80
     * @param $func
81
     */
82
    private function handleCallback($func)
83
    {
84
        $content = call_user_func($func);
85
        $this->response->setContent($content);
86
    }
87
88
    /**
89
     * Handle Controller
90
     * @param Route $route
91
     */
92
    private function handleController(Route $route)
93
    {
94
        $controller_class = "App\\Controller\\" . $route->controller;
95
96
        if (!class_exists($controller_class)) {
97
            throw new \InvalidArgumentException("controller " . $controller_class . " not found");
98
        }
99
100
        $controller = new $controller_class($this->request, $this->response);
101
102
        $action = $route->action;
103
104
        if (!method_exists($controller, $action)) {
105
            throw new \InvalidArgumentException("method " . $action . " not found");
106
        }
107
108
        $content = $controller->$action();
109
        $this->response->setContent($content);
110
    }
111
112
    /**
113
     * @param string $id
114
     * @return array
115
     */
116
    public function getRoute($id = null)
117
    {
118
        if (is_null($id)) {
119
            return $this->routes;
120
        }
121
122
        if (!is_string($id)) {
123
            throw new \InvalidArgumentException("var id must be a string");
124
        }
125
126
        foreach ($this->routes as $route) {
127
            if ($route->id == $id) {
128
                return $route;
129
            }
130
        }
131
132
        throw new \OutOfBoundsException("not found route with id " . $id);
133
    }
134
135
    /**
136
     * @param string $path
137
     * @param string $method
138
     * @return array
139
     */
140
    public function getRouteByPath($path, $method)
141
    {
142
143
        foreach ($this->routes as $route) {
144
            if ($route->path == $path && $route->method == $method) {
145
                return $route;
146
            }
147
        }
148
149
        return null;
150
    }
151
152
    /**
153
     * @param array $routes
154
     */
155
    public function setRoutes($routes)
156
    {
157
        $this->routes = $routes;
158
    }
159
}
160