Completed
Push — master ( 9fc245...19e0e1 )
by Jean-Christophe
02:13
created

SessionUtils::addValueToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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