Completed
Push — master ( 8c3c1c...30ad8f )
by Jean-Christophe
01:42
created

SessionUtils::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ubiquity\utils;
4
5
/**
6
 * Session utilities
7
 * @author jc
8
 * @version 1.0.0.2
9
 */
10
class SessionUtils {
11
12
	/**
13
	 * Retourne un tableau stocké en variable de session sous le nom $arrayKey
14
	 * @param string $arrayKey
15
	 * @return array
16
	 */
17
	public static function getArray($arrayKey) {
18
		if (isset($_SESSION[$arrayKey])) {
19
			$array=$_SESSION[$arrayKey];
20
			if (!is_array($array))
21
				$array=array ();
22
		} else
23
			$array=array ();
24
		return $array;
25
	}
26
27
	public static function addOrRemoveValueFromArray($arrayKey, $value, $add=true) {
28
		$array=self::getArray($arrayKey);
29
		$_SESSION[$arrayKey]=$array;
30
		$search=array_search($value, $array);
31
		if ($search === FALSE && $add) {
32
			$_SESSION[$arrayKey][]=$value;
33
			return true;
34
		} else {
35
			unset($_SESSION[$arrayKey][$search]);
36
			$_SESSION[$arrayKey]=array_values($_SESSION[$arrayKey]);
37
			return false;
38
		}
39
	}
40
41
	public static function removeValueFromArray($arrayKey, $value) {
42
		return self::addOrRemoveValueFromArray($arrayKey, $value, false);
43
	}
44
45
	public static function checkBoolean($key) {
46
		$_SESSION[$key]=!self::getBoolean($key);
47
		return $_SESSION[$key];
48
	}
49
50
	public static function getBoolean($key) {
51
		$ret=false;
52
		if (isset($_SESSION[$key])) {
53
			$ret=$_SESSION[$key];
54
		}
55
		return $ret;
56
	}
57
58
	public static function session($key, $default=NULL) {
59
		return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
60
	}
61
62
	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...
63
		return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
64
	}
65
66
	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...
67
		$_SESSION[$key]=$value;
68
	}
69
70
	public static function delete($key) {
71
		unset($_SESSION[$key]);
72
	}
73
74
	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...
75
		if(isset($name)) \session_name();
76
		if (!isset($_SESSION)) { \session_start(); }
77
	}
78
79
	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...
80
		return isset($_SESSION);
81
	}
82
83
	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...
84
		if(!self::isStarted())
85
			return;
86
		$_SESSION = array();
87
88
		if (\ini_get("session.use_cookies")) {
89
			$params = \session_get_cookie_params();
90
			\setcookie(\session_name(), '', \time() - 42000,
91
					$params["path"], $params["domain"],
92
					$params["secure"], $params["httponly"]
93
					);
94
		}
95
		\session_destroy();
96
	}
97
}
98