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.

Cookie   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 162
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 4 1
A get() 0 14 3
A put() 0 17 4
A forever() 0 4 1
A forget() 0 4 1
A hash() 0 4 1
A parse() 0 18 3
A send() 0 7 2
1
<?php
2
/**
3
 * Pimf
4
 *
5
 * @copyright Copyright (c)  Gjero Krsteski (http://krsteski.de)
6
 * @license   http://opensource.org/licenses/MIT MIT
7
 */
8
9
namespace Pimf;
10
11
/**
12
 * Using the cookie
13
 *
14
 * <code>
15
 *    // Get the value of the "favorite" cookie
16
 *    $favorite = Cookie::get('favorite');
17
 *
18
 *    // Get the value of a cookie or return a default value
19
 *    $favorite = Cookie::get('framework', 'Pimf');
20
 *
21
 *    // Set the value of the "favorite" cookie
22
 *    Cookie::put('favorite', 'Pimf');
23
 *
24
 *    // Set the value of the "favorite" cookie for twenty minutes
25
 *    Cookie::put('favorite', 'Pimf', 20);
26
 *
27
 *    // Set a cookie that should last one year
28
 *    Cookie::forever('favorite', 'Blue');
29
 *
30
 * </code>
31
 *
32
 * @package Pimf
33
 * @author  Gjero Krsteski <[email protected]>
34
 */
35
class Cookie
36
{
37
    /**
38
     * How long is forever (in minutes)?
39
     *
40
     * @var int
41
     */
42
    const FOREVER = 2628000;
43
44
    /**
45
     * The cookies that have been set.
46
     *
47
     * @var array
48
     */
49
    public static $jar = array();
50
51
    /**
52
     * Determine if a cookie exists.
53
     *
54
     * @param  string $name
55
     *
56
     * @return bool
57
     */
58
    public static function has($name)
59
    {
60
        return (static::get($name) !== null);
61
    }
62
63
    /**
64
     * Get the value of a cookie.
65
     *
66
     * @param      $name
67
     * @param null $default
68
     *
69
     * @return null|string
70
     */
71
    public static function get($name, $default = null)
72
    {
73
        if (isset(static::$jar[$name])) {
74
            return static::parse(static::$jar[$name]['value']);
75
        }
76
77
        $cookie = Request::$cookieData;
78
79
        if (!is_null($value = $cookie->get($name))) {
80
            return static::parse($value);
81
        }
82
83
        return $default;
84
    }
85
86
    /**
87
     * Set the value of a cookie.
88
     *
89
     * @param        $name
90
     * @param        $value
91
     * @param int    $expiration
92
     * @param string $path
93
     * @param null   $domain
94
     * @param bool   $secure
95
     *
96
     * @return bool
97
     * @throws \RuntimeException
98
     */
99
    public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
100
    {
101
        if ($expiration !== 0) {
102
            $expiration = time() + ($expiration * 60);
103
        }
104
105
        $value = static::hash($value) . '+' . $value;
106
107
        // are we attempting to send a secure cookie over the insecure HTTP.
108
        if ($secure === true && Config::get('ssl') === false) {
109
            throw new \RuntimeException("Attempting to set secure cookie over HTTP!");
110
        }
111
112
        static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
113
114
        return true;
115
    }
116
117
    /**
118
     * Set a "permanent" cookie. The cookie will last for one year.
119
     *
120
     * @param        $name
121
     * @param        $value
122
     * @param string $path
123
     * @param null   $domain
124
     * @param bool   $secure
125
     *
126
     * @return bool
127
     */
128
    public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
129
    {
130
        return static::put($name, $value, static::FOREVER, $path, $domain, $secure);
131
    }
132
133
    /**
134
     * Delete a cookie.
135
     *
136
     * @param        string $name
137
     * @param string        $path
138
     * @param null          $domain
139
     * @param bool          $secure
140
     *
141
     * @return bool
142
     */
143
    public static function forget($name, $path = '/', $domain = null, $secure = false)
144
    {
145
        return static::put($name, null, -2000, $path, $domain, $secure);
146
    }
147
148
    /**
149
     * Hash the given cookie value.
150
     *
151
     * @param string $value
152
     *
153
     * @return string
154
     */
155
    public static function hash($value)
156
    {
157
        return hash_hmac('sha1', $value, Config::get('app.key'));
158
    }
159
160
    /**
161
     * Parse a hash fingerprinted cookie value.
162
     *
163
     * @param string $value
164
     *
165
     * @return string
166
     */
167
    protected static function parse($value)
168
    {
169
        $segments = explode('+', $value);
170
171
        // check if the cookie is invalid.
172
        if (!(count($segments) >= 2)) {
173
            return null;
174
        }
175
176
        $value = implode('+', array_slice($segments, 1));
177
178
        // check the SHA-1 hash from the cookie.
179
        if ($segments[0] == static::hash($value)) {
180
            return $value;
181
        }
182
183
        return null;
184
    }
185
186
    /**
187
     * Send along with the rest of the HTTP headers.
188
     */
189
    public static function send()
190
    {
191
        foreach (static::$jar as $cookie) {
192
            setcookie($cookie['name'], $cookie['value'], $cookie['expiration'], $cookie['path'], $cookie['domain'],
193
                $cookie['secure'], true);
194
        }
195
    }
196
}
197