Completed
Push — master ( 32b61b...fad9f9 )
by Jean-Christophe
01:41
created

Session::start()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 5
nop 1
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
use Ubiquity\utils\base\UString;
6
7
/**
8
 * Http Session utilities
9
 * @author jc
10
 * @version 1.0.0.4
11
 */
12
class Session {
13
	private static $name;
14
15
	/**
16
	 * Returns an array stored in session variable as $arrayKey
17
	 * @param string $arrayKey the key of the array to return
18
	 * @return array
19
	 */
20
	public static function getArray($arrayKey) {
21
		self::start();
22
		if (isset($_SESSION[$arrayKey])) {
23
			$array=$_SESSION[$arrayKey];
24
			if (!is_array($array))
25
				$array=[ ];
26
		} else
27
			$array=[ ];
28
		return $array;
29
	}
30
31
	/**
32
	 * Adds or removes a value from an array in session
33
	 * @param string $arrayKey the key of the array to add or remove in
34
	 * @param mixed $value the value to add
35
	 * @param boolean $add If true, adds otherwise removes
36
	 * @return boolean
37
	 */
38
	public static function addOrRemoveValueFromArray($arrayKey, $value, $add=true) {
39
		$array=self::getArray($arrayKey);
40
		$_SESSION[$arrayKey]=$array;
41
		$search=array_search($value, $array);
42
		if ($search === FALSE && $add) {
43
			$_SESSION[$arrayKey][]=$value;
44
			return true;
45
		} else {
46
			unset($_SESSION[$arrayKey][$search]);
47
			$_SESSION[$arrayKey]=array_values($_SESSION[$arrayKey]);
48
			return false;
49
		}
50
	}
51
52
	/**
53
	 * Removes a value from an array in session
54
	 * @param string $arrayKey the key of the array to remove in
55
	 * @param mixed $value the value to remove
56
	 * @return boolean
57
	 */
58
	public static function removeValueFromArray($arrayKey, $value) {
59
		return self::addOrRemoveValueFromArray($arrayKey, $value, false);
60
	}
61
62
	/**
63
	 * Adds a value from an array in session
64
	 * @param string $arrayKey the key of the array to add in
65
	 * @param mixed $value the value to add
66
	 * @return boolean
67
	 */
68
	public static function addValueToArray($arrayKey, $value) {
69
		return self::addOrRemoveValueFromArray($arrayKey, $value, true);
70
	}
71
72
	/**
73
	 * Sets a boolean value at key position in session
74
	 * @param string $key the key to add or set in
75
	 * @param mixed $value the value to set
76
	 * @return boolean
77
	 */
78
	public static function setBoolean($key, $value) {
79
		$_SESSION[$key]=UString::isBooleanTrue($value);
80
		return $_SESSION[$key];
81
	}
82
83
	/**
84
	 * Returns a boolean stored at the key position in session
85
	 * @param string $key the key to add or set
86
	 * @return boolean
87
	 */
88
	public static function getBoolean($key) {
89
		self::start();
90
		$ret=false;
91
		if (isset($_SESSION[$key])) {
92
			$ret=UString::isBooleanTrue($_SESSION[$key]);
93
		}
94
		return $ret;
95
	}
96
97
	/**
98
	 * Returns the value stored at the key position in session
99
	 * @param string $key the key to retreive
100
	 * @param mixed $default the default value to return if the key does not exists in session
101
	 * @return mixed
102
	 */
103
	public static function session($key, $default=NULL) {
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
104
		self::start();
105
		return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
106
	}
107
108
	/**
109
	 * Returns the value stored at the key position in session
110
	 * @param string $key the key to retreive
111
	 * @param mixed $default the default value to return if the key does not exists in session
112
	 * @return mixed
113
	 */
114
	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...
115
		self::start();
116
		return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
117
	}
118
119
	/**
120
	 * Adds or sets a value to the Session at position $key
121
	 * @param string $key the key to add or set
122
	 * @param mixed $value
123
	 */
124
	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...
125
		$_SESSION[$key]=$value;
126
		return $value;
127
	}
128
129
	/**
130
	 * Deletes the key in Session
131
	 * @param string $key the key to delete
132
	 */
133
	public static function delete($key) {
134
		self::start();
135
		unset($_SESSION[$key]);
136
	}
137
138
	/**
139
	 * Increment the value at the key index in session
140
	 * @param string $key
141
	 * @param number $inc
142
	 * @return number
143
	 */
144
	public static function inc($key, $inc=1) {
145
		return self::set($key, self::get($key, 0) + $inc);
146
	}
147
148
	/**
149
	 * Decrement the value at the key index in session
150
	 * @param string $key
151
	 * @param number $dec
152
	 * @return number
153
	 */
154
	public static function dec($key, $dec=1) {
155
		return self::set($key, self::get($key, 0) - $dec);
156
	}
157
158
	/**
159
	 * Start new or resume existing session
160
	 * @param string|null $name the name of the session
161
	 */
162
	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...
163
		if (!isset($_SESSION)) {
164
			if (isset($name) && $name !== "") {
165
				self::$name=$name;
166
			}
167
			if (isset(self::$name)) {
168
				\session_name(self::$name);
169
			}
170
			\session_start();
171
		}
172
	}
173
174
	/**
175
	 * Returns true if the session is started
176
	 * @return boolean
177
	 */
178
	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...
179
		return isset($_SESSION);
180
	}
181
182
	/**
183
	 * Returns true if the key exists in Session
184
	 * @param string $key the key to test
185
	 * @return boolean
186
	 */
187
	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...
188
		self::start();
189
		return isset($_SESSION[$key]);
190
	}
191
192
	/**
193
	 * Terminates the active session
194
	 */
195
	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...
196
		if (!self::isStarted())
197
			return;
198
		self::start();
199
		$_SESSION=array ();
200
201
		if (\ini_get("session.use_cookies")) {
202
			$params=\session_get_cookie_params();
203
			\setcookie(\session_name(), '', \time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
204
		}
205
		\session_destroy();
206
	}
207
}
208