Completed
Push — master ( d13eb2...e589bc )
by Mikael
01:48
created

Session   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 43.75%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 16
c 6
b 0
f 0
lcom 1
cbo 0
dl 0
loc 172
ccs 21
cts 48
cp 0.4375
rs 10

9 Methods

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