Passed
Push — master ( 93f443...7dd0c4 )
by Mikael
01:34
created

Session   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 50%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
c 4
b 0
f 0
lcom 1
cbo 0
dl 0
loc 154
ccs 21
cts 42
cp 0.5
rs 10

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