Passed
Push — master ( acc01d...5abdee )
by Wilder
01:36
created

Cookie   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
eloc 30
c 2
b 0
f 0
dl 0
loc 123
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 3 1
A set() 0 8 3
A has() 0 13 5
A encrypt() 0 3 1
A get() 0 12 4
A decrypt() 0 3 1
A expire() 0 3 1
A name() 0 3 1
A setCookie() 0 3 1
A all() 0 4 1
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
            $value = self::encrypt(http_build_query($value));
25
        } else {
26
            $value = ($encrypt ? self::encrypt($value) : $value);
27
        }
28
        return self::setCookie($name, $value, self::expire($minutes), $path);
29
    }
30
31
    /**
32
     * @param string $name
33
     * @param string|null $path
34
     * @return bool
35
     */
36
    public static function destroy(string $name, ?string $path = null): bool
37
    {
38
        return self::setCookie($name, null, -1, $path);
39
    }
40
41
    /**
42
     * @param string $name
43
     * @param string|null $value
44
     * @param bool $encrypt
45
     * @return bool
46
     */
47
    public static function has(string $name, ?string $value = null, bool $encrypt = true): bool
48
    {
49
        $cookie = self::name($name);
50
        if (!$value) {
51
            if ($cookie) {
52
                return true;
53
            }
54
        } else {
55
            if ($cookie == ($encrypt ? self::encrypt($value) : $value)) {
56
                return true;
57
            }
58
        }
59
        return false;
60
    }
61
62
    /**
63
     * @param string $name
64
     * @param bool $isArray
65
     * @param bool $decrypt
66
     * @return mixed|string|null
67
     */
68
    public static function get(string $name, bool $isArray = false, bool $decrypt = true)
69
    {
70
        $cookie = self::name($name);
71
        if ($cookie) {
72
            if (!$isArray) {
73
                return $decrypt ? self::decrypt($cookie) : $cookie;
74
            } else {
75
                parse_str(self::decrypt($cookie), $data);
76
                return $data;
77
            }
78
        }
79
        return null;
80
    }
81
82
    /**
83
     * @param string $name
84
     * @param string|null $value
85
     * @param int $expire
86
     * @param string|null $path
87
     * @return bool
88
     */
89
    private static function setCookie(string $name, ?string $value, int $expire, ?string $path = null): bool
90
    {
91
        return setcookie($name, $value, $expire, ($path ?? '/'));
92
    }
93
94
    /**
95
     * @param int $minutes
96
     * @return int
97
     */
98
    private static function expire(int $minutes): int
99
    {
100
        return time() + (60 * $minutes);
101
    }
102
103
    /**
104
     * @param string $value
105
     * @return string
106
     */
107
    private static function encrypt(string $value): string
108
    {
109
        return base64_encode($value);
110
    }
111
112
    /**
113
     * @param string $value
114
     * @return string
115
     */
116
    private static function decrypt(string $value): string
117
    {
118
        return base64_decode($value);
119
    }
120
121
    /**
122
     * @param string $name
123
     * @return mixed
124
     */
125
    private static function name(string $name)
126
    {
127
        return filter_input(INPUT_COOKIE, $name, FILTER_SANITIZE_STRIPPED);
128
    }
129
130
    public static function all()
131
    {
132
        echo '<pre>';
133
        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...
134
    }
135
}