Completed
Push — master ( 9e7db4...93f997 )
by Restu
11:20
created

RouteHandle   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 0
loc 173
rs 10
wmc 25
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A initialize() 0 5 1
A create() 0 4 1
A handle() 0 12 2
B tryHandleRequest() 0 20 6
A handleCallback() 0 5 1
A handleController() 0 19 3
B getRoute() 0 18 5
A getRouteByPath() 0 11 4
A setRoutes() 0 4 1
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
/**
8
 * Class RouteHandle
9
 * @package JayaCode\Framework\Core\Route
10
 */
11
class RouteHandle
12
{
13
    /**
14
     * @var array
15
     */
16
    public $routes = array();
17
18
    /**
19
     * @var Request
20
     */
21
    public $request;
22
23
    /**
24
     * @var Response
25
     */
26
    public $response;
27
28
    /**
29
     * Route constructor.
30
     * @param Request $request
31
     * @param Response $response
32
     */
33
    public function __construct(Request &$request = null, Response &$response = null)
34
    {
35
        $this->initialize($request, $response);
36
    }
37
38
    /**
39
     * initialize Route
40
     * @param Request $request
41
     * @param Response $response
42
     */
43
    public function initialize(Request &$request = null, Response &$response = null)
44
    {
45
        $this->request = &$request;
46
        $this->response = &$response;
47
    }
48
49
    /**
50
     * Create Route object from static function
51
     * @param Request $request
52
     * @param Response $response
53
     * @return RouteHandle
54
     */
55
    public static function create(Request &$request = null, Response &$response = null)
56
    {
57
        return new RouteHandle($request, $response);
58
    }
59
60
    /**
61
     * Handle request
62
     */
63
    public function handle()
64
    {
65
        $path = $this->request->path();
66
        $method = $this->request->method();
67
        $route = $this->getRouteByPath($path, $method);
68
69
        if (!$this->tryHandleRequest($route)) {
70
            $this->response->setNotFound($path);
71
        }
72
73
        return $this->response;
74
    }
75
76
    /**
77
     * Try to handle request if success return true
78
     * @param $route
79
     * @return bool
80
     */
81
    private function tryHandleRequest($route)
82
    {
83
        if (is_null($route)) {
84
            return false;
85
        }
86
87
        // is callback function
88
        if (is_callable($route->action)) {
89
            $this->handleCallback($route->action);
90
            return true;
91
        }
92
93
        // is class controller
94
        if (is_array($route->action) && arr_get($route->action, "controller") && arr_get($route->action, "method")) {
95
            $this->handleController($route);
96
            return true;
97
        }
98
99
        return false;
100
    }
101
102
    /**
103
     * Handle route with action callback
104
     * @param callable $func
105
     */
106
    private function handleCallback(callable $func)
107
    {
108
        $content = call_user_func($func);
109
        $this->response->setContent($content);
110
    }
111
112
    /**
113
     * Handle Controller
114
     * @param Route $route
115
     */
116
    private function handleController(Route $route)
117
    {
118
        $controllerName = "App\\Controller\\" . $route->action["controller"];
119
120
        if (!class_exists($controllerName)) {
121
            throw new \InvalidArgumentException("controller " . $controllerName . " not found");
122
        }
123
124
        $controller = new $controllerName($this->request, $this->response);
125
126
        $actionMethod = $route->action["method"];
127
128
        if (!method_exists($controller, $actionMethod)) {
129
            throw new \InvalidArgumentException("method " . $actionMethod . " not found");
130
        }
131
132
        $content = $controller->$actionMethod();
133
        $this->response->setContent($content);
134
    }
135
136
    /**
137
     * @param string $key
138
     * @return Route
139
     */
140
    public function getRoute($key = null)
141
    {
142
        if (is_null($key)) {
143
            return $this->routes;
144
        }
145
146
        if (!is_string($key)) {
147
            throw new \InvalidArgumentException("var key must be a string");
148
        }
149
150
        foreach ($this->routes as $route) {
151
            if ($route->key == $key) {
152
                return $route;
153
            }
154
        }
155
156
        throw new \OutOfBoundsException("not found route with key " . $key);
157
    }
158
159
    /**
160
     * @param string $path
161
     * @param string $method
162
     * @return Route
163
     */
164
    public function getRouteByPath($path, $method)
165
    {
166
167
        foreach ($this->routes as $route) {
168
            if ($route->path == $path && $route->method == $method) {
169
                return $route;
170
            }
171
        }
172
173
        return null;
174
    }
175
176
    /**
177
     * @param array $routes
178
     */
179
    public function setRoutes($routes)
180
    {
181
        $this->routes = $routes;
182
    }
183
}
184