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
Pull Request — master (#13)
by Jérémy
03:43 queued 01:52
created

Route::after()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
use CapMousse\ReactRestify\Routing\ControllerContainer;
9
use CapMousse\ReactRestify\Traits\EventTrait;
10
11
class Route extends EventEmitter
12
{
13
    use EventTrait;
14
    
15
    /**
16
     * Regexp ready route
17
     * @var String
18
     */
19
    public $parsedRoute;
20
21
    /**
22
     * Route method type
23
     * @var String
24
     */
25
    public $method;
26
27
    /**
28
     * Route action
29
     * @var Callable
30
     */
31
    public $action;
32
33
    /**
34
     * Route uri
35
     * @var String
36
     */
37
    private $uri;
38
39
    /**
40
     * Route filters
41
     * @var array
42
     */
43
    private $filters = [];
44
45
    /**
46
     * @param String   $method
47
     * @param String   $uri
48
     * @param Callable $action
49
     */
50
    public function __construct ($method, $uri, $action)
51
    {
52
        $this->method = $method;
53
        $this->uri = $uri;
54
        $this->action = $action;
55
    }
56
57
    /**
58
     * Create a new filter for current route
59
     *
60
     * @param String $param  parameter to filter
61
     * @param String $filter regexp to execute
62
     *
63
     * @return void
64
     */
65
    public function where($param, $filter)
66
    {
67
        if (is_array($param)) {
68
            $this->filters = array_merge($this->filters, $param);
69
70
            return;
71
        }
72
73
        $this->filters[$param] = $filter;
74
    }
75
76
    /**
77
     * Parse route uri
78
     *
79
     * @return void
80
     */
81
    public function parse()
82
    {
83
        preg_match_all("#\{(\w+)\}#", $this->uri, $params);
84
        $replace = [];
85
86
        foreach ($params[1] as $param) {
87
            $replace['{'.$param.'}'] = '(?<'.$param.'>'. (isset($this->filters[$param]) ? $this->filters[$param] : '[a-zA-Z+0-9-.]+') .')';
88
        }
89
90
        $this->parsedRoute = str_replace(array_keys($replace), array_values($replace), $this->uri);
91
    }
92
93
    /**
94
     * Check if uri is parsed
95
     *
96
     * @return boolean
97
     */
98
    public function isParsed()
99
    {
100
        return !empty($this->parsedRoute);
101
    }
102
103
    /**
104
     * Check if path match route uri
105
     * @param  String $path
106
     * @return bool
107
     */
108
    public function match($path)
109
    {
110
        if (!$this->isParsed()) $this->parse();
111
112
        return preg_match('#'.$this->parsedRoute.'$#', $path);
113
    }
114
115
    /**
116
     * Parse route arguments
117
     * @param  String $path 
118
     * @return array
119
     */
120
    public function getArgs($path)
121
    {
122
        if (!$this->isParsed()) $this->parse();
123
        
124
        $data = [];
125
        $args = [];
126
        preg_match('#'.$this->parsedRoute.'$#', $path, $data);
127
128
129
        foreach ($data as $name => $value) {
130
            if (is_int($name)) continue;
131
            $args[$name] = $value;
132
        }
133
134
        return $args;
135
    }
136
137
    /**
138
     * Run the current route
139
     *
140
     * @param \React\Http\Request     $request
141
     * @param \React\Restify\Response $response
142
     * @param Callable                $next
143
     *
144
     * @return Void
145
     */
146
    public function run(Request $request, Response $response, $next)
147
    {
148
        if (is_string($this->action)) {
149
            $this->action = ControllerContainer::resolveController($this->action);
150
        }
151
152
        try {
153
            call_user_func_array($this->action, array($request, $response, $next));
154
            $this->emit('after', [$request, $response, $this]);
155
        } catch (\Exception $e) {
156
            $this->emit('error', [$request, $response, $e->getMessage()]);
157
        }
158
    }
159
}
160