Completed
Push — 4.x ( f42e95 )
by Akihito
14s queued 11s
created

Segment::resumeOrStartSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
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 20
    public function __construct(Session $session, $name)
48
    {
49 20
        $this->session = $session;
50 20
        $this->name = $name;
51 20
    }
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 10
    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 10
        $this->resumeSession();
67 10
        return isset($_SESSION[$this->name][$key])
68 10
             ? $_SESSION[$this->name][$key]
69 10
             : $alt;
70
    }
71
72
    /**
73
     *
74
     * Returns the entire segment.
75
     *
76
     * @return mixed
77
     *
78
     */
79
    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 11
        $this->resumeSession();
82
        return isset($_SESSION[$this->name])
83 11
            ? $_SESSION[$this->name]
84 11
            : null;
85 11
    }
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 2
     *
95
     */
96 2
    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 1
    {
98 1
        $this->resumeOrStartSession();
99 2
        $_SESSION[$this->name][$key] = $val;
100
    }
101
102
    /**
103
     *
104
     * Clear all data from the segment.
105
     *
106
     * @return null
107
     *
108
     */
109
    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 3
    {
111
        if ($this->resumeSession()) {
112 3
            $_SESSION[$this->name] = array();
113 3
        }
114 3
    }
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
    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
        if ($this->resumeSession()) {
124
            if($key){
125
                if(isset($_SESSION[$this->name]) && array_key_exists($key, $_SESSION[$this->name])){
126
                    unset($_SESSION[$this->name][$key]);
127 3
                }
128
            } else {
129 3
                unset($_SESSION[$this->name]);
130 3
            }
131 3
        }
132 3
    }
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 2
     */
143
    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 2
    {
145 2
        $this->resumeOrStartSession();
146 2
        $_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val;
147 2
    }
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 2
    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 2
        $this->resumeSession();
163 2
        return isset($_SESSION[Session::FLASH_NOW][$this->name][$key])
164 2
             ? $_SESSION[Session::FLASH_NOW][$this->name][$key]
165 2
             : $alt;
166
    }
167
168
    /**
169
     *
170
     * Clears flash values for *only* the next request.
171
     *
172
     * @return null
173
     *
174
     */
175
    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...
176
    {
177 2
        if ($this->resumeSession()) {
178
            $_SESSION[Session::FLASH_NEXT][$this->name] = array();
179 2
        }
180 2
    }
181 2
182 2
    /**
183
     *
184
     * Gets the flash value for a key in the *next* request.
185
     *
186
     * @param string $key The key for the flash value.
187
     *
188
     * @param mixed $alt An alternative value to return if the key is not set.
189
     *
190
     * @return mixed The flash value itself.
191 2
     *
192
     */
193 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...
194 2
    {
195 2
        $this->resumeSession();
196 2
        return isset($_SESSION[Session::FLASH_NEXT][$this->name][$key])
197 2
             ? $_SESSION[Session::FLASH_NEXT][$this->name][$key]
198
             : $alt;
199
    }
200
201
    /**
202
     *
203
     * Sets a flash value for the *next* request *and* the current one.
204
     *
205
     * @param string $key The key for the flash value.
206
     *
207 2
     * @param mixed $val The flash value itself.
208
     *
209 2
     */
210 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...
211 2
    {
212 2
        $this->resumeOrStartSession();
213 2
        $_SESSION[Session::FLASH_NOW][$this->name][$key] = $val;
214 2
        $_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val;
215 2
    }
216
217
    /**
218
     *
219
     * Clears flash values for *both* the next request *and* the current one.
220
     *
221
     * @return null
222
     *
223
     */
224
    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...
225 18
    {
226
        if ($this->resumeSession()) {
227 18
            $_SESSION[Session::FLASH_NOW][$this->name] = array();
228 12
            $_SESSION[Session::FLASH_NEXT][$this->name] = array();
229 12
        }
230
    }
231
232 15
    /**
233
     *
234
     * Retains all the current flash values for the next request; values that
235
     * already exist for the next request take precedence.
236
     *
237
     * @return null
238
     *
239
     */
240
    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...
241
    {
242 15
        if ($this->resumeSession()) {
243
            $_SESSION[Session::FLASH_NEXT][$this->name] = array_merge(
244 15
                $_SESSION[Session::FLASH_NEXT][$this->name],
245 15
                $_SESSION[Session::FLASH_NOW][$this->name]
246 15
            );
247
        }
248 15
    }
249 15
250 15
    /**
251
     *
252 15
     * Loads the segment only if the session has already been started, or if
253 15
     * a session is available (in which case it resumes the session first).
254 15
     *
255 15
     * @return bool
256
     *
257
     */
258
    protected function resumeSession()
259
    {
260
        if ($this->session->isStarted() || $this->session->resume()) {
261
            $this->load();
262
            return true;
263
        }
264 14
265
        return false;
266 14
    }
267 12
268 12
    /**
269 12
     *
270 14
     * Sets the segment properties to $_SESSION references.
271
     *
272
     * @return null
273
     *
274
     */
275
    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...
276
    {
277
        if (! isset($_SESSION[$this->name])) {
278
            $_SESSION[$this->name] = array();
279
        }
280
281
        if (! isset($_SESSION[Session::FLASH_NOW][$this->name])) {
282
            $_SESSION[Session::FLASH_NOW][$this->name] = array();
283
        }
284
285
        if (! isset($_SESSION[Session::FLASH_NEXT][$this->name])) {
286
            $_SESSION[Session::FLASH_NEXT][$this->name] = array();
287
        }
288
    }
289
290
    /**
291
     *
292
     * Resumes a previous session, or starts a new one, and loads the segment.
293
     *
294
     * @return null
295
     *
296
     */
297
    protected function resumeOrStartSession()
298
    {
299
        if (! $this->resumeSession()) {
300
            $this->session->start();
301
            $this->load();
302
        }
303
    }
304
}
305