Passed
Push — master ( ba9a33...b16cde )
by Wilder
01:41
created

Cookie::setDoesntHave()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace ElePHPant\Cookie;
5
6
7
/**
8
 * Class Cookie
9
 * @package ElePHPant\Cookie
10
 */
11
class Cookie
12
{
13
    /**
14
     * @param string $name
15
     * @param mixed $value
16
     * @param int $minutes
17
     * @param string|null $path
18
     * @param bool $encrypt
19
     * @return bool
20
     */
21
    public static function set(string $name, $value, int $minutes, ?string $path = null, bool $encrypt = true): bool
22
    {
23
        if (is_array($value)) {
24
            $queryString = http_build_query($value);
25
            $value = $encrypt ? self::encrypt($queryString) : $queryString;
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 $path
35
     * @return bool
36
     */
37
    public static function destroy(string $name, ?string $path = null): bool
38
    {
39
        return self::setCookie($name, null, -1, $path);
40
    }
41
42
    /**
43
     * @param string $name
44
     * @param string|null $value
45
     * @param bool $encrypt
46
     * @return bool
47
     */
48
    public static function has(string $name, ?string $value = null, bool $encrypt = true): bool
49
    {
50
        $getCookie = self::getCookie($name);
51
        if (!$value) {
52
            if ($getCookie) {
53
                return true;
54
            }
55
        } else {
56
            if ($getCookie == ($encrypt ? self::encrypt($value) : $value)) {
57
                return true;
58
            }
59
        }
60
        return false;
61
    }
62
63
    /**
64
     * @param string $name
65
     * @param bool $decrypt
66
     * @return mixed|string|null
67
     */
68
    public static function get(string $name, bool $decrypt = true)
69
    {
70
        $getCookie = self::getCookie($name);
71
        if ($getCookie) {
72
            $cookie = $decrypt ? self::decrypt($getCookie) : $getCookie;
73
            parse_str($cookie, $isArray);
74
            $explode = explode('=', $cookie);
75
            if (!$isArray[$explode[0]]) {
76
                return $cookie;
77
            } else {
78
                parse_str($cookie, $data);
79
                return $data;
80
            }
81
        }
82
        return null;
83
    }
84
85
    /**
86
     * @param string $name
87
     * @param mixed $value
88
     * @param int $minutes
89
     * @param string|null $path
90
     * @param bool $destroy
91
     * @return bool|null
92
     */
93
    public static function setDoesntHave(string $name, $value, int $minutes, ?string $path = null, bool $destroy = true)
94
    {
95
        if (!self::has($name)) {
96
            return self::set($name, $value, $minutes, $path);
97
        }
98
        if ($destroy) {
99
            return self::destroy($name);
100
        }
101
        return null;
102
    }
103
104
    /**
105
     * @param string $name
106
     * @param string|null $value
107
     * @param int $expire
108
     * @param string|null $path
109
     * @return bool
110
     */
111
    private static function setCookie(string $name, ?string $value, int $expire, ?string $path = null): bool
112
    {
113
        return setcookie($name, $value, $expire, ($path ?? '/'));
114
    }
115
116
    /**
117
     * @param string $name
118
     * @return mixed
119
     */
120
    private static function getCookie(string $name)
121
    {
122
        return filter_input(INPUT_COOKIE, $name, FILTER_SANITIZE_STRIPPED);
123
    }
124
125
    /**
126
     * @param int $minutes
127
     * @return int
128
     */
129
    private static function expire(int $minutes): int
130
    {
131
        return time() + (60 * $minutes);
132
    }
133
134
    /**
135
     * @param string $value
136
     * @return string
137
     */
138
    private static function encrypt(string $value): string
139
    {
140
        return base64_encode($value);
141
    }
142
143
    /**
144
     * @param string $value
145
     * @return string
146
     */
147
    private static function decrypt(string $value): string
148
    {
149
        return base64_decode($value);
150
    }
151
152
    public static function all()
153
    {
154
        echo '<pre>';
155
        var_dump($_COOKIE);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($_COOKIE) looks like debug code. Are you sure you do not want to remove it?
Loading history...
156
    }
157
}