Test Failed
Pull Request — master (#19)
by Flo
03:42
created

SessionManager::getFlashbagFormData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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
    public function has(string $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...
92
    {
93
        if (empty($_SESSION[$key])) {
94
            return false;
95
        }
96
97
        return true;
98
    }
99
100
    /**
101
     * Check if flash message key exists
102
     * @param string $key
103
     * @return boolean
104
     */
105
    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...
106
    {
107
        return isset($_SESSION['flashMessage'][$key]) ? true : false;
108
    }
109
110
    /**
111
     * Set flashbag value by key
112
     * @param string|array      $key
113
     * @param null|string|array $value
114
     * @return boolean
115
     */
116
    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...
117
    {
118
        if (is_array($key)) {
119
120
            foreach ($key AS $k => $val) {
121
                $_SESSION['flashMessage'][$k] = $val;
122
            }
123
124
            return true;
125
126
        }
127
128
        $_SESSION['flashMessage'][$key] = $value;
129
130
        return true;
131
    }
132
133
    /**
134
     * Get flash message value by key
135
     *
136
     * @param string $key
137
     * @return null|string|array
138
     */
139
    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...
140
    {
141
        if (!isset($_SESSION['flashMessage'][$key])) {
142
            return null;
143
        }
144
145
        $result = $_SESSION['flashMessage'][$key];
146
147
        unset($_SESSION['flashMessage'][$key]);
148
149
        return $result;
150
    }
151
152
}