Completed
Push — master ( 595398...2d23b0 )
by Matthias
07:08
created

Response::hasData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 Response holds all information which are outputtet at the end
29
 * of the request.
30
 * 
31
 * @package Zepi\Turbo\Response
32
 * @author Matthias Zobrist <[email protected]>
33
 * @copyright Copyright (c) 2015 zepi
34
 */
35
36
namespace Zepi\Turbo\Response;
37
38
use Zepi\Turbo\Request\RequestAbstract;
39
40
/**
41
 * The Response holds all information which are outputtet at the end
42
 * of the request.
43
 * 
44
 * @author Matthias Zobrist <[email protected]>
45
 * @copyright Copyright (c) 2015 zepi
46
 */
47
class Response
48
{
49
    /**
50
     * @access protected
51
     * @var RequestAbstract
52
     */
53
    protected $_request;
54
    
55
    /**
56
     * @access protected
57
     * @var array
58
     */
59
    protected $_data = array();
60
    
61
    /**
62
     * @access protected
63
     * @var array
64
     */
65
    protected $_outputParts = array();
66
    
67
    /**
68
     * @access protected
69
     * @var string
70
     */
71
    protected $_output;
72
    
73
    /**
74
     * Constructs the object
75
     * 
76
     * @access public
77
     * @param \Zepi\Turbo\Request\RequestAbstract $request
78
     */
79
    public function __construct(RequestAbstract $request)
80
    {
81
        $this->_request = $request;
82
    }
83
    
84
    /**
85
     * Return the data for the given key. If the key does 
86
     * not exists the function will return false.
87
     * 
88
     * @access public
89
     * @param string $key
90
     * @return mixed
91
     */
92
    public function getData($key)
93
    {
94
        if (!$this->hasData($key)) {
95
            return false;
96
        }
97
        
98
        return $this->_data[$key];
99
    }
100
    
101
    /**
102
     * Returns true if the given key is set.
103
     * 
104
     * @access public
105
     * @param string $key
106
     * @return boolean
107
     */
108
    public function hasData($key)
109
    {
110
        return (isset($this->_data[$key]));
111
    }
112
    
113
    /**
114
     * Saves the value for the given key in the response object.
115
     * 
116
     * @access public
117
     * @param string $key
118
     * @param mixed $value
119
     */
120
    public function setData($key, $value)
121
    {
122
        $this->_data[$key] = $value;
123
    }
124
    
125
    /**
126
     * Returns the output for the given key. If the key does
127
     * not exists the function will return false.
128
     * 
129
     * @access public
130
     * @param string $key
131
     * @return false|string
132
     */
133
    public function getOutputPart($key)
134
    {
135
        if (!$this->hasOutputPart($key)) {
136
            return false;
137
        }
138
        
139
        return $this->_outputParts[$key];
140
    }
141
    
142
    /**
143
     * Returns true if the given key exists as output key.
144
     * 
145
     * @access public
146
     * @param string $key
147
     * @return boolean
148
     */
149
    public function hasOutputPart($key)
150
    {
151
        return (isset($this->_outputParts[$key]));
152
    }
153
    
154
    /**
155
     * Saves the output for the given key in the Response object.
156
     * 
157
     * @access public
158
     * @param string $key
159
     * @param string $output
160
     */
161
    public function setOutputPart($key, $output)
162
    {
163
        $this->_outputParts[$key] = $output;
164
    }
165
    
166
    /**
167
     * Returns all output parts of the Response object.
168
     * 
169
     * @access public
170
     * @return array
171
     */
172
    public function getOutputParts()
173
    {
174
        return $this->_outputParts;
175
    }
176
    
177
    /**
178
     * Returns the output of the response.
179
     * 
180
     * @access public
181
     * @return string
182
     */
183
    public function getOutput()
184
    {
185
        return $this->_output;
186
    }
187
    
188
    /**
189
     * Returns true if the response has an output.
190
     * 
191
     * @access public
192
     * @return boolean
193
     */
194
    public function hasOutput()
195
    {
196
        return ($this->_output != '');
197
    }
198
    
199
    /**
200
     * Sets the output of the response.
201
     * 
202
     * @access public
203
     * @param string $output
204
     */
205
    public function setOutput($output)
206
    {
207
        $this->_output = $output;
208
    }
209
    
210
    /**
211
     * Set the Location header to redirect a request
212
     * 
213
     * @access public
214
     * @param string $target
215
     * @param integer $headerCode
216
     * @param boolean $withOrigin
217
     */
218
    public function redirectTo($target, $headerCode = 301, $withOrigin = false)
219
    {
220
        if (strpos($target, 'http://') === false) {
221
            $target = $this->_request->getFullRoute($target);
222
        }
223
        
224
        if ($withOrigin) {
225
            $origin = $this->_request->getFullRoute();
226
            $additionalQuery = '_origin=' . base64_encode($origin);
227
            
228
            $parts = parse_url($target);
229
            
230
            if (!isset($parts['query'])) {
231
                $parts['query'] = '';
232
            } else if ($parts['query'] !== '') {
233
                $parts['query'] .= '&';
234
            }
235
            
236
            $parts['query'] .= $additionalQuery;
237
            $target = $this->buildUrl($parts);
0 ignored issues
show
Security Bug introduced by
It seems like $parts defined by parse_url($target) on line 228 can also be of type false; however, Zepi\Turbo\Response\Response::buildUrl() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
238
        }
239
        
240
        header("Location: " . $target, true, $headerCode);
241
    }
242
    
243
    /**
244
     * Returns a full url for the given url parts array
245
     * from the function `parse_url()`.
246
     * 
247
     * @access public
248
     * @param array $urlParts
249
     * @return string
250
     */
251
    public function buildUrl($urlParts)
252
    {
253
        $url = '';
254
        $auth = false;
255
        
256
        if (isset($urlParts['scheme'])) {
257
            $url .= $urlParts['scheme'] . '://';
258
        }
259
        
260
        if (isset($urlParts['user'])) {
261
            $url .= $urlParts['user'];
262
            $auth = true;
263
        }
264
        
265
        if (isset($urlParts['pass'])) {
266
            $url .= ':' . $urlParts['pass'];
267
            $auth = true;
268
        }
269
        
270
        if ($auth) {
271
            $url .= '@';
272
        }
273
        
274
        if (isset($urlParts['host'])) {
275
            $url .= $urlParts['host'];
276
        }
277
        
278
        if (isset($urlParts['port'])) {
279
            $url .= ':' . $urlParts['port'];
280
        }
281
        
282
        if (isset($urlParts['path'])) {
283
            $url .= $urlParts['path'];
284
        }
285
        
286
        if (isset($urlParts['query'])) {
287
            $url .= '?' . $urlParts['query'];
288
        }
289
        
290
        if (isset($urlParts['fragment'])) {
291
            $url .= '#' . $urlParts['fragment'];
292
        }
293
        
294
        return $url;
295
    }
296
}
297