Completed
Push — master ( 3be334...ac7b95 )
by Mikael
01:56
created

Session::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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