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.

LanguageSwitcherHook::executeBefore()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 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 fkooman\SeCookie\Cookie;
13
use SURFnet\VPN\Common\Http\Exception\HttpException;
14
15
/**
16
 * This hook is used to be able to switch the language without requiring to be
17
 * authenticated. As the language switcher is only a user preference stored in
18
 * a cookie this is not a problem. This way, even the authentication page can
19
 * use the language switcher.
20
 */
21
class LanguageSwitcherHook implements BeforeHookInterface
22
{
23
    /** @var \fkooman\SeCookie\Cookie */
24
    private $cookie;
25
26
    /** @var array */
27
    private $supportedLanguages;
28
29
    public function __construct(array $supportedLanguages, Cookie $cookie)
30
    {
31
        $this->supportedLanguages = $supportedLanguages;
32
        $this->cookie = $cookie;
33
    }
34
35
    public function executeBefore(Request $request, array $hookData)
36
    {
37
        if ('POST' !== $request->getRequestMethod()) {
38
            return false;
39
        }
40
41
        if ('/setLanguage' !== $request->getPathInfo()) {
42
            return false;
43
        }
44
45
        $language = $request->getPostParameter('setLanguage', false, 'en_US');
46
        if (!in_array($language, $this->supportedLanguages)) {
47
            throw new HttpException('invalid language', 400);
48
        }
49
50
        $this->cookie->set('uiLanguage', $language);
51
52
        return new RedirectResponse($request->getHeader('HTTP_REFERER'), 302);
53
    }
54
}
55