Passed
Branch master (9957c5)
by Jean-Christophe
12:44
created

USession::isStarted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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