Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

Session   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A prefix() 0 6 1
A set() 0 5 1
A get() 0 9 2
A del() 0 6 1
A destroy() 0 9 3
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf\Module\Helper;
13
14
/**
15
 * Manage session variables for a module. Session variable will be
16
 * prefixed with the module name to separate it from variables set
17
 * by other modules or system functions. All data is serialized, so
18
 * any arbitrary data (i.e. array) can be stored.
19
 *
20
 * @category  Xmf\Module\Helper\Session
21
 * @package   Xmf
22
 * @author    trabis <[email protected]>
23
 * @author    Richard Griffith <[email protected]>
24
 * @copyright 2011-2016 XOOPS Project (http://xoops.org)
25
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26
 * @version   Release: 1.0
27
 * @link      http://xoops.org
28
 * @since     1.0
29
 */
30
class Session extends AbstractHelper
31
{
32
    /**
33
     * @var string
34
     */
35
    protected $prefix;
36
37
    /**
38
     * Initialize parent::__constuct calls this after verifying module object.
39
     *
40
     * @return void
41
     */
42
    public function init()
43
    {
44
        $this->prefix = $this->module->getVar('dirname') . '_';
45
    }
46
47
    /**
48
     * Add our module prefix to a name
49
     *
50
     * @param string $name name to prefix
51
     *
52
     * @return string module prefixed name
53
     */
54
    protected function prefix($name)
55
    {
56
        $prefixedName = $this->prefix . $name;
57
58
        return $prefixedName;
59
    }
60
61
    /**
62
     * Sets a named session variable respecting our module prefix
63
     *
64
     * @param string $name  name of variable
65
     * @param mixed  $value value of variable
66
     *
67
     * @return void
68
     */
69
    public function set($name, $value)
0 ignored issues
show
Coding Style introduced by
set uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
70
    {
71
        $prefixedName = $this->prefix($name);
72
        $_SESSION[$prefixedName] = serialize($value);
73
    }
74
75
    /**
76
     * Fetch a named session variable respecting our module prefix
77
     *
78
     * @param string $name    name of variable
79
     * @param mixed  $default default value to return if config $name is not set
80
     *
81
     * @return mixed  $value value of session variable or false if not set
82
     */
83
    public function get($name, $default = false)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
84
    {
85
        $prefixedName = $this->prefix($name);
86
        if (isset($_SESSION[$prefixedName])) {
87
            return unserialize($_SESSION[$prefixedName]);
88
        } else {
89
            return $default;
90
        }
91
    }
92
93
    /**
94
     * Deletes a named session variable respecting our module prefix
95
     *
96
     * @param string $name name of variable
97
     *
98
     * @return void
99
     */
100
    public function del($name)
0 ignored issues
show
Coding Style introduced by
del uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
101
    {
102
        $prefixedName = $this->prefix($name);
103
        $_SESSION[$prefixedName] = null;
104
        unset($_SESSION[$prefixedName]);
105
    }
106
107
    /**
108
     * Delete all session variable starting with our module prefix
109
     *
110
     * @return void
111
     */
112
    public function destroy()
0 ignored issues
show
Coding Style introduced by
destroy uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
113
    {
114
        foreach ($_SESSION as $key => $value) {
115
            if (0 == substr_compare($key, $this->prefix, 0, strlen($this->prefix))) {
116
                $_SESSION[$key] = null;
117
                unset($_SESSION[$key]);
118
            }
119
        }
120
    }
121
}
122