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.

Service   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 5
dl 0
loc 139
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A addBeforeHook() 0 4 1
A addAfterHook() 0 4 1
A addRoute() 0 4 1
A get() 0 4 1
A post() 0 4 1
A addModule() 0 4 1
C run() 0 74 10
A runAfterHooks() 0 8 2
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Common\Http;
11
12
use SURFnet\VPN\Common\Http\Exception\HttpException;
13
use SURFnet\VPN\Common\Http\Exception\InputValidationException;
14
use SURFnet\VPN\Common\TplInterface;
15
16
class Service
17
{
18
    /** @var \SURFnet\VPN\Common\TplInterface|null */
19
    private $tpl;
20
21
    /** @var array */
22
    private $routes;
23
24
    /** @var array */
25
    private $beforeHooks;
26
27
    /** @var array */
28
    private $afterHooks;
29
30
    public function __construct(TplInterface $tpl = null)
31
    {
32
        $this->tpl = $tpl;
33
        $this->routes = [
34
            'GET' => [],
35
            'POST' => [],
36
        ];
37
        $this->beforeHooks = [];
38
        $this->afterHooks = [];
39
    }
40
41
    public function addBeforeHook($name, BeforeHookInterface $beforeHook)
42
    {
43
        $this->beforeHooks[$name] = $beforeHook;
44
    }
45
46
    public function addAfterHook($name, AfterHookInterface $afterHook)
47
    {
48
        $this->afterHooks[$name] = $afterHook;
49
    }
50
51
    public function addRoute($requestMethod, $pathInfo, callable $callback)
52
    {
53
        $this->routes[$requestMethod][$pathInfo] = $callback;
54
    }
55
56
    public function get($pathInfo, callable $callback)
57
    {
58
        $this->addRoute('GET', $pathInfo, $callback);
59
    }
60
61
    public function post($pathInfo, callable $callback)
62
    {
63
        $this->addRoute('POST', $pathInfo, $callback);
64
    }
65
66
    public function addModule(ServiceModuleInterface $module)
67
    {
68
        $module->init($this);
69
    }
70
71
    public function run(Request $request)
72
    {
73
        try {
74
            // before hooks
75
            $hookData = [];
76
            foreach ($this->beforeHooks as $k => $v) {
77
                $hookResponse = $v->executeBefore($request, $hookData);
78
                // if we get back a Response object, return it immediately
79
                if ($hookResponse instanceof Response) {
80
                    // run afterHooks
81
                    return $this->runAfterHooks($request, $hookResponse);
82
                }
83
84
                $hookData[$k] = $hookResponse;
85
            }
86
87
            $requestMethod = $request->getRequestMethod();
88
            $pathInfo = $request->getPathInfo();
89
90
            if (!array_key_exists($requestMethod, $this->routes)) {
91
                throw new HttpException(
92
                    sprintf('method "%s" not allowed', $requestMethod),
93
                    405,
94
                    ['Allow' => implode(',', array_keys($this->routes))]
95
                );
96
            }
97
            if (!array_key_exists($pathInfo, $this->routes[$requestMethod])) {
98
                throw new HttpException(
99
                    sprintf('"%s" not found', $pathInfo),
100
                    404
101
                );
102
            }
103
104
            $response = $this->routes[$requestMethod][$pathInfo]($request, $hookData);
105
106
            // after hooks
107
            return $this->runAfterHooks($request, $response);
108
        } catch (InputValidationException $e) {
109
            // in case an InputValidationException is encountered, convert it
110
            // into a Bad Request HTTP response
111
            throw new HttpException($e->getMessage(), 400);
112
        } catch (HttpException $e) {
113
            if ($request->isBrowser()) {
114
                if (is_null($this->tpl)) {
115
                    // template not available
116
                    $response = new Response($e->getCode(), 'text/plain');
117
                    $response->setBody(sprintf('%d: %s', $e->getCode(), $e->getMessage()));
118
                } else {
119
                    // template available
120
                    $response = new Response($e->getCode(), 'text/html');
121
                    $response->setBody(
122
                        $this->tpl->render(
123
                            'errorPage',
124
                            [
125
                                'code' => $e->getCode(),
126
                                'message' => $e->getMessage(),
127
                            ]
128
                        )
129
                    );
130
                }
131
            } else {
132
                // not a browser
133
                $response = new Response($e->getCode(), 'application/json');
134
                $response->setBody(json_encode(['error' => $e->getMessage()]));
135
            }
136
137
            foreach ($e->getResponseHeaders() as $key => $value) {
138
                $response->addHeader($key, $value);
139
            }
140
141
            // after hooks
142
            return $this->runAfterHooks($request, $response);
143
        }
144
    }
145
146
    private function runAfterHooks(Request $request, Response $response)
147
    {
148
        foreach ($this->afterHooks as $v) {
149
            $response = $v->executeAfter($request, $response);
150
        }
151
152
        return $response;
153
    }
154
}
155