SessionStorage::check()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 2
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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