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
Push — master ( ee497f...cca939 )
by François
02:31
created

LanguageSwitcherHook::executeBefore()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 4
nop 2
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Common\Http;
20
21
use SURFnet\VPN\Common\Http\Exception\HttpException;
22
23
/**
24
 * This hook is used to be able to switch the language without requiring to be
25
 * authenticated. As the language switcher is only a user preference stored in
26
 * a cookie this is not a problem. This way, even the authentication page can
27
 * use the language switcher.
28
 */
29
class LanguageSwitcherHook implements BeforeHookInterface
30
{
31
    /** @var bool */
32
    private $secureCookie;
33
34
    /** @var array */
35
    private $supportedLanguages;
36
37
    public function __construct(array $supportedLanguages, $secureCookie = true)
38
    {
39
        $this->supportedLanguages = $supportedLanguages;
40
        $this->secureCookie = (bool) $secureCookie;
41
    }
42
43
    public function executeBefore(Request $request, array $hookData)
44
    {
45
        if ('POST' !== $request->getRequestMethod()) {
46
            return false;
47
        }
48
49
        if ('/setLanguage' !== $request->getPathInfo()) {
50
            return false;
51
        }
52
53
        $language = $request->getPostParameter('setLanguage', false, 'en_US');
54
        if (!in_array($language, $this->supportedLanguages)) {
55
            throw new HttpException('invalid language', 400);
56
        }
57
58
        setcookie(
59
            'uiLanguage',
60
            $language,
61
            time() + 60 * 60 * 24 * 365,    // remember for 1 year
62
            $request->getRoot(),
63
            $request->getServerName(),
64
            $this->secureCookie,
65
            true
66
        );
67
68
        // XXX do we need to validate HTTP_REFERER here?
69
        return new RedirectResponse($request->getHeader('HTTP_REFERER'), 302);
70
    }
71
}
72