Test Failed
Pull Request — master (#19)
by Flo
04:39
created

SessionManager::setFlashbag()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
/**
3
 * Class SessionManager | SessionManager.php
4
 *
5
 * @package Faulancer\Session;
6
 * @author Florian Knapp <[email protected]>
7
 */
8
namespace Faulancer\Session;
9
10
/**
11
 * Class SessionManager
12
 */
13
class SessionManager
14
{
15
    /**
16
     * Start session handler
17
     * @return void
18
     * @codeCoverageIgnore Covered by php
19
     */
20
    public function startSession()
21
    {
22
        if (!$this->hasSession()) {
23
            session_start();
24
        }
25
    }
26
27
    /**
28
     * Destroy session at all
29
     * @return void
30
     */
31
    public function destroySession()
32
    {
33
        if ($this->hasSession()) {
34
            session_destroy();
35
        }
36
    }
37
38
    /**
39
     * Check if session exists
40
     * @return boolean
41
     */
42
    public function hasSession()
43
    {
44
        if (!empty(session_id())) {
45
            return true;
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * Set session key with value
53
     * @param string $key
54
     * @param string|array $value
55
     */
56
    public function set(string $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...
57
    {
58
        $_SESSION[$key] = $value;
59
    }
60
61
    /**
62
     * Get session value by key
63
     * @param string $key
64
     * @return null|string|array
65
     */
66
    public function get(string $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...
67
    {
68
        if (!isset($_SESSION[$key])) {
69
            return null;
70
        }
71
72
        return $_SESSION[$key];
73
    }
74
75
    /**
76
     * Delete session value by key
77
     * @param string $key
78
     * @return boolean
79
     */
80
    public function delete(string $key)
0 ignored issues
show
Coding Style introduced by
delete 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...
81
    {
82
        if (!isset($_SESSION[$key])) {
83
            return false;
84
        }
85
86
        unset($_SESSION[$key]);
87
88
        return true;
89
    }
90
91
    /**
92
     * Check if flash message key exists
93
     * @param string $key
94
     * @return boolean
95
     */
96
    public function hasFlashMessage(string $key)
0 ignored issues
show
Coding Style introduced by
hasFlashMessage 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...
97
    {
98
        return isset($_SESSION['flashMessage'][$key]) ? true : false;
99
    }
100
101
    /**
102
     * Set flashbag value by key
103
     * @param string|array      $key
104
     * @param null|string|array $value
105
     * @return boolean
106
     */
107
    public function setFlashMessage($key, $value = null)
0 ignored issues
show
Coding Style introduced by
setFlashMessage 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...
108
    {
109
        if (is_array($key)) {
110
111
            foreach ($key AS $k => $val) {
112
                $_SESSION['flashMessage'][$k] = $val;
113
            }
114
115
            return true;
116
117
        }
118
119
        $_SESSION['flashMessage'][$key] = $value;
120
121
        return true;
122
    }
123
124
    /**
125
     * Get flash message value by key
126
     *
127
     * @param string $key
128
     * @return null|string|array
129
     */
130
    public function getFlashMessage(string $key)
0 ignored issues
show
Coding Style introduced by
getFlashMessage 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...
131
    {
132
        if (!isset($_SESSION['flashMessage'][$key])) {
133
            return null;
134
        }
135
136
        $result = $_SESSION['flashMessage'][$key];
137
138
        unset($_SESSION['flashMessage'][$key]);
139
140
        return $result;
141
    }
142
143
}