Completed
Push — master ( 10e991...2799b6 )
by Matthias
05:16
created

WebRequest::clearSessionData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
/*
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2015 zepi
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 *
25
 */
26
27
/**
28
 * The WebRequest representates a web framework request.
29
 * 
30
 * @package Zepi\Turbo\Request
31
 * @author Matthias Zobrist <[email protected]>
32
 * @copyright Copyright (c) 2015 zepi
33
 */
34
35
namespace Zepi\Turbo\Request;
36
37
use \Zepi\Turbo\FrameworkInterface\SessionInterface;
38
39
/**
40
 * The WebRequest representates a web framework request.
41
 * 
42
 * @author Matthias Zobrist <[email protected]>
43
 * @copyright Copyright (c) 2015 zepi
44
 */
45
class WebRequest extends RequestAbstract
46
{
47
    /**
48
     * @access protected
49
     * @var boolean
50
     */
51
    protected $_isSsl = false;
52
    
53
    /**
54
     * @access protected
55
     * @var \Zepi\Turbo\FrameworkInterface\SessionInterface
56
     */
57
    protected $_session = null;
58
    
59
    /**
60
     * Constructs the object
61
     * 
62
     * @access public
63
     * @param string $route
64
     * @param array params
65
     * @param string $base
66
     * @param string $locale
67
     * @param boolean $isSsl
68
     * @param array $data
69
     */
70
    public function __construct($route, $params, $base, $locale, $isSsl, $data = array())
71
    {
72
        parent::__construct($route, $params, $base, $locale, $data);
73
        
74
        $this->_isSsl = $isSsl;
75
    }
76
    
77
    /**
78
     * Returns the delimitier, which is used to split the route
79
     * into parts.
80
     * The delimiter for the html request is the slash (/).
81
     * 
82
     * @access public
83
     * @return string
84
     */
85
    public function getRouteDelimiter()
86
    {
87
        return '/';
88
    }
89
    
90
    /**
91
     * Saves the given value for the given key in the session data
92
     * 
93
     * @access public
94
     * @param string $key
95
     * @param mixed $value
96
     */
97
    public function setSessionData($key, $value)
1 ignored issue
show
Coding Style introduced by
setSessionData 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...
98
    {
99
        $_SESSION[$key] = $value;
100
    }
101
    
102
    /**
103
     * Returns the session value of the given key.
104
     * 
105
     * @access public
106
     * @param string $key
107
     * @return mixed
108
     */
109
    public function getSessionData($key = '')
1 ignored issue
show
Coding Style introduced by
getSessionData 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
        if (!isset($_SESSION[$key])) {
112
            return false;
113
        }
114
        
115
        return $_SESSION[$key];
116
    }
117
    
118
    /**
119
     * Deletes the value for the given key
120
     * 
121
     * @access public
122
     * @param string $key
123
     * @return boolean
124
     */
125
    public function deleteSessionData($key)
1 ignored issue
show
Coding Style introduced by
deleteSessionData 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...
126
    {
127
        if (!isset($_SESSION[$key])) {
128
            return false;
129
        }
130
        
131
        unset($_SESSION[$key]);
132
        return true;
133
    }
134
    
135
    /**
136
     * Removes all session data
137
     * 
138
     * @access public
139
     */
140
    public function clearSessionData()
1 ignored issue
show
Coding Style introduced by
clearSessionData 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
        foreach ($_SESSION as $key => $value) {
143
            unset($_SESSION[$key]);
144
        }
145
    }
146
    
147
    /**
148
     * Returns the cookie value of the given key.
149
     * 
150
     * @access public
151
     * @param string $key
152
     * @return mixed
153
     */
154
    public function getCookieData($key = '')
1 ignored issue
show
Coding Style introduced by
getCookieData uses the super-global variable $_COOKIE 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...
155
    {
156
        if (!isset($_COOKIE[$key])) {
157
            return false;
158
        }
159
        
160
        return $_COOKIE[$key];
161
    }
162
    
163
    /**
164
     * Returns true if this request was made over a 
165
     * secure connection trough ssl.
166
     * 
167
     * @access public
168
     * @return boolean
169
     */
170
    public function isSsl()
171
    {
172
        return ($this->_isSsl);
173
    }
174
    
175
    /**
176
     * Adds a session object to the request
177
     * 
178
     * @access public
179
     * @param \Zepi\Turbo\FrameworkInterface\SessionInterface $session
180
     * @return boolean
181
     */
182
    public function setSession(SessionInterface $session)
183
    {
184
        if (!is_object($session) || $this->_session !== null) {
185
            return false;
186
        }
187
        
188
        $this->_session = $session;
189
        
190
        return true;
191
    }
192
    
193
    /**
194
     * Returns true if a session for the given name
195
     * exists. Otherwise returns false.
196
     * 
197
     * @access public
198
     * @return boolean
199
     */
200
    public function hasSession()
201
    {
202
        if ($this->_session === null) {
203
            return false;
204
        }
205
        
206
        return true;
207
    }
208
    
209
    /**
210
     * Returns the session
211
     * 
212
     * @access public
213
     * @return false|\Zepi\Turbo\FrameworkInterface\SessionInterface
214
     */
215
    public function getSession()
216
    {
217
        if ($this->_session === null) {
218
            return false;
219
        }
220
        
221
        return $this->_session;
222
    }
223
    
224
    /**
225
     * Removes the session
226
     * 
227
     * @access public
228
     */
229
    public function removeSession()
230
    {
231
        $this->_session = null;
232
        $this->clearSessionData();
233
    }
234
}
235