GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — dev ( 64e823...0fe7c8 )
by Jérémy
03:48 queued 01:56
created

Router   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
lcom 1
cbo 8
dl 0
loc 193
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A addRoutes() 0 12 2
A addRoute() 0 4 1
A addGroup() 0 6 1
A addMiddleware() 0 13 1
A launch() 0 22 3
B matchRoutes() 0 39 6
A runStack() 0 9 1
1
<?php
2
3
namespace CapMousse\ReactRestify\Routing;
4
5
use CapMousse\ReactRestify\Evenement\EventEmitter;
6
use CapMousse\ReactRestify\Http\Request;
7
use CapMousse\ReactRestify\Http\Response;
8
use CapMousse\ReactRestify\Traits\WaterfallTrait;
9
use CapMousse\ReactRestify\Container\Container;
10
11
class Router extends EventEmitter
12
{
13
    use WaterfallTrait;
14
    /**
15
     * The current routes list
16
     * @var array
17
     */
18
    public $routes = [];
19
20
    /**
21
     * @var array
22
     */
23
    private $middlewares = [];
24
25
    /**
26
     * The current asked uri
27
     * @var string|boolean
28
     */
29
    private $uri = false;
30
31
    /**
32
     * Create a new routing element
33
     *
34
     * @param array $routes a route array
35
     *
36
     * @throws \InvalidArgumentException
37
     */
38
    public function __construct($routes = [])
39
    {
40
        if (!is_array($routes)) {
41
            throw new \InvalidArgumentException("Routes must be an array");
42
        }
43
44
        $this->addRoutes($routes);
45
    }
46
47
    /**
48
     * Add routes
49
     *
50
     * @param array $routes a route array
51
     *
52
     * @throws \InvalidArgumentException
53
     * @return Void
54
     */
55
    public function addRoutes($routes)
56
    {
57
        if (!is_array($routes)) {
58
            throw new \InvalidArgumentException("Routes must be an array");
59
        }
60
61
        $routes = array_filter($routes, function ($route) {
62
            return is_a('Route', $route);
63
        });
64
65
        $this->routes = array_merge($this->routes, $routes);
66
    }
67
68
    /**
69
     * Add a new route
70
     *
71
     * @param String   $method   type of route
72
     * @param String   $route    uri to catch
73
     * @param Callable $callback
74
     */
75
    public function addRoute($method, $route, $callback)
76
    {
77
        return $this->routes[] = new Route(strtoupper($method), $route, $callback);
78
    }
79
80
    /**
81
     * Create a new group of routes
82
     *
83
     * @param String $prefix prefix of thes routes
84
     *
85
     * @return \CapMousse\ReactRestify\Routing\Group
86
     */
87
    public function addGroup($prefix, $callback)
88
    {
89
        $group = new Routes($this, $prefix, $callback);
90
91
        return $group;
92
    }
93
94
    /**
95
     * Add a middleware
96
     * @param string|Callable $callback
97
     */
98
    public function addMiddleware($callback)
99
    {
100
        $this->middlewares[] = function (Callable $next, Request $request, Response $response) use ($callback) {
101
            $container = Container::getInstance();
102
            $parameters = array_merge([
103
                "request"   => $request,
104
                "response"  => $response,
105
                "next"      => $next
106
            ], $request->getData());
107
108
            $container->call($callback, $parameters);
109
        };
110
    }
111
112
    /**
113
     * Launch the route parsing
114
     *
115
     * @param \React\Http\Request     $request
116
     * @param \React\Restify\Response $response
117
     *
118
     * @throws \RuntimeException
119
     * @return Void
120
     */
121
    public function launch(Request $request, Response $response)
122
    {
123
        if (count($this->routes) === 0) {
124
            throw new \RuntimeException("No routes defined");
125
        }
126
127
        $this->uri = $request->httpRequest->getPath();
128
129
        if ($this->uri = null) {
130
            $this->uri = "/";
131
        }
132
133
        $request->on('end', function () use (&$request, &$response) {
134
            $this->matchRoutes($request, $response);  
135
        });
136
137
        $request->on('error', function ($error) use (&$request, &$response) {
138
            $this->emit('error', [$request, $response, $error]);
139
        });
140
141
        $request->parseData();
142
    }
143
144
    /**
145
     * Try to match the current uri with all routes
146
     *
147
     *
148
     * @param \React\Http\Request     $request
149
     * @param \React\Restify\Response $response
150
     *
151
     * @throws \RuntimeException
152
     * @return Void
153
     */
154
    private function matchRoutes(Request $request, Response $response)
155
    {
156
        $stack = [];
157
        $badMethod = false;
158
159
        foreach ($this->routes as $route) {
160
            if (!$route->match($request->httpRequest->getPath())) {
161
                continue;
162
            }
163
164
            if ($route->method != strtoupper($request->httpRequest->getMethod())) {
165
                $badMethod = true;
166
                continue;
167
            }
168
169
            $methodArgs = $route->getArgs($request->httpRequest->getPath());
170
            $request->setData($methodArgs);
171
172
            $route->on('error', function () {
173
                $this->emit('error', func_get_args());
174
            });
175
176
            $stack[] = function (...$params) use ($route) {
177
                $route->run(...$params);
178
            };
179
        }
180
181
        if (count($stack)) {
182
            $this->runStack($stack, $request, $response);
183
            return;
184
        }
185
186
        if ($badMethod) {
187
            $this->emit('MethodNotAllowed', array(&$request, &$response));
188
            return;
189
        }
190
191
        $this->emit('NotFound', array($request, $response));
192
    }
193
194
    protected function runStack(array $stack, Request $request, Response $response)
195
    {
196
        $stack[] = function () use ($response) {
197
            $response->end();
198
        };
199
200
        $finalStack = array_merge($this->middlewares, $stack);
201
        $this->waterfall($finalStack, [$request, $response]);
202
    }
203
}
204