Passed
Push — master ( 0deb8f...f54565 )
by Goffy
04:13
created

SessionStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 10 2
A get() 0 7 2
A __construct() 0 3 1
A remove() 0 7 1
A check() 0 4 2
1
<?php
2
3
namespace XoopsModules\Wggithub\Github\Storages;
4
5
use XoopsModules\Wggithub\Github;
6
7
8
/**
9
 * Session storage which uses $_SESSION directly. Session must be started already before use.
10
 *
11
 * @author  Miloslav Hůla (https://github.com/milo)
12
 */
13
class SessionStorage extends Github\Sanity implements ISessionStorage
14
{
15
	const SESSION_KEY = 'milo.github-api';
16
17
	/** @var string */
18
	private $sessionKey;
19
20
21
	/**
22
	 * @param  string
23
	 */
24
	public function __construct($sessionKey = self::SESSION_KEY)
25
	{
26
		$this->sessionKey = $sessionKey;
27
	}
28
29
30
	/**
31
	 * @param  string
32
	 * @param  mixed
33
	 * @return self
34
	 */
35
	public function set($name, $value)
36
	{
37
		if ($value === NULL) {
38
			return $this->remove($name);
39
		}
40
41
		$this->check(__METHOD__);
42
		$_SESSION[$this->sessionKey][$name] = $value;
43
44
		return $this;
45
	}
46
47
48
	/**
49
	 * @param  string
50
	 * @return mixed
51
	 */
52
	public function get($name)
53
	{
54
		$this->check(__METHOD__);
55
56
		return isset($_SESSION[$this->sessionKey][$name])
57
			? $_SESSION[$this->sessionKey][$name]
58
			: NULL;
59
	}
60
61
62
	/**
63
	 * @param  string
64
	 * @return self
65
	 */
66
	public function remove($name)
67
	{
68
		$this->check(__METHOD__);
69
70
		unset($_SESSION[$this->sessionKey][$name]);
71
72
		return $this;
73
	}
74
75
76
	/**
77
	 * @param  string
78
	 */
79
	private function check($method)
80
	{
81
		if (!isset($_SESSION)) {
82
			trigger_error("Start session before using $method().", E_USER_WARNING);
83
		}
84
	}
85
86
}
87