Cookie::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace System;
4
5
class Cookie
6
{
7
    /**
8
     * Application Object
9
     *
10
     * @var \System\Application
11
     */
12
    private $app;
13
14
    /**
15
     * Constructor
16
     *
17
     * @property object $request
18
     * @param \System\Application $app
19
     */
20
    public function __construct(Application $app)
21
    {
22
        $this->app = $app;
23
    }
24
25
    /**
26
     * Set new value to cookie
27
     *
28
     * @param string $key
29
     * @param mixed $value
30
     * @param int $hours
31
     * @return void
32
     */
33
    public function set(string $key, $value, int $hours = 1800, $path = '/')
34
    {
35
        $expireTime = $hours == -1 ? -1 : time() + $hours * 3600;
36
37
        setcookie($key, $value, $expireTime, $path, '', false, true);
38
    }
39
40
    /**
41
     * Get value from cookies by the given key
42
     *
43
     * @param string $key
44
     * @param mixed $default
45
     * @return mixed
46
     */
47
    public function get(string $key, $default = null)
48
    {
49
        return array_get($_COOKIE, $key, $default);
50
    }
51
52
    /**
53
     * Determine if the cookies has the given key
54
     *
55
     * @param string $key
56
     * @return bool
57
     */
58
    public function has(string $key)
59
    {
60
        return array_key_exists($key, $_COOKIE);
61
    }
62
63
    /**
64
     * Remove the given key from cookie
65
     *
66
     * @param string $key
67
     * @return void
68
     */
69
    public function remove(string $key)
70
    {
71
        $this->set($key, null, -1);
72
73
        unset($_COOKIE[$key]);
74
    }
75
76
    /**
77
     * Get all cookies data
78
     *
79
     * @return array
80
     */
81
    public function all()
82
    {
83
        return $_COOKIE;
84
    }
85
86
    /**
87
     * Destroy cookie
88
     *
89
     * @return void
90
     */
91
    public function destroy()
92
    {
93
        foreach (array_keys($this->all()) as $key) {
94
            $this->remove($key);
95
        }
96
        unset($_COOKIE);
97
    }
98
}
99