Passed
Push — 1.0.0-dev ( 93958a...e1c8ef )
by nguereza
02:26
created

Session::getLogger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
	defined('ROOT_PATH') or exit('Access denied');
3
	/**
4
	 * TNH Framework
5
	 *
6
	 * A simple PHP framework using HMVC architecture
7
	 *
8
	 * This content is released under the GNU GPL License (GPL)
9
	 *
10
	 * Copyright (C) 2017 Tony NGUEREZA
11
	 *
12
	 * This program is free software; you can redistribute it and/or
13
	 * modify it under the terms of the GNU General Public License
14
	 * as published by the Free Software Foundation; either version 3
15
	 * of the License, or (at your option) any later version.
16
	 *
17
	 * This program is distributed in the hope that it will be useful,
18
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
	 * GNU General Public License for more details.
21
	 *
22
	 * You should have received a copy of the GNU General Public License
23
	 * along with this program; if not, write to the Free Software
24
	 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
	*/
26
	class Session extends BaseStaticClass{
27
		
28
		/**
29
		 * The session flash key to use
30
		 * @const
31
		 */
32
		const SESSION_FLASH_KEY = 'session_flash';
33
34
		/**
35
		 * Get the session item value
36
		 * @param  string $item    the session item name to get
37
		 * @param  mixed $default the default value to use if can not find the session item in the list
38
		 * @return mixed          the session value if exist or the default value
39
		 */
40
		public static function get($item, $default = null){
41
			$logger = self::getLogger();
42
			$logger->debug('Getting session data for item [' .$item. '] ...');
43
			if(array_key_exists($item, $_SESSION)){
44
				$logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($_SESSION[$item]) . ']');
45
				return $_SESSION[$item];
46
			}
47
			$logger->warning('Cannot find session item [' . $item . '] using the default value ['. $default . ']');
48
			return $default;
49
		}
50
51
		/**
52
		 * Set the session item value
53
		 * @param string $item  the session item name to set
54
		 * @param mixed $value the session item value
55
		 */
56
		public static function set($item, $value){
57
			$logger = self::getLogger();
58
			$logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']');
59
			$_SESSION[$item] = $value;
60
		}
61
62
		/**
63
		 * Get the session flash item value
64
		 * @param  string $item    the session flash item name to get
65
		 * @param  mixed $default the default value to use if can not find the session flash item in the list
66
		 * @return mixed          the session flash value if exist or the default value
67
		 */
68
		public static function getFlash($item, $default = null){
69
			$logger = self::getLogger();
70
			$key = self::SESSION_FLASH_KEY.'_'.$item;
71
			$return = array_key_exists($key, $_SESSION) ?
72
			($_SESSION[$key]) : $default;
73
			if(array_key_exists($key, $_SESSION)){
74
				unset($_SESSION[$key]);
75
			}
76
			else{
77
				$logger->warning('Cannot find session flash item ['. $key .'] using the default value ['. $default .']');
78
			}
79
			return $return;
80
		}
81
82
		/**
83
		 * Check whether the given session flash item exists
84
		 * @param  string  $item the session flash item name
85
		 * @return boolean 
86
		 */
87
		public static function hasFlash($item){
88
			$key = self::SESSION_FLASH_KEY.'_'.$item;
89
			return array_key_exists($key, $_SESSION);
90
		}
91
92
		/**
93
		 * Set the session flash item value
94
		 * @param string $item  the session flash item name to set
95
		 * @param mixed $value the session flash item value
96
		 */
97
		public static function setFlash($item, $value){
98
			$key = self::SESSION_FLASH_KEY.'_'.$item;
99
			$_SESSION[$key] = $value;
100
		}
101
102
		/**
103
		 * Clear the session item in the list
104
		 * @param  string $item the session item name to be deleted
105
		 */
106
		public static function clear($item){
107
			$logger = self::getLogger();
108
			if(array_key_exists($item, $_SESSION)){
109
				$logger->info('Deleting of session for item ['.$item.' ]');
110
				unset($_SESSION[$item]);
111
			}
112
			else{
113
				$logger->warning('Session item ['.$item.'] to be deleted does not exists');
114
			}
115
		}
116
		
117
		/**
118
		 * Clear the session flash item in the list
119
		 * @param  string $item the session flash item name to be deleted
120
		 */
121
		public static function clearFlash($item){
122
			$logger = self::getLogger();
123
			$key = self::SESSION_FLASH_KEY.'_'.$item;
124
			if(array_key_exists($key, $_SESSION)){
125
				$logger->info('Delete session flash for item ['.$item.']');
126
				unset($_SESSION[$item]);
127
			}
128
			else{
129
				$logger->warning('Dession flash item ['.$item.'] to be deleted does not exists');
130
			}
131
		}
132
133
		/**
134
		 * Check whether the given session item exists
135
		 * @param  string  $item the session item name
136
		 * @return boolean 
137
		 */
138
		public static function exists($item){
139
			return array_key_exists($item, $_SESSION);
140
		}
141
142
		/**
143
		 * Destroy all session data values
144
		 */
145
		public static function clearAll(){
146
			session_unset();
147
			session_destroy();
148
		}
149
150
	}
151