Session   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 111
ccs 0
cts 43
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A set() 0 5 1
A destroy() 0 5 1
A update() 0 8 2
A get() 0 4 2
A has() 0 4 1
A getIterator() 0 4 1
1
<?php
2
namespace Nkey\Caribu\Mvc\Util;
3
4
use IteratorAggregate;
5
6
/**
7
 * This class contains to the Caribu MVC package.
8
 *
9
 * It provides an abstract access to session variables.
10
 *
11
 * @author Maik Greubel <[email protected]>
12
 */
13
class Session implements \IteratorAggregate
14
{
15
16
    /**
17
     * The session data
18
     *
19
     * @var array
20
     */
21
    private $sessionData = array();
22
23
    /**
24
     * The session namespace
25
     *
26
     * @var string
27
     */
28
    private $namespace = null;
29
30
    /**
31
     * The session identifier
32
     *
33
     * @var string
34
     */
35
    private $sessionId;
36
37
    /**
38
     * Create a new session instance
39
     *
40
     * @param string $namespace
41
     *            Optional name of session namespace
42
     */
43
    public function __construct($namespace = null)
0 ignored issues
show
Coding Style introduced by
__construct 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...
44
    {
45
        session_start();
46
        $this->sessionId = session_id();
47
        
48
        if (null === $namespace) {
49
            $this->sessionData = $_SESSION;
50
        } else {
51
            if (! isset($_SESSION[$namespace])) {
52
                $_SESSION[$namespace] = array();
53
            }
54
            $this->sessionData = $_SESSION[$namespace];
55
        }
56
    }
57
58
    /**
59
     * Set a specific session key to arbitrary data
60
     *
61
     * @param string $key
62
     *            The session data key
63
     * @param mixed $value
64
     *            The value
65
     */
66
    public function set($key, $value)
67
    {
68
        $this->sessionData[$key] = $value;
69
        $this->update();
70
    }
71
72
    /**
73
     * Destroy the session
74
     */
75
    public function destroy()
76
    {
77
        $this->sessionData = array();
78
        session_destroy();
79
    }
80
81
    /**
82
     * Update internal session data
83
     */
84
    private function update()
0 ignored issues
show
Coding Style introduced by
update 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...
85
    {
86
        if (null !== $this->namespace) {
87
            $_SESSION[$this->namespace] = $this->sessionData;
88
        } else {
89
            $_SESSION = $this->sessionData;
90
        }
91
    }
92
93
    /**
94
     * Get specific session data
95
     *
96
     * @param string $key
97
     * @return NULL|mixed
98
     */
99
    public function get($key)
100
    {
101
        return $this->has($key) ? $this->sessionData[$key] : null;
102
    }
103
104
    /**
105
     * Checks whether a specific key exists in session data
106
     *
107
     * @param string $key
108
     */
109
    public function has($key)
110
    {
111
        return isset($this->sessionData[$key]);
112
    }
113
114
    /**
115
     * (non-PHPdoc)
116
     *
117
     * @see IteratorAggregate::getIterator()
118
     */
119
    public function getIterator()
120
    {
121
        return new \ArrayIterator($this->sessionData);
122
    }
123
}
124