Session::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Session class
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category   OpCacheGUI
8
 * @package    Storage
9
 * @author     Pieter Hordijk <[email protected]>
10
 * @copyright  Copyright (c) 2014 Pieter Hordijk
11
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
12
 * @version    1.0.0
13
 */
14
namespace OpCacheGUI\Storage;
15
16
/**
17
 * Session class
18
 *
19
 * @category   OpCacheGUI
20
 * @package    Storage
21
 * @author     Pieter Hordijk <[email protected]>
22
 */
23
class Session implements KeyValuePair, Regeneratable
24
{
25
    /**
26
     * Sets the value
27
     *
28
     * @param string $key   The key in which to store the value
29
     * @param mixed  $value The value to store
30
     *
31
     * @return void
32
     */
33 3
    public function set($key, $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...
34
    {
35 3
        $_SESSION[$key] = $value;
36 3
    }
37
38
    /**
39
     * Gets a value from the session superglobal
40
     *
41
     * @param string $key The key of which to retrieve the value
42
     *
43
     * @return mixed                                   The value
44
     * @throws \OpCacheGUI\Storage\InvalidKeyException When the key is not found
45
     */
46 2
    public function get($key)
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...
47
    {
48 2
        if (!$this->isKeyValid($key)) {
49 1
            throw new InvalidKeyException('Key (`' . $key . '`) not found in session.');
50
        }
51
52 1
        return $_SESSION[$key];
53
    }
54
55
    /**
56
     * Check whether the supplied key is valid (i.e. does exist in the session superglobal)
57
     *
58
     * @param string $key The key to check
59
     *
60
     * @return boolean Whether the supplied key is valid
61
     */
62 4
    public function isKeyValid($key)
0 ignored issues
show
Coding Style introduced by
isKeyValid 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...
63
    {
64 4
        if (array_key_exists($key, $_SESSION)) {
65 2
            return true;
66
        }
67
68 2
        return false;
69
    }
70
71
    /**
72
     * Regenerates a new session id and initializes the session superglobal
73
     *
74
     * @codeCoverageIgnore
75
     */
76
    public function regenerate()
0 ignored issues
show
Coding Style introduced by
regenerate 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...
77
    {
78
        session_regenerate_id(true);
79
        $_SESSION = [];
80
    }
81
}
82