Completed
Push — master ( 73cb1f...8d952e )
by Julito
33:13
created

ChamiloSession::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Framework\Container;
5
6
/**
7
 * @todo replace all $_SESSION calls with this class.
8
 */
9
class ChamiloSession implements \ArrayAccess
10
{
11
    /**
12
     * @param string $variable
13
     * @param null $default
14
     * @return mixed|null
15
     */
16
    static function read($variable, $default = null)
17
    {
18
        $session = Container::getSession();
19
        $result = null;
20
        if (isset($session)) {
21
            $result = $session->get($variable);
22
        }
23
24
        // Check if the value exists in the $_SESSION array
25
        if (empty($result)) {
26
            return $default;
27
        } else {
28
            return $result;
29
        }
30
    }
31
32
    /**
33
     * @param string $variable
34
     * @param mixed $value
35
     */
36
    static function write($variable, $value)
37
    {
38
        //$_SESSION[$variable] = $value;
39
        $session = Container::getSession();
40
        // Writing the session in 2 instances because
41
        $_SESSION[$variable] = $value;
42
        $session->set($variable, $value);
43
    }
44
45
    /**
46
     * @param string $variable
47
     */
48
    static function erase($variable)
49
    {
50
        $variable = (string) $variable;
51
        $session = Container::getSession();
52
        $session->remove($variable);
53
54
        if (isset($GLOBALS[$variable])) {
55
            unset($GLOBALS[$variable]);
56
        }
57
        if (isset($_SESSION[$variable])) {
58
            unset($_SESSION[$variable]);
59
        }
60
    }
61
62
    /**
63
     * Returns true if session has variable set up, false otherwise.
64
     *
65
     * @param string $variable
66
     *
67
     * @return bool
68
     */
69
    static function has($variable)
70
    {
71
        return isset($_SESSION[$variable]);
72
    }
73
74
    /**
75
     * Clear
76
     */
77
    static function clear()
78
    {
79
        $session = Container::getSession();
80
        $session->clear();
81
    }
82
83
    /**
84
     * Destroy
85
     */
86
    static function destroy()
87
    {
88
        $session = Container::getSession();
89
        $session->invalidate();
90
    }
91
92
    /*
93
     * ArrayAccess
94
     */
95
    public function offsetExists($offset)
96
    {
97
        return isset($_SESSION[$offset]);
98
    }
99
100
    /**
101
     * It it exists returns the value stored at the specified offset.
102
     * If offset does not exists returns null. Do not trigger a warning.
103
     *
104
     * @param string $offset
105
     * @return any
106
     */
107
    public function offsetGet($offset)
108
    {
109
        return self::read($offset);
110
    }
111
112
    public function offsetSet($offset, $value)
113
    {
114
        self::write($offset, $value);
115
    }
116
117
    public function offsetUnset($offset)
118
    {
119
        unset($_SESSION[$offset]);
120
    }
121
122
    /**
123
     * @param string $name
124
     */
125
    public function __unset($name)
126
    {
127
        unset($_SESSION[$name]);
128
    }
129
130
    /**
131
     * @param string $name
132
     * @return bool
133
     */
134
    public function __isset($name)
135
    {
136
        return self::has($name);
137
    }
138
139
    /**
140
     * It it exists returns the value stored at the specified offset.
141
     * If offset does not exists returns null. Do not trigger a warning.
142
     *
143
     * @param string $name
144
     *
145
     * @return mixed
146
     *
147
     */
148
    function __get($name)
149
    {
150
        return self::read($name);
151
    }
152
153
    /**
154
     *
155
     * @param string $name
156
     * @param mixed $value
157
     */
158
    function __set($name, $value)
159
    {
160
        self::write($name, $value);
161
    }
162
}
163