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::run()   C
last analyzed

Complexity

Conditions 10
Paths 136

Size

Total Lines 74
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
c 0
b 0
f 0
rs 5.4676
cc 10
eloc 41
nc 136
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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