Passed
Push — master ( 963c04...636654 )
by Jean-Christophe
08:21
created

USession::setTmp()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

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