Session::destroy()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 7.9062

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 6
cts 16
cp 0.375
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 7.9062
1
<?php
2
3
namespace Anax\Session;
4
5
/**
6
 * Class for wrapping sessions.
7
 */
8
class Session implements SessionInterface
9
{
10
    /**
11
     * Set a session name.
12
     *
13
     * @param string $name to set as session name.
14
     *
15
     * @return self
16
     */
17 2
    public function name($name)
18
    {
19 2
        if (php_sapi_name() !== 'cli') {
20
            session_name($name);
21
        }
22 2
        return $this;
23
    }
24
25
26
27
    /**
28
     * Start the session (ignore when on cli).
29
     *
30
     * @return self
31
     */
32 1
    public function start()
33
    {
34 1
        if (php_sapi_name() !== 'cli') {
35
            session_start();
36
        }
37 1
        return $this;
38
    }
39
40
41
42
    /**
43
     * Check if a value is set in the session.
44
     *
45
     * @param string $key   in session variable.
46
     *
47
     * @return boolean true if $key is set, else false.
48
     *
49
     * @SuppressWarnings(PHPMD.Superglobals)
50
     */
51 3
    public function has($key)
52
    {
53 3
        return isset($_SESSION) && isset($_SESSION[$key]);
54
    }
55
56
57
58
    /**
59
     * Get a value from the session.
60
     *
61
     * @param string $key     in session variable.
62
     * @param mixed  $default default value to return when key is not set
63
     *                        in the session.
64
     *
65
     * @return mixed value from session and null if not set.
66
     *
67
     * @SuppressWarnings(PHPMD.Superglobals)
68
     */
69 3
    public function get($key, $default = null)
70
    {
71 3
        return $this->has($key)
72 3
            ? $_SESSION[$key]
73 3
            : $default;
74
    }
75
76
77
78
    /**
79
     * Read a value from the session and unset it, this is a form of read
80
     * once flash memory using the session.
81
     *
82
     * @param string $key     in session variable.
83
     * @param mixed  $default default value to return when key is not set
84
     *                        in the session.
85
     *
86
     * @return mixed value from session and null if not set.
87
     */
88 1
    public function getOnce($key, $default = null)
89
    {
90 1
        $read = $this->get($key, $default);
91 1
        $this->delete($key);
92 1
        return $read;
93
    }
94
95
96
97
    /**
98
     * Set values in session.
99
     *
100
     * @param string $key   in session variable.
101
     * @param mixed  $value to set in session.
102
     *
103
     * @return self
104
     *
105
     * @SuppressWarnings(PHPMD.Superglobals)
106
     */
107 3
    public function set($key, $value)
108
    {
109 3
        $_SESSION[$key] = $value;
110 3
        return $this;
111
    }
112
113
114
115
    /**
116
     * Unset session value of this key.
117
     *
118
     * @param string $key in session variable.
119
     *
120
     * @return self
121
     *
122
     * @SuppressWarnings(PHPMD.Superglobals)
123
     */
124 1
    public function delete($key)
125
    {
126 1
        if (isset($_SESSION[$key])) {
127 1
            unset($_SESSION[$key]);
128
        }
129 1
        return $this;
130
    }
131
132
133
134
    /**
135
     * Destroy the session (works even on cli).
136
     *
137
     * @return void
138
     *
139
     * @SuppressWarnings(PHPMD.Superglobals)
140
     */
141 1
    public function destroy()
142
    {
143
        // Unset all of the session variables.
144 1
        $_SESSION = [];
145
146
        // Delete the session cookie.
147 1
        if (php_sapi_name() !== 'cli'
148 1
            && ini_get("session.use_cookies")
149
        ) {
150
            $params = session_get_cookie_params();
151
            setcookie(
152
                session_name(),
153
                '',
154
                time() - 42000,
155
                $params["path"],
156
                $params["domain"],
157
                $params["secure"],
158
                $params["httponly"]
159
            );
160
        }
161
162
        // Finally, destroy the session.
163 1
        if (php_sapi_name() !== 'cli') {
164
            session_destroy();
165
        }
166 1
    }
167
168
169
170
    /**
171
     * To enable var_dump() of the session object.
172
     *
173
     * @return array
174
     *
175
     * @SuppressWarnings(PHPMD.Superglobals)
176
     */
177 1
    public function __debugInfo() : ?array
178
    {
179 1
        return $_SESSION ?? null;
180
    }
181
}
182