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 ( 3c229d...169a4f )
by François
109:09 queued 100:54
created

Cookie::replace()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 10
nc 8
nop 2
1
<?php
2
/**
3
 *  Copyright (C) 2017 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
class Cookie
22
{
23
    /** @var array */
24
    private $cookieOptions;
25
26
    /**
27
     * @param array $cookieOptions
28
     */
29
    public function __construct(array $cookieOptions = [])
30
    {
31
        $this->cookieOptions = array_merge(
32
            [
33
                // defaults
34
                'Secure' => true,       // bool
35
                'HttpOnly' => true,     // bool
36
                'Path' => '/',          // string
37
                'Domain' => null,       // string
38
                'Max-Age' => null,      // int > 0
39
                'SameSite' => 'Strict', // "Strict|Lax"
40
            ],
41
            $cookieOptions
42
        );
43
    }
44
45
    public function delete($name)
46
    {
47
        self::set($name, '');
48
    }
49
50
    public function set($name, $value)
51
    {
52
        $attributeValueList = [];
53
        if ($this->cookieOptions['Secure']) {
54
            $attributeValueList[] = 'Secure';
55
        }
56
        if ($this->cookieOptions['HttpOnly']) {
57
            $attributeValueList[] = 'HttpOnly';
58
        }
59
        $attributeValueList[] = sprintf('Path=%s', $this->cookieOptions['Path']);
60
        if (!is_null($this->cookieOptions['Domain'])) {
61
            $attributeValueList[] = sprintf('Domain=%s', $this->cookieOptions['Domain']);
62
        }
63
64
        if (!is_null($this->cookieOptions['Max-Age'])) {
65
            $attributeValueList[] = sprintf('Max-Age=%d', $this->cookieOptions['Max-Age']);
66
        }
67
        $attributeValueList[] = sprintf('SameSite=%s', $this->cookieOptions['SameSite']);
68
69
        header(
70
            sprintf(
71
                'Set-Cookie: %s=%s; %s',
72
                $name,
73
                $value,
74
                implode('; ', $attributeValueList)
75
            ),
76
            false
77
        );
78
    }
79
80
    /**
81
     * Replace an existing HTTP cookie.
82
     *
83
     * @param string $name  the cookie name
84
     * @param string $value the cookie value
85
     */
86
    protected function replace($name, $value)
87
    {
88
        $cookieList = [];
89
        foreach (headers_list() as $hdr) {
90
            if (0 === stripos($hdr, 'Set-Cookie: ')) {
91
                // found "Set-Cookie"
92
                if (0 !== stripos($hdr, sprintf('Set-Cookie: %s=%s', $name, $value))) {
93
                    // not the one we want to replace, add to backup list
94
                    $cookieList[] = $hdr;
95
                }
96
            }
97
        }
98
        // remove all "Set-Cookie" headers, `header_remove()` is case
99
        // insensitive
100
        header_remove('Set-Cookie');
101
102
        // restore cookies we want to keep
103
        foreach ($cookieList as $cookie) {
104
            header($cookie, false);
105
        }
106
107
        self::set($name, $value);
108
    }
109
}
110