Completed
Push — development ( e80362...5ad269 )
by Claudio
02:41
created

Session::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Session::rename() 0 4 1
1
<?php
2
3
namespace App\Helpers;
4
5
use App\Singleton;
6
7
/**
8
 * Class Session
9
 * @package App\Helpers
10
 */
11
final class Session extends Singleton
12
{
13
    /**
14
     * Rename the Session ID.
15
     *
16
     * @param string $name
17
     */
18
    public function rename($name)
19
    {
20
        session_name($name);
21
    }
22
23
    /**
24
     * Start Session Handler.
25
     */
26
    public function start()
27
    {
28
        session_start();
29
    }
30
31
    /**
32
     * Stop Session Handler.
33
     */
34
    public function destroy()
35
    {
36
        session_destroy();
37
    }
38
39
    /**
40
     * Store a Variable in the Session.
41
     *
42
     * @param string $key
43
     * @param mixed $value
44
     *
45
     * @return mixed
46
     */
47
    public function set($key, $value)
48
    {
49
        $_SESSION[$key] = $value;
50
51
        return $value;
52
    }
53
54
    /**
55
     * Get a Attribute Value from Session.
56
     *
57
     * @param string $key
58
     *
59
     * @return mixed
60
     */
61
    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...
62
    {
63
        return $this->has($key) ? $_SESSION[$key] : null;
64
    }
65
66
    /**
67
     * Check if a Key exists in the Session.
68
     *
69
     * @param mixed $key
70
     *
71
     * @return bool
72
     */
73
    public function has($key)
0 ignored issues
show
Coding Style introduced by
has 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...
74
    {
75
        return array_key_exists($key, $_SESSION);
76
    }
77
78
    /**
79
     * Erase a Attribute from Session.
80
     *
81
     * @param string $key
82
     */
83
    public function erase($key)
0 ignored issues
show
Coding Style introduced by
erase 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
        $_SESSION[$key] = null;
86
87
        unset($_SESSION[$key]);
88
    }
89
}
90