Completed
Pull Request — master (#16)
by Flo
07:29
created

SessionManager::getFlashbag()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
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
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 flashbag key exists
93
     * @param string $key
94
     * @return boolean
95
     */
96
    public function hasFlashbagKey(string $key)
0 ignored issues
show
Coding Style introduced by
hasFlashbagKey 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['flashbag'][$key]) ? true : false;
99
    }
100
101
    /**
102
     * Check if flashbag error key exists
103
     * @param string $key
104
     * @return boolean
105
     */
106
    public function hasFlashbagErrorsKey($key)
0 ignored issues
show
Coding Style introduced by
hasFlashbagErrorsKey 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...
107
    {
108
        return isset($_SESSION['flashbag']['errors'][$key]) ? true : false;
109
    }
110
111
    /**
112
     * Set flashbag value by key
113
     * @param string|array      $key
114
     * @param null|string|array $value
115
     * @return boolean
116
     */
117
    public function setFlashbag($key, $value = null)
0 ignored issues
show
Coding Style introduced by
setFlashbag 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...
118
    {
119
        if (is_array($key)) {
120
121
            foreach ($key AS $k => $val) {
122
                $_SESSION['flashbag'][$k] = $val;
123
            }
124
125
            return true;
126
127
        }
128
129
        $_SESSION['flashbag'][$key] = $value;
130
131
        return true;
132
    }
133
134
    /**
135
     * Get flashbag value by key
136
     *
137
     * @param string $key
138
     * @return null|string|array
139
     */
140
    public function getFlashbag(string $key)
0 ignored issues
show
Coding Style introduced by
getFlashbag 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...
141
    {
142
        if (!isset($_SESSION['flashbag'][$key])) {
143
            return null;
144
        }
145
146
        $result = $_SESSION['flashbag'][$key];
147
148
        unset($_SESSION['flashbag'][$key]);
149
150
        return $result;
151
    }
152
153
    /**
154
     * Get flashbag error by key
155
     *
156
     * @param string $key
157
     * @return null|string|array
158
     */
159 View Code Duplication
    public function getFlashbagError(string $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
getFlashbagError 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...
160
    {
161
        if (!isset($_SESSION['flashbag']['errors'][$key])) {
162
            return null;
163
        }
164
165
        $result = $_SESSION['flashbag']['errors'][$key];
166
167
        unset($_SESSION['flashbag']['errors'][$key]);
168
169
        return $result;
170
    }
171
172
    /**
173
     * Set form data in session flashbag
174
     * @param array $formData
175
     */
176
    public function setFlashbagFormData(array $formData)
0 ignored issues
show
Coding Style introduced by
setFlashbagFormData 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...
177
    {
178
        if (is_array($formData)) {
179
            $_SESSION['flashbag']['formData'] = $formData;
180
        }
181
    }
182
183
    /**
184
     * Get form data from session flashbag
185
     * @param string $key
186
     * @return null|array|string
187
     */
188 View Code Duplication
    public function getFlashbagFormData(string $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
getFlashbagFormData 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...
189
    {
190
        if (!isset($_SESSION['flashbag']['formData'][$key])) {
191
            return null;
192
        }
193
194
        $result = $_SESSION['flashbag']['formData'][$key];
195
196
        unset($_SESSION['flashbag']['formData'][$key]);
197
198
        return $result;
199
    }
200
201
}