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
Branch master (a445f1)
by Jérémy
03:02
created

Router::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 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
9
class Router extends EventEmitter
10
{
11
    /**
12
     * The current routes list
13
     * @var array
14
     */
15
    public $routes = array();
16
17
    /**
18
     * The current asked uri
19
     * @var string|boolean
20
     */
21
    private $uri = false;
22
23
    /**
24
     * Create a new routing element
25
     *
26
     * @param array $routes a route array
27
     *
28
     * @throws \InvalidArgumentException
29
     * @return Router
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31
    public function __construct($routes = array())
32
    {
33
        if (!is_array($routes)) {
34
            throw new \InvalidArgumentException("Routes must be an array");
35
        }
36
37
        $this->addRoutes($routes);
38
    }
39
40
    /**
41
     * Add routes
42
     *
43
     * @param array $routes a route array
44
     *
45
     * @throws \InvalidArgumentException
46
     * @return Void
47
     */
48
    public function addRoutes($routes)
49
    {
50
        if (!is_array($routes)) {
51
            throw new \InvalidArgumentException("Routes must be an array");
52
        }
53
54
        $routes = array_filter($routes, function ($route) {
55
            return is_a('Route', $route);
56
        });
57
58
        $this->routes = array_merge($this->routes, $routes);
59
    }
60
61
    /**
62
     * Add a new route
63
     *
64
     * @param String   $method   type of route
65
     * @param String   $route    uri to catch
66
     * @param Callable $callback
67
     */
68
    public function addRoute($method, $route, $callback)
69
    {
70
        return $this->routes[] = new Route(strtoupper($method), $route, $callback);
71
    }
72
73
    /**
74
     * Create a new group of routes
75
     *
76
     * @param String $prefix prefix of thes routes
77
     *
78
     * @return \CapMousse\ReactRestify\Routing\Group
79
     */
80
    public function addGroup($prefix, $callback)
81
    {
82
        $group = new Routes($this, $prefix, $callback);
83
84
        return $group;
85
    }
86
87
    /**
88
     * Launch the route parsing
89
     *
90
     * @param \React\Http\Request     $request
91
     * @param \React\Restify\Response $response
92
     *
93
     * @throws \RuntimeException
94
     * @return Void
95
     */
96
    public function launch(Request $request, Response $response, $next)
97
    {
98
        if (count($this->routes) === 0) {
99
            throw new \RuntimeException("No routes defined");
100
        }
101
102
        $this->uri = $request->httpRequest->getPath();
103
104
        if ($this->uri = null) {
105
            $this->uri = "/";
106
        }
107
108
        $this->matchRoutes($request, $response, $next);
109
    }
110
111
    /**
112
     * Try to match the current uri with all routes
113
     *
114
     *
115
     * @param \React\Http\Request     $request
116
     * @param \React\Restify\Response $response
117
     *
118
     * @throws \RuntimeException
119
     * @return Void
120
     */
121
    private function matchRoutes(Request $request, Response $response, $next)
122
    {
123
        $badMethod = false;
124
125
        foreach ($this->routes as $route) {
126
            if (!$route->isParsed()) {
127
                $route->parse();
128
            }
129
130
            if (preg_match('#'.$route->parsed.'$#', $request->httpRequest->getPath(), $array)) {
131
                if ($route->method != strtoupper($request->httpRequest->getMethod())) {
132
                    $badMethod = true;
133
                    continue;
134
                }
135
136
                $methodArgs = array();
137
138
                foreach ($array as $name => $value) {
139
                    if (!is_int($name)) {
140
                      $methodArgs[$name] = $value;
141
                    }
142
                }
143
144
                if (count($methodArgs) > 0) {
145
                    $request->setData($methodArgs);
146
                }
147
148
                $route->on('error', function () {
149
                    $this->emit('error', func_get_args());
150
                });
151
                $route->run($request, $response, $next);
152
153
                return;
154
            }
155
        }
156
157
        if ($badMethod) {
158
            $this->emit('MethodNotAllowed', array($request, $response, $next));
159
160
            return;
161
        }
162
163
        $this->emit('NotFound', array($request, $response, $next));
164
    }
165
}
166