Session::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wfdownloads\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 *  WfdownloadsSession class
17
 *
18
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
19
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
20
 * @package         Wfdownloads
21
 * @since           3.23
22
 * @author          trabis <[email protected]>
23
 * @author          Harry Fuecks (PHP Anthology Volume II)
24
 */
25
26
use XoopsModules\Wfdownloads;
27
28
// require_once  dirname(dirname(__DIR__)) . '/include/common.php';
29
30
/**
31
 * Class Session
32
 */
33
class Session
34
{
35
    /**
36
     * Session constructor<br>
37
     * Starts the session with session_start()
38
     * <strong>Note:</strong> that if the session has already started,
39
     * session_start() does nothing
40
     * @throws \RuntimeException
41
     */
42
    protected function __construct()
43
    {
44
        if (!@\session_start()) {
45
            throw new \RuntimeException('Session could not start.');
46
        }
47
    }
48
49
    /**
50
     * Sets a session variable
51
     *
52
     * @param string $name  name of variable
53
     * @param mixed  $value value of variable
54
     *
55
     * @access public
56
     */
57
    public function set($name, $value)
58
    {
59
        $_SESSION[$name] = $value;
60
    }
61
62
    /**
63
     * Fetches a session variable
64
     *
65
     * @param string $name name of variable
66
     *
67
     * @return mixed value of session variable
68
     * @access public
69
     */
70
    public function get($name)
71
    {
72
        return $_SESSION[$name] ?? false;
73
    }
74
75
    /**
76
     * Deletes a session variable
77
     *
78
     * @param string $name name of variable
79
     *
80
     * @access public
81
     */
82
    public function del($name)
83
    {
84
        unset($_SESSION[$name]);
85
    }
86
87
    /**
88
     * Destroys the whole session
89
     *
90
     * @access public
91
     */
92
    public function destroy()
93
    {
94
        $_SESSION = [];
95
        \session_destroy();
96
    }
97
98
    /**
99
     * @return \XoopsModules\Wfdownloads\Common\Session
100
     */
101
    public static function getInstance()
102
    {
103
        static $instance;
104
        if (null === $instance) {
105
            $instance = new static();
106
        }
107
108
        return $instance;
109
    }
110
}
111