Passed
Branch master (2016e6)
by Wilder
01:40
created

Cookie::getCookie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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
            $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 string|null $value
88
     * @param int $expire
89
     * @param string|null $path
90
     * @return bool
91
     */
92
    private static function setCookie(string $name, ?string $value, int $expire, ?string $path = null): bool
93
    {
94
        return setcookie($name, $value, $expire, ($path ?? '/'));
95
    }
96
97
    /**
98
     * @param string $name
99
     * @return mixed
100
     */
101
    private static function getCookie(string $name)
102
    {
103
        return filter_input(INPUT_COOKIE, $name, FILTER_SANITIZE_STRIPPED);
104
    }
105
106
    /**
107
     * @param int $minutes
108
     * @return int
109
     */
110
    private static function expire(int $minutes): int
111
    {
112
        return time() + (60 * $minutes);
113
    }
114
115
    /**
116
     * @param string $value
117
     * @return string
118
     */
119
    private static function encrypt(string $value): string
120
    {
121
        return base64_encode($value);
122
    }
123
124
    /**
125
     * @param string $value
126
     * @return string
127
     */
128
    private static function decrypt(string $value): string
129
    {
130
        return base64_decode($value);
131
    }
132
133
    public static function all()
134
    {
135
        echo '<pre>';
136
        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...
137
    }
138
}