Completed
Push — develop ( aad7d9...222a68 )
by Michael
02:33
created

Session::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 * @version 1.0.0
9
 */
10
11
namespace miBadger\Http;
12
13
use miBadger\Singleton\SingletonTrait;
14
15
/**
16
 * The session class
17
 *
18
 * @since 1.0.0
19
 */
20
class Session implements \IteratorAggregate
21
{
22
	use SingletonTrait;
23
24
	/** @var array The session. */
25
	private $session;
26
27
	/**
28
	 * Construct a Sesssion object.
29
	 *
30
	 * @global array $_SESSION The session parameters.
31
	 */
32 14
	protected function __construct()
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
33
	{
34 14
		$this->session = &$_SESSION;
35 14
	}
36
37
	/**
38
	 * Initialize the session, if none exists.
39
	 *
40
	 * @param $name = null
41
	 * @return null
42
	 * @throws \RuntimeException on failure.
43
	 */
44 5
	public static function start($name = null)
45
	{
46 5
		if (session_status() !== PHP_SESSION_NONE) {
47 1
			throw new \RuntimeException('Can\'t start a new session.');
48
		}
49
50 5
		if ($name !== null) {
51 2
			session_name($name);
52
		}
53
54 5
		if (!session_start()) {
55
			throw new \RuntimeException('Failed to start a new session.');
56
		}
57 5
	}
58
59
	/**
60
	 * Destroy the session, if one exists.
61
	 *
62
	 * @return null
63
	 * @throws \RuntimeException on failure.
64
	 */
65 3
	public static function destroy()
66
	{
67 3
		if (session_status() !== PHP_SESSION_ACTIVE) {
68 1
			throw new \RuntimeException('Can\'t destroy the session. There is no active session.');
69
		}
70
71 3
		if (!session_destroy()) {
72 1
			throw new \RuntimeException('Failed to destroy the active session.');
73
		}
74 2
	}
75
76
	/**
77
	 * {@inheritdoc}
78
	 */
79 1
	public function getIterator()
80
	{
81 1
		return new \ArrayIterator(static::getInstance()->session);
82
	}
83
84
	/**
85
	 * Returns the number of key-value mappings in the session map.
86
	 *
87
	 * @return int the number of key-value mappings in the session map.
88
	 */
89 1
	public static function count()
90
	{
91 1
		return count(static::getInstance()->session);
92
	}
93
94
	/**
95
	 * Returns true if the session map contains no key-value mappings.
96
	 *
97
	 * @return bool true if the session map contains no key-value mappings.
98
	 */
99 2
	public static function isEmpty()
100
	{
101 2
		return empty(static::getInstance()->session);
102
	}
103
104
	/**
105
	 * Returns true if the session map contains a mapping for the specified key.
106
	 *
107
	 * @param string $key
108
	 * @return bool true if the session map contains a mapping for the specified key.
109
	 */
110 4
	public static function containsKey($key)
111
	{
112 4
		return isset(static::getInstance()->session[$key]);
113
	}
114
115
	/**
116
	 * Returns true if the session map maps one or more keys to the specified value.
117
	 *
118
	 * @param string $value
119
	 * @return bool true if the session map maps one or more keys to the specified value.
120
	 */
121 1
	public static function containsValue($value)
122
	{
123 1
		return in_array($value, static::getInstance()->session);
124
	}
125
126
	/**
127
	 * Returns the value to which the specified key is mapped, or null if the session map contains no mapping for the key.
128
	 *
129
	 * @param string $key
130
	 * @return string|null the value to which the specified key is mapped, or null if the session map contains no mapping for the key.
131
	 */
132 2
	public static function get($key)
133
	{
134 2
		return static::containsKey($key) ? static::getInstance()->session[$key] : null;
135
	}
136
137
	/**
138
	 * Associates the specified value with the specified key in the session map.
139
	 *
140
	 * @param string $key
141
	 * @param string $value
142
	 * @return null
143
	 */
144 8
	public static function set($key, $value)
145
	{
146 8
		static::getInstance()->session[$key] = $value;
147 8
	}
148
149
	/**
150
	 * Removes the mapping for the specified key from the session map if present.
151
	 *
152
	 * @param string $key
153
	 * @return null
154
	 */
155 1
	public static function remove($key)
156
	{
157 1
		if (static::containsKey($key)) {
158 1
			unset(static::getInstance()->session[$key]);
159
		}
160 1
	}
161
162
	/**
163
	 * Removes all of the mappings from the session map.
164
	 *
165
	 * @return null
166
	 */
167 1
	public static function clear()
168
	{
169 1
		static::getInstance()->session = [];
170 1
	}
171
}
172