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
04:00 queued 02:10
created

Server   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 9
Bugs 1 Features 0
Metric Value
wmc 12
c 9
b 1
f 0
lcom 1
cbo 4
dl 0
loc 159
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 4 1
A __construct() 0 14 3
A __invoke() 0 14 2
A group() 0 4 1
A setAccessControlAllowOrigin() 0 4 1
B initEvents() 0 24 1
A on() 0 5 1
A listen() 0 7 1
A use() 0 4 1
1
<?php
2
3
namespace CapMousse\ReactRestify;
4
5
use React\Http\Request as HttpRequest;
6
use React\Http\Response as HttpResponse;
7
use CapMousse\ReactRestify\Container\Container;
8
9
class Server
10
{
11
    /**
12
     * Name of the server
13
     * @var string
14
     */
15
    public $name = "ReactRestify";
16
17
    /**
18
     * Version of the API
19
     * @var null
20
     */
21
    public $version = null;
22
23
    /**
24
     * @var Routing\Router
25
     */
26
    private $router;
27
28
    /**
29
     * @var string
30
     */
31
    private $allowOrigin = "*";
32
33
    /**
34
     * @param null $name
35
     * @param null $version
36
     */
37
    public function __construct($name = null, $version = null)
38
    {
39
        if (null !== $name) {
40
            $this->name = $name;
41
        }
42
43
        if (null !== $version) {
44
            $this->version = $version;
45
        }
46
47
        $this->router = new Routing\Router();
48
49
        $this->initEvents();
50
    }
51
52
    /**
53
     * Parse request from user
54
     *
55
     * @param \React\Http\Request  $httpRequest
56
     * @param \React\Http\Response $httpResponse
57
     */
58
    public function __invoke(HttpRequest $httpRequest, HttpResponse $httpResponse)
59
    {
60
        $request = new Http\Request($httpRequest);
61
        $response = new Http\Response($httpResponse, $this->name, $this->version);
62
63
        try {
64
            $this->router->launch($request, $response);
65
        } catch (\Exception $e) {
66
            $response
67
                ->setStatus(500)
68
                ->write($e->getMessage())
69
                ->end();
70
        }
71
    }
72
73
    /**
74
     * Create a new group of route
75
     * @param String   $prefix   prefix of the routes
76
     * @param Callable $callback
77
     *
78
     * @return \CapMousse\ReactRestify\Routing\Routes
79
     */
80
    public function group($prefix, $callback)
81
    {
82
        return $this->router->addGroup($prefix, $callback);
83
    }
84
85
    /**
86
     * The the Access-Control-Allow-Origin header
87
     *
88
     * @param string $origin
89
     */
90
    public function setAccessControlAllowOrigin($origin)
91
    {
92
        $this->allowOrigin = $origin;
93
    }
94
95
    /**
96
     * Init default event catch
97
     *
98
     * @return void
99
     */
100
    private function initEvents()
101
    {
102
        $this->router->on('NotFound', function($request, $response) {
103
            $response
104
                ->setStatus(404)
105
                ->write('Not found')
106
                ->end();
107
        });
108
109
        $this->router->on('MethodNotAllowed', function($request, $response) {
110
            $response
111
                ->setStatus(405)
112
                ->write('Method Not Allowed')
113
                ->end();
114
        });
115
116
        $this->router->on('error', function ($request, $response, $error) {
117
            $response
118
                ->reset()
119
                ->setStatus(500)
120
                ->write($error)
121
                ->end();
122
        });
123
    }
124
125
    /**
126
     * Manual router event manager
127
     * @param String          $event
128
     * @param Callable|string $callback
129
     */
130
    public function on($event, $callback)
131
    {
132
        $this->router->removeAllListeners($event);
133
        $this->router->on($event, $callback);
134
    }
135
136
    /**
137
     * Create runner instance
138
     * @param  Int    $port
139
     * @param  String $host
140
     * @return Server
141
     */
142
    public function listen($port, $host = "127.0.0.1")
143
    {
144
        $runner = new Runner($this);
145
        $runner->listen($port, $host);
146
147
        return $this;
148
    }
149
150
    /**
151
     * Server middleware
152
     * @param  Callable|string $callback
153
     */
154
    public function use($callback)
155
    {
156
        return $this->router->addMiddleware($callback);
157
    }
158
159
    /**
160
     * @param string $name      method to call
161
     * @param array  $arguments
162
     */
163
    public function __call($name, $arguments)
164
    {
165
        return $this->router->addRoute($name, $arguments[0], $arguments[1]);
166
    }
167
}
168