Completed
Pull Request — master (#2344)
by Julito
39:02 queued 15:18
created

ChamiloSession::start()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 43
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 8
nop 1
dl 0
loc 43
rs 8.439
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A ChamiloSession::clear() 0 4 1
A ChamiloSession::offsetExists() 0 3 1
A ChamiloSession::has() 0 3 1
A ChamiloSession::destroy() 0 4 1
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
14
     * @return mixed|null
15
     */
16
    public 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
            if (isset($_SESSION[$variable])) {
27
                return $_SESSION[$variable];
28
            }
29
            return $default;
30
        } else {
31
            return $result;
32
        }
33
    }
34
35
    /**
36
     * @param string $variable
37
     * @param mixed $value
38
     */
39
    public static function write($variable, $value)
40
    {
41
        //$_SESSION[$variable] = $value;
42
        $session = Container::getSession();
43
        // Writing the session in 2 instances because
44
        $_SESSION[$variable] = $value;
45
        $session->set($variable, $value);
46
    }
47
48
    /**
49
     * @param string $variable
50
     */
51
    public static function erase($variable)
52
    {
53
        $variable = (string) $variable;
54
        $session = Container::getSession();
55
        $session->remove($variable);
56
57
        if (isset($GLOBALS[$variable])) {
58
            unset($GLOBALS[$variable]);
59
        }
60
        if (isset($_SESSION[$variable])) {
61
            unset($_SESSION[$variable]);
62
        }
63
    }
64
65
    /**
66
     * Returns true if session has variable set up, false otherwise.
67
     *
68
     * @param string $variable
69
     *
70
     * @return bool
71
     */
72
    public static function has($variable)
73
    {
74
        return isset($_SESSION[$variable]);
75
    }
76
77
    /**
78
     * Clear
79
     */
80
    public static function clear()
81
    {
82
        $session = Container::getSession();
83
        $session->clear();
84
    }
85
86
    /**
87
     * Destroy
88
     */
89
    public static function destroy()
90
    {
91
        $session = Container::getSession();
92
        $session->invalidate();
93
    }
94
95
    /*
96
     * ArrayAccess
97
     */
98
    public function offsetExists($offset)
99
    {
100
        return isset($_SESSION[$offset]);
101
    }
102
103
    /**
104
     * It it exists returns the value stored at the specified offset.
105
     * If offset does not exists returns null. Do not trigger a warning.
106
     *
107
     * @param string $offset
108
     * @return any
109
     */
110
    public function offsetGet($offset)
111
    {
112
        return self::read($offset);
113
    }
114
115
    public function offsetSet($offset, $value)
116
    {
117
        self::write($offset, $value);
118
    }
119
120
    public function offsetUnset($offset)
121
    {
122
        unset($_SESSION[$offset]);
123
    }
124
125
    /**
126
     * @param string $name
127
     */
128
    public function __unset($name)
129
    {
130
        unset($_SESSION[$name]);
131
    }
132
133
    /**
134
     * @param string $name
135
     * @return bool
136
     */
137
    public function __isset($name)
138
    {
139
        return self::has($name);
140
    }
141
142
    /**
143
     * It it exists returns the value stored at the specified offset.
144
     * If offset does not exists returns null. Do not trigger a warning.
145
     *
146
     * @param string $name
147
     *
148
     * @return mixed
149
     *
150
     */
151
    public function __get($name)
152
    {
153
        return self::read($name);
154
    }
155
156
    /**
157
     *
158
     * @param string $name
159
     * @param mixed $value
160
     */
161
    public function __set($name, $value)
162
    {
163
        self::write($name, $value);
164
    }
165
}
166