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

Server::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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