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.

Request::isBrowser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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
14
class Request
15
{
16
    /** @var array */
17
    private $serverData;
18
19
    /** @var array */
20
    private $getData;
21
22
    /** @var array */
23
    private $postData;
24
25
    public function __construct(array $serverData, array $getData = [], array $postData = [])
26
    {
27
        $requiredHeaders = [
28
            'REQUEST_METHOD',
29
            'SERVER_NAME',
30
            'SERVER_PORT',
31
            'REQUEST_URI',
32
            'SCRIPT_NAME',
33
        ];
34
35
        foreach ($requiredHeaders as $key) {
36
            if (!array_key_exists($key, $serverData)) {
37
                // this indicates something wrong with the interaction between
38
                // the web server and PHP, these headers MUST always be available
39
                throw new HttpException(sprintf('missing header "%s"', $key), 500);
40
            }
41
        }
42
        $this->serverData = $serverData;
43
        $this->getData = $getData;
44
        $this->postData = $postData;
45
    }
46
47
    public function getAuthority()
48
    {
49
        // scheme
50
        if (!array_key_exists('REQUEST_SCHEME', $this->serverData)) {
51
            $requestScheme = 'http';
52
        } else {
53
            $requestScheme = $this->serverData['REQUEST_SCHEME'];
54
        }
55
56
        // server_name
57
        $serverName = $this->serverData['SERVER_NAME'];
58
59
        // port
60
        $serverPort = (int) $this->serverData['SERVER_PORT'];
61
62
        $usePort = false;
63
        if ('https' === $requestScheme && 443 !== $serverPort) {
64
            $usePort = true;
65
        }
66
        if ('http' === $requestScheme && 80 !== $serverPort) {
67
            $usePort = true;
68
        }
69
70
        if ($usePort) {
71
            return sprintf('%s://%s:%d', $requestScheme, $serverName, $serverPort);
72
        }
73
74
        return sprintf('%s://%s', $requestScheme, $serverName);
75
    }
76
77
    public function getUri()
78
    {
79
        $requestUri = $this->serverData['REQUEST_URI'];
80
81
        return sprintf('%s%s', $this->getAuthority(), $requestUri);
82
    }
83
84
    public function getRoot()
85
    {
86
        $rootDir = dirname($this->serverData['SCRIPT_NAME']);
87
        if ('/' !== $rootDir) {
88
            return sprintf('%s/', $rootDir);
89
        }
90
91
        return $rootDir;
92
    }
93
94
    public function getRootUri()
95
    {
96
        return sprintf('%s%s', $this->getAuthority(), $this->getRoot());
97
    }
98
99
    public function getRequestMethod()
100
    {
101
        return $this->serverData['REQUEST_METHOD'];
102
    }
103
104
    public function getServerName()
105
    {
106
        return $this->serverData['SERVER_NAME'];
107
    }
108
109
    public function isBrowser()
110
    {
111
        if (!array_key_exists('HTTP_ACCEPT', $this->serverData)) {
112
            return false;
113
        }
114
115
        return false !== mb_strpos($this->serverData['HTTP_ACCEPT'], 'text/html');
116
    }
117
118
    public function getPathInfo()
119
    {
120
        // remove the query string
121
        $requestUri = $this->serverData['REQUEST_URI'];
122
        if (false !== $pos = mb_strpos($requestUri, '?')) {
123
            $requestUri = mb_substr($requestUri, 0, $pos);
124
        }
125
126
        // remove script_name (if it is part of request_uri
127
        if (0 === mb_strpos($requestUri, $this->serverData['SCRIPT_NAME'])) {
128
            return substr($requestUri, mb_strlen($this->serverData['SCRIPT_NAME']));
129
        }
130
131
        // remove the root
132
        if ('/' !== $this->getRoot()) {
133
            return mb_substr($requestUri, mb_strlen($this->getRoot()) - 1);
134
        }
135
136
        return $requestUri;
137
    }
138
139
    public function getQueryParameters()
140
    {
141
        return $this->getData;
142
    }
143
144
    public function getQueryParameter($key, $isRequired = true, $defaultValue = null)
145
    {
146
        return Utils::getValueFromArray($this->getData, $key, $isRequired, $defaultValue);
147
    }
148
149
    public function getPostParameters()
150
    {
151
        return $this->postData;
152
    }
153
154
    public function getPostParameter($key, $isRequired = true, $defaultValue = null)
155
    {
156
        return Utils::getValueFromArray($this->postData, $key, $isRequired, $defaultValue);
157
    }
158
159
    public function getHeader($key, $isRequired = true, $defaultValue = null)
160
    {
161
        return Utils::getValueFromArray($this->serverData, $key, $isRequired, $defaultValue);
162
    }
163
}
164