Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.62% covered (success)
93.62%
44 / 47
92.86% covered (success)
92.86%
13 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
SessionNamespace
93.62% covered (success)
93.62%
44 / 47
92.86% covered (success)
92.86%
13 / 14
28.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 setNamespace
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getNamespace
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTimedValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setRequestValue
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 init
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
4.59
 kill
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 checkRequests
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 checkExpirations
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 toArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 __set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 __isset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __unset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Session;
15
16/**
17 * Session namespace class
18 *
19 * @category   Pop
20 * @package    Pop\Session
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.3.0
25 */
26class SessionNamespace extends AbstractSession
27{
28
29    /**
30     * Session namespace
31     * @var string
32     */
33    private $namespace = null;
34
35    /**
36     * Constructor
37     *
38     * Private method to instantiate the session object
39     *
40     * @param  string $namespace
41     * @throws Exception
42     */
43    public function __construct($namespace)
44    {
45        if ($namespace == '_POP_SESSION_') {
46            throw new Exception("Error: Cannot use the reserved namespace '_POP_SESSION_'.");
47        }
48        $this->setNamespace($namespace);
49        $sess = Session::getInstance();
50        if (!isset($sess[$namespace])) {
51            $sess[$namespace] = [];
52        }
53        $this->init();
54    }
55
56    /**
57     * Set current namespace
58     *
59     * @param  string $namespace
60     * @return SessionNamespace
61     */
62    public function setNamespace($namespace)
63    {
64        $this->namespace = $namespace;
65        return $this;
66    }
67
68    /**
69     * Get current namespace
70     *
71     * @return string
72     */
73    public function getNamespace()
74    {
75        return $this->namespace;
76    }
77
78    /**
79     * Set a time-based value
80     *
81     * @param  string $key
82     * @param  mixed  $value
83     * @param  int    $expire
84     * @return SessionNamespace
85     */
86    public function setTimedValue($key, $value, $expire = 300)
87    {
88        $_SESSION[$this->namespace][$key] = $value;
89        $_SESSION['_POP_SESSION_'][$this->namespace]['expirations'][$key] = time() + (int)$expire;
90        return $this;
91    }
92
93    /**
94     * Set a request-based value
95     *
96     * @param  string $key
97     * @param  mixed  $value
98     * @param  int    $hops
99     * @return SessionNamespace
100     */
101    public function setRequestValue($key, $value, $hops = 1)
102    {
103        $_SESSION[$this->namespace][$key] = $value;
104        $_SESSION['_POP_SESSION_'][$this->namespace]['requests'][$key] = [
105            'current' => 0,
106            'limit'   => (int)$hops
107        ];
108        return $this;
109    }
110
111    /**
112     * Init the session
113     *
114     * @return void
115     */
116    private function init()
117    {
118        if (!isset($_SESSION['_POP_SESSION_'])) {
119            $_SESSION['_POP_SESSION_'] = [
120                $this->namespace => [
121                    'requests'    => [],
122                    'expirations' => []
123                ]
124            ];
125        } else if (isset($_SESSION['_POP_SESSION_']) && !isset($_SESSION['_POP_SESSION_'][$this->namespace])) {
126            $_SESSION['_POP_SESSION_'][$this->namespace] = [
127                'requests'    => [],
128                'expirations' => []
129            ];
130        } else {
131            $this->checkRequests();
132            $this->checkExpirations();
133        }
134    }
135
136    /**
137     * Kill the session namespace
138     *
139     * @return void
140     */
141    public function kill()
142    {
143        if (isset($_SESSION[$this->namespace])) {
144            unset($_SESSION[$this->namespace]);
145        }
146    }
147
148    /**
149     * Check the request-based session values
150     *
151     * @return void
152     */
153    private function checkRequests()
154    {
155        foreach ($_SESSION[$this->namespace] as $key => $value) {
156            if (isset($_SESSION['_POP_SESSION_'][$this->namespace]['requests'][$key])) {
157                $_SESSION['_POP_SESSION_'][$this->namespace]['requests'][$key]['current']++;
158                $current = $_SESSION['_POP_SESSION_'][$this->namespace]['requests'][$key]['current'];
159                $limit   = $_SESSION['_POP_SESSION_'][$this->namespace]['requests'][$key]['limit'];
160                if ($current > $limit) {
161                    unset($_SESSION[$this->namespace][$key]);
162                    unset($_SESSION['_POP_SESSION_'][$this->namespace]['requests'][$key]);
163                }
164            }
165        }
166    }
167
168    /**
169     * Check the time-based session values
170     *
171     * @return void
172     */
173    private function checkExpirations()
174    {
175        foreach ($_SESSION[$this->namespace] as $key => $value) {
176            if (isset($_SESSION['_POP_SESSION_'][$this->namespace]['expirations'][$key]) &&
177                (time() > $_SESSION['_POP_SESSION_'][$this->namespace]['expirations'][$key])) {
178                unset($_SESSION[$this->namespace][$key]);
179                unset($_SESSION['_POP_SESSION_'][$this->namespace]['expirations'][$key]);
180            }
181        }
182    }
183
184    /**
185     * Get the session values as an array
186     *
187     * @return array
188     */
189    public function toArray()
190    {
191        return (isset($_SESSION[$this->namespace])) ? $_SESSION[$this->namespace] : null;
192    }
193
194    /**
195     * Set a property in the session object that is linked to the $_SESSION global variable
196     *
197     * @param  string $name
198     * @param  mixed $value
199     * @return void
200     */
201    public function __set($name, $value)
202    {
203        $_SESSION[$this->namespace][$name] = $value;
204    }
205
206    /**
207     * Get method to return the value of the $_SESSION global variable
208     *
209     * @param  string $name
210     * @return mixed
211     */
212    public function __get($name)
213    {
214        return (isset($_SESSION[$this->namespace][$name])) ? $_SESSION[$this->namespace][$name] : null;
215    }
216
217    /**
218     * Return the isset value of the $_SESSION global variable
219     *
220     * @param  string $name
221     * @return boolean
222     */
223    public function __isset($name)
224    {
225        return isset($_SESSION[$this->namespace][$name]);
226    }
227
228    /**
229     * Unset the $_SESSION global variable
230     *
231     * @param  string $name
232     * @return void
233     */
234    public function __unset($name)
235    {
236        $_SESSION[$this->namespace][$name] = null;
237        unset($_SESSION[$this->namespace][$name]);
238    }
239
240}