Passed
Push — master ( 3009ce...13c9b3 )
by Rafael Damasceno
01:24
created

StaticCookie::get()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 8
c 1
b 1
f 0
nc 7
nop 2
dl 0
loc 13
rs 9.6111
1
<?php
2
3
namespace RafaelDms\Cookie;
4
5
class StaticCookie
6
{
7
    /**
8
     * @param string $name
9
     * @param mixed $value
10
     * @param int $minutes
11
     * @param string|null $path
12
     * @param bool $encrypt
13
     * @return bool
14
     */
15
    public static function set(
16
        string $name,
17
        mixed $value,
18
        int $minutes,
19
        ?string $path = null,
20
        bool $encrypt = true
21
    ): bool {
22
        //check if the cookie value is an array to save in json
23
        if (is_array($value)) {
24
            $cookie = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
25
            $value = $encrypt ? self::encrypt($cookie) : $cookie;
26
        } else {
27
            $value = $encrypt ? self::encrypt($value) : $value;
28
        }
29
        return self::setCookie($name, $value, self::expire($minutes), $path);
30
    }
31
32
    /**
33
     * @param string $name
34
     * @param string|null $value
35
     * @param string|null $path
36
     * @return bool
37
     */
38
    public static function destroy(string $name, ?string $value = '', ?string $path = null): bool
39
    {
40
        return self::setCookie($name, $value, -1, $path);
41
    }
42
43
    /**
44
     * @param string $name
45
     * @param string|null $value
46
     * @param string|null $path
47
     * @return bool
48
     */
49
    public static function destroyAndUnset(string $name, ?string $value = '', ?string $path = null): bool
50
    {
51
        self::unset($name);
52
        return self::setCookie($name, $value, -1, $path);
53
    }
54
55
    /**
56
     * @param string $name
57
     * @return void
58
     */
59
    public static function unset(string $name): void
60
    {
61
        unset($_COOKIE[$name]);
62
    }
63
64
    /**
65
     * @param string $name
66
     * @param string|null $value
67
     * @param bool $encrypt
68
     * @return bool
69
     */
70
    public static function has(string $name, ?string $value = null, bool $encrypt = true): bool
71
    {
72
        $getCookie = self::getCookie($name);
73
        if (!$value) {
74
            if ($getCookie) {
75
                return true;
76
            }
77
            return false;
78
        } else {
79
            if ($getCookie == ($encrypt ? self::encrypt($value) : $value)) {
80
                return true;
81
            }
82
        }
83
        return false;
84
    }
85
86
    /**
87
     * @param string $name
88
     * @param bool $decrypt
89
     * @return mixed|string|null
90
     */
91
    public static function get(string $name, bool $decrypt = true): mixed
92
    {
93
        if (self::has($name)) {
94
            $cookie = ($decrypt ? self::decrypt(self::getCookie($name)) : self::getCookie($name));
95
            if ($cookie) {
96
                if ($decode = json_decode($cookie, true)) {
97
                    return $decode;
98
                }
99
                return $cookie;
100
            }
101
            return null;
102
        }
103
        return null;
104
    }
105
106
    /**
107
     * @param string $name
108
     * @param mixed $value
109
     * @param int $minutes
110
     * @param string|null $path
111
     * @param bool $removeHas
112
     * @return bool|null
113
     */
114
    public static function setDoesntHave(
115
        string $name,
116
        mixed $value,
117
        int $minutes,
118
        ?string $path = null,
119
        bool $removeHas = false
120
    ): ?bool {
121
        if (!self::has($name)) {
122
            return self::set($name, $value, $minutes, $path);
123
        }
124
        if ($removeHas) {
125
            return self::destroy($name);
126
        }
127
        return null;
128
    }
129
130
    /**
131
     * @param string $name
132
     * @param string|null $value
133
     * @param int $expire
134
     * @param string|null $path
135
     * @param string|null $domain
136
     * @param bool $secure
137
     * @return bool
138
     */
139
    public static function setCookie(
140
        string $name,
141
        ?string $value,
142
        int $expire,
143
        ?string $path,
144
        ?string $domain = "",
145
        bool $secure = false
146
    ): bool {
147
        return setCookie($name, $value, $expire, ($path ?? "/"), ($domain ?? ""), $secure);
148
    }
149
150
    /**
151
     * @param string $name
152
     * @return mixed
153
     */
154
    public static function getCookie(string $name): mixed
155
    {
156
        return filter_input(INPUT_COOKIE, $name, FILTER_DEFAULT);
157
    }
158
159
    /**
160
     * @param int $minutes
161
     * @return int
162
     */
163
    public static function expire(int $minutes): int
164
    {
165
        return time() + (60 * $minutes);
166
    }
167
168
    /**
169
     * @param string $value
170
     * @return string
171
     */
172
    public static function encrypt(string $value): string
173
    {
174
        return base64_encode($value);
175
    }
176
177
    /**
178
     * @param string $value
179
     * @return string
180
     */
181
    public static function decrypt(string $value): string
182
    {
183
        return base64_decode($value);
184
    }
185
186
}