Completed
Push — master ( 6427e5...77fbe2 )
by Jean-Christophe
01:41
created

USession::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
use Ubiquity\utils\base\UString;
6
7
/**
8
 * Http Session utilities
9
 *
10
 * @author jc
11
 * @version 1.0.0.4
12
 */
13
class USession {
14
	private static $name;
15
16
	/**
17
	 * Returns an array stored in session variable as $arrayKey
18
	 *
19
	 * @param string $arrayKey
20
	 *        	the key of the array to return
21
	 * @return array
22
	 */
23
	public static function getArray($arrayKey) {
24
		self::start ();
25
		if (isset ( $_SESSION [$arrayKey] )) {
26
			$array = $_SESSION [$arrayKey];
27
			if (! is_array ( $array ))
28
				$array = [ ];
29
		} else
30
			$array = [ ];
31
		return $array;
32
	}
33
34
	/**
35
	 * Adds or removes a value from an array in session
36
	 *
37
	 * @param string $arrayKey
38
	 *        	the key of the array to add or remove in
39
	 * @param mixed $value
40
	 *        	the value to add
41
	 * @param boolean $add
42
	 *        	If true, adds otherwise removes
43
	 * @return boolean
44
	 */
45
	public static function addOrRemoveValueFromArray($arrayKey, $value, $add = true) {
46
		$array = self::getArray ( $arrayKey );
47
		$_SESSION [$arrayKey] = $array;
48
		$search = array_search ( $value, $array );
49
		if ($search === FALSE && $add) {
50
			$_SESSION [$arrayKey] [] = $value;
51
			return true;
52
		} else {
53
			unset ( $_SESSION [$arrayKey] [$search] );
54
			$_SESSION [$arrayKey] = array_values ( $_SESSION [$arrayKey] );
55
			return false;
56
		}
57
	}
58
59
	/**
60
	 * Removes a value from an array in session
61
	 *
62
	 * @param string $arrayKey
63
	 *        	the key of the array to remove in
64
	 * @param mixed $value
65
	 *        	the value to remove
66
	 * @return boolean
67
	 */
68
	public static function removeValueFromArray($arrayKey, $value) {
69
		return self::addOrRemoveValueFromArray ( $arrayKey, $value, false );
70
	}
71
72
	/**
73
	 * Adds a value from an array in session
74
	 *
75
	 * @param string $arrayKey
76
	 *        	the key of the array to add in
77
	 * @param mixed $value
78
	 *        	the value to add
79
	 * @return boolean
80
	 */
81
	public static function addValueToArray($arrayKey, $value) {
82
		return self::addOrRemoveValueFromArray ( $arrayKey, $value, true );
83
	}
84
85
	/**
86
	 * Sets a boolean value at key position in session
87
	 *
88
	 * @param string $key
89
	 *        	the key to add or set in
90
	 * @param mixed $value
91
	 *        	the value to set
92
	 * @return boolean
93
	 */
94
	public static function setBoolean($key, $value) {
95
		$_SESSION [$key] = UString::isBooleanTrue ( $value );
96
		return $_SESSION [$key];
97
	}
98
99
	/**
100
	 * Returns a boolean stored at the key position in session
101
	 *
102
	 * @param string $key
103
	 *        	the key to add or set
104
	 * @return boolean
105
	 */
106
	public static function getBoolean($key) {
107
		self::start ();
108
		$ret = false;
109
		if (isset ( $_SESSION [$key] )) {
110
			$ret = UString::isBooleanTrue ( $_SESSION [$key] );
111
		}
112
		return $ret;
113
	}
114
115
	/**
116
	 * Returns the value stored at the key position in session
117
	 *
118
	 * @param string $key
119
	 *        	the key to retreive
120
	 * @param mixed $default
121
	 *        	the default value to return if the key does not exists in session
122
	 * @return mixed
123
	 */
124
	public static function session($key, $default = NULL) {
125
		self::start ();
126
		return isset ( $_SESSION [$key] ) ? $_SESSION [$key] : $default;
127
	}
128
129
	/**
130
	 * Returns the value stored at the key position in session
131
	 *
132
	 * @param string $key
133
	 *        	the key to retreive
134
	 * @param mixed $default
135
	 *        	the default value to return if the key does not exists in session
136
	 * @return mixed
137
	 */
138
	public static function get($key, $default = NULL) {
0 ignored issues
show
Coding Style introduced by
get 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...
139
		self::start ();
140
		return isset ( $_SESSION [$key] ) ? $_SESSION [$key] : $default;
141
	}
142
143
	/**
144
	 * Adds or sets a value to the Session at position $key
145
	 *
146
	 * @param string $key
147
	 *        	the key to add or set
148
	 * @param mixed $value
149
	 */
150
	public static function set($key, $value) {
0 ignored issues
show
Coding Style introduced by
set 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...
151
		$_SESSION [$key] = $value;
152
		return $value;
153
	}
154
155
	/**
156
	 * Deletes the key in Session
157
	 *
158
	 * @param string $key
159
	 *        	the key to delete
160
	 */
161
	public static function delete($key) {
162
		self::start ();
163
		unset ( $_SESSION [$key] );
164
	}
165
166
	/**
167
	 * Increment the value at the key index in session
168
	 *
169
	 * @param string $key
170
	 * @param number $inc
171
	 * @return number
172
	 */
173
	public static function inc($key, $inc = 1) {
174
		return self::set ( $key, self::get ( $key, 0 ) + $inc );
175
	}
176
177
	/**
178
	 * Decrement the value at the key index in session
179
	 *
180
	 * @param string $key
181
	 * @param number $dec
182
	 * @return number
183
	 */
184
	public static function dec($key, $dec = 1) {
185
		return self::set ( $key, self::get ( $key, 0 ) - $dec );
186
	}
187
188
	/**
189
	 * Adds a string at the end of the value at the key index in session
190
	 *
191
	 * @param string $key
192
	 * @param string $str
193
	 * @return string
194
	 */
195
	public static function concat($key, $str, $default = NULL) {
196
		return self::set ( $key, self::get ( $key, $default ) . $str );
197
	}
198
199
	/**
200
	 * Applies a callback function to the value at the key index in session
201
	 *
202
	 * @param string $key
203
	 * @param string|callable $callback
204
	 * @return mixed
205
	 */
206
	public static function apply($key, $callback, $default = NULL) {
207
		$value = self::get ( $key, $default );
208
		if (is_string ( $callback ) && function_exists ( $callback )) {
209
			$value = call_user_func ( $callback, $value );
210
		} elseif (is_callable ( $callback )) {
211
			$value = $callback ( $value );
212
		} else {
213
			return $value;
214
		}
215
		return self::set ( $key, $value );
216
	}
217
218
	/**
219
	 * Apply a user supplied function to every member of Session array
220
	 *
221
	 * @param callable $callback
222
	 * @param mixed $userData
223
	 * @return array
224
	 */
225
	public static function Walk($callback, $userData = null) {
226
		self::start ();
227
		array_walk ( $_SESSION, $callback, $userData );
228
		return $_SESSION;
229
	}
230
231
	/**
232
	 * Replaces elements from Session array with $keyAndValues
233
	 *
234
	 * @param array $keyAndValues
235
	 * @return array
236
	 */
237
	public static function replace($keyAndValues) {
238
		self::start ();
239
		$_SESSION = array_replace ( $_SESSION, $keyAndValues );
240
		return $_SESSION;
241
	}
242
243
	/**
244
	 * Returns the associative array of session vars
245
	 *
246
	 * @return array
247
	 */
248
	public static function getAll() {
0 ignored issues
show
Coding Style introduced by
getAll 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...
249
		self::start ();
250
		return $_SESSION;
251
	}
252
253
	/**
254
	 * Start new or resume existing session
255
	 *
256
	 * @param string|null $name
257
	 *        	the name of the session
258
	 */
259
	public static function start($name = null) {
0 ignored issues
show
Coding Style introduced by
start 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...
260
		if (! isset ( $_SESSION )) {
261
			if (isset ( $name ) && $name !== "") {
262
				self::$name = $name;
263
			}
264
			if (isset ( self::$name )) {
265
				\session_name ( self::$name );
266
			}
267
			\session_start ();
268
		}
269
	}
270
271
	/**
272
	 * Returns true if the session is started
273
	 *
274
	 * @return boolean
275
	 */
276
	public static function isStarted() {
0 ignored issues
show
Coding Style introduced by
isStarted 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...
277
		return isset ( $_SESSION );
278
	}
279
280
	/**
281
	 * Returns true if the key exists in Session
282
	 *
283
	 * @param string $key
284
	 *        	the key to test
285
	 * @return boolean
286
	 */
287
	public static function exists($key) {
0 ignored issues
show
Coding Style introduced by
exists 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...
288
		self::start ();
289
		return isset ( $_SESSION [$key] );
290
	}
291
	
292
	/**
293
	 * Initialize the key in Session if key does not exists
294
	 * @param string $key
295
	 * @param mixed $value
296
	 * @return mixed
297
	 */
298
	public static function init($key,$value){
1 ignored issue
show
Coding Style introduced by
init 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...
299
		if(!isset($_SESSION[$key])){
300
			$_SESSION[$key]=$value;
301
		}
302
		return $_SESSION[$key];
303
	}
304
305
	/**
306
	 * Terminates the active session
307
	 */
308
	public static function terminate() {
0 ignored issues
show
Coding Style introduced by
terminate 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...
309
		if (! self::isStarted ())
310
			return;
311
		self::start ();
312
		$_SESSION = array ();
313
314
		if (\ini_get ( "session.use_cookies" )) {
315
			$params = \session_get_cookie_params ();
316
			\setcookie ( \session_name (), '', \time () - 42000, $params ["path"], $params ["domain"], $params ["secure"], $params ["httponly"] );
317
		}
318
		\session_destroy ();
319
	}
320
}
321