Completed
Pull Request — 3.x (#52)
by jake
02:15
created

Segment::getAllNextFlash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Session;
10
11
/**
12
 *
13
 * A session segment; lazy-loads from the session.
14
 *
15
 * @package Aura.Session
16
 *
17
 */
18
class Segment implements SegmentInterface
19
{
20
    /**
21
     *
22
     * The session manager.
23
     *
24
     * @var Session
25
     *
26
     */
27
    protected $session;
28
29
    /**
30
     *
31
     * The segment name.
32
     *
33
     * @var string
34
     *
35
     */
36
    protected $name;
37
38
    /**
39
     *
40
     * Constructor.
41
     *
42
     * @param Session $session The session manager.
43
     *
44
     * @param string $name The segment name.
45
     *
46
     */
47 22
    public function __construct(Session $session, $name)
48
    {
49 22
        $this->session = $session;
50 22
        $this->name = $name;
51 22
    }
52
53
    /**
54
     *
55
     * Returns the value of a key in the segment.
56
     *
57
     * @param string $key The key in the segment.
58
     *
59
     * @param mixed $alt An alternative value to return if the key is not set.
60
     *
61
     * @return mixed
62
     *
63
     */
64 11
    public function get($key, $alt = null)
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...
65
    {
66 11
        $this->resumeSession();
67 11
        return isset($_SESSION[$this->name][$key])
68 11
             ? $_SESSION[$this->name][$key]
69 11
             : $alt;
70
    }
71
72
    /**
73
     *
74
     * Returns the entire segment.
75
     *
76
     * @return mixed
77
     *
78
     */
79 1
    public function getSegment()
0 ignored issues
show
Coding Style introduced by
getSegment 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...
80
    {
81 1
        $this->resumeSession();
82 1
        return isset($_SESSION[$this->name])
83 1
            ? $_SESSION[$this->name]
84 1
            : null;
85
    }
86
87
    /**
88
     *
89
     * Sets the value of a key in the segment.
90
     *
91
     * @param string $key The key to set.
92
     *
93
     * @param mixed $val The value to set it to.
94
     *
95
     */
96 14
    public function set($key, $val)
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...
97
    {
98 14
        $this->resumeOrStartSession();
99 14
        $_SESSION[$this->name][$key] = $val;
100 14
    }
101
102
    /**
103
     *
104
     * Clear all data from the segment.
105
     *
106
     * @return null
107
     *
108
     */
109 2
    public function clear()
0 ignored issues
show
Coding Style introduced by
clear 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...
110
    {
111 2
        if ($this->resumeSession()) {
112 1
            $_SESSION[$this->name] = array();
113 1
        }
114 2
    }
115
116
117
    /**
118
     * Remove a key from the segment, or remove the entire segment (including key) from the session
119
     *
120
     * @param null $key
121
     */
122 2
    public function remove($key = null) {
0 ignored issues
show
Coding Style introduced by
remove 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...
123 2
        if ($this->resumeSession()) {
124 2
            if($key){
125 1
                if(isset($_SESSION[$this->name]) && array_key_exists($key, $_SESSION[$this->name])){
126 1
                    unset($_SESSION[$this->name][$key]);
127 1
                }
128 1
            } else {
129 1
                unset($_SESSION[$this->name]);
130
            }
131 2
        }
132 2
    }
133
134
    /**
135
     *
136
     * Sets a flash value for the *next* request.
137
     *
138
     * @param string $key The key for the flash value.
139
     *
140
     * @param mixed $val The flash value itself.
141
     *
142
     */
143 3
    public function setFlash($key, $val)
0 ignored issues
show
Coding Style introduced by
setFlash 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...
144
    {
145 3
        $this->resumeOrStartSession();
146 3
        $_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val;
147 3
    }
148
149
    /**
150
     *
151
     * Gets the flash value for a key in the *current* request.
152
     *
153
     * @param string $key The key for the flash value.
154
     *
155
     * @param mixed $alt An alternative value to return if the key is not set.
156
     *
157
     * @return mixed The flash value itself.
158
     *
159
     */
160 3
    public function getFlash($key, $alt = null)
0 ignored issues
show
Coding Style introduced by
getFlash 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...
161
    {
162 3
        $this->resumeSession();
163 3
        return isset($_SESSION[Session::FLASH_NOW][$this->name][$key])
164 3
             ? $_SESSION[Session::FLASH_NOW][$this->name][$key]
165 3
             : $alt;
166
    }
167
168
    /**
169
     *
170
     * Gets all the flash values in the *current* request.
171
     *
172
     * @param mixed $alt An alternative value to return if no flash values are set.
173
     *
174
     * @return mixed The flash values themselves.
175
     *
176
     */
177 1
    public function getAllCurrentFlash($alt = array())
0 ignored issues
show
Coding Style introduced by
getAllCurrentFlash 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...
178
    {
179 1
        $this->resumeSession();
180 1
        return isset($_SESSION[Session::FLASH_NOW][$this->name])
181 1
             ? $_SESSION[Session::FLASH_NOW][$this->name]
182 1
             : $alt;
183
    }
184
185
    /**
186
     *
187
     * Clears flash values for *only* the next request.
188
     *
189
     * @return null
190
     *
191
     */
192 2
    public function clearFlash()
0 ignored issues
show
Coding Style introduced by
clearFlash 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...
193
    {
194 2
        if ($this->resumeSession()) {
195 2
            $_SESSION[Session::FLASH_NEXT][$this->name] = array();
196 2
        }
197 2
    }
198
199
    /**
200
     *
201
     * Gets the flash value for a key in the *next* request.
202
     *
203
     * @param string $key The key for the flash value.
204
     *
205
     * @param mixed $alt An alternative value to return if the key is not set.
206
     *
207
     * @return mixed The flash value itself.
208
     *
209
     */
210 2
    public function getFlashNext($key, $alt = null)
0 ignored issues
show
Coding Style introduced by
getFlashNext 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...
211
    {
212 2
        $this->resumeSession();
213 2
        return isset($_SESSION[Session::FLASH_NEXT][$this->name][$key])
214 2
             ? $_SESSION[Session::FLASH_NEXT][$this->name][$key]
215 2
             : $alt;
216
    }
217
218
    /**
219
     *
220
     * Gets all flash values for the *next* request.
221
     *
222
     * @param mixed $alt An alternative value to return if no flash values set.
223
     *
224
     * @return mixed The flash values themselves.
225
     *
226
     */
227 1
    public function getAllNextFlash($alt = array())
0 ignored issues
show
Coding Style introduced by
getAllNextFlash 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...
228
    {
229 1
        $this->resumeSession();
230 1
        return isset($_SESSION[Session::FLASH_NEXT][$this->name])
231 1
             ? $_SESSION[Session::FLASH_NEXT][$this->name]
232 1
             : $alt;
233
    }
234
235
    /**
236
     *
237
     * Sets a flash value for the *next* request *and* the current one.
238
     *
239
     * @param string $key The key for the flash value.
240
     *
241
     * @param mixed $val The flash value itself.
242
     *
243
     */
244 2
    public function setFlashNow($key, $val)
0 ignored issues
show
Coding Style introduced by
setFlashNow 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...
245
    {
246 2
        $this->resumeOrStartSession();
247 2
        $_SESSION[Session::FLASH_NOW][$this->name][$key] = $val;
248 2
        $_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val;
249 2
    }
250
251
    /**
252
     *
253
     * Clears flash values for *both* the next request *and* the current one.
254
     *
255
     * @return null
256
     *
257
     */
258 2
    public function clearFlashNow()
0 ignored issues
show
Coding Style introduced by
clearFlashNow 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...
259
    {
260 2
        if ($this->resumeSession()) {
261 2
            $_SESSION[Session::FLASH_NOW][$this->name] = array();
262 2
            $_SESSION[Session::FLASH_NEXT][$this->name] = array();
263 2
        }
264 2
    }
265
266
    /**
267
     *
268
     * Retains all the current flash values for the next request; values that
269
     * already exist for the next request take precedence.
270
     *
271
     * @return null
272
     *
273
     */
274 2
    public function keepFlash()
0 ignored issues
show
Coding Style introduced by
keepFlash 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...
275
    {
276 2
        if ($this->resumeSession()) {
277 2
            $_SESSION[Session::FLASH_NEXT][$this->name] = array_merge(
278 2
                $_SESSION[Session::FLASH_NEXT][$this->name],
279 2
                $_SESSION[Session::FLASH_NOW][$this->name]
280 2
            );
281 2
        }
282 2
    }
283
284
    /**
285
     *
286
     * Loads the segment only if the session has already been started, or if
287
     * a session is available (in which case it resumes the session first).
288
     *
289
     * @return bool
290
     *
291
     */
292 21
    protected function resumeSession()
293
    {
294 21
        if ($this->session->isStarted() || $this->session->resume()) {
295 15
            $this->load();
296 15
            return true;
297
        }
298
299 18
        return false;
300
    }
301
302
    /**
303
     *
304
     * Sets the segment properties to $_SESSION references.
305
     *
306
     * @return null
307
     *
308
     */
309 18
    protected function load()
0 ignored issues
show
Coding Style introduced by
load 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...
310
    {
311 18
        if (! isset($_SESSION[$this->name])) {
312 18
            $_SESSION[$this->name] = array();
313 18
        }
314
315 18
        if (! isset($_SESSION[Session::FLASH_NOW][$this->name])) {
316 18
            $_SESSION[Session::FLASH_NOW][$this->name] = array();
317 18
        }
318
319 18
        if (! isset($_SESSION[Session::FLASH_NEXT][$this->name])) {
320 18
            $_SESSION[Session::FLASH_NEXT][$this->name] = array();
321 18
        }
322 18
    }
323
324
    /**
325
     *
326
     * Resumes a previous session, or starts a new one, and loads the segment.
327
     *
328
     * @return null
329
     *
330
     */
331 17
    protected function resumeOrStartSession()
332
    {
333 17
        if (! $this->resumeSession()) {
334 15
            $this->session->start();
335 15
            $this->load();
336 15
        }
337 17
    }
338
}
339