Passed
Push — master ( 864eb7...cda898 )
by Matthias
02:34
created

Response::buildUrlWithOrigin()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
231
        }
232
        
233 2
        header("Location: " . $target, true, $headerCode);
234 2
    }
235
    
236
    /**
237
     * Sends a header
238
     * 
239
     * @access public
240
     * @param string $message
241
     * @param integer $code
242
     */
243
    public function sendHeader($message, $code = null)
244
    {
245
        if (!($this->request instanceof WebRequest)) {
246
            return;
247
        }
248
        
249
        if ($code !== null) {
250
            header($message, true, $code);
251
        } else {
252
            header($message);
253
        }
254
    }
255
    
256
    /**
257
     * Sends the status code for the give code
258
     * 
259
     * @access public
260
     * @param integer $code
261
     * @param boolean $resetOutput
262
     */
263
    public function sendHttpStatus($code, $resetOutput = false)
264
    {
265
        if (!($this->request instanceof WebRequest)) {
266
            return;
267
        }
268
        
269
        $codes = array(
270
            100 => 'Continue',
271
            101 => 'Switching Protocols',
272
            102 => 'Processing',
273
            200 => 'OK',
274
            201 => 'Created',
275
            202 => 'Accepted',
276
            203 => 'Non-Authoritative Information',
277
            204 => 'No Content',
278
            205 => 'Reset Content',
279
            206 => 'Partial Content',
280
            207 => 'Multi-Status',
281
            300 => 'Multiple Choices',
282
            301 => 'Moved Permanently',
283
            302 => 'Found',
284
            303 => 'See Other',
285
            304 => 'Not Modified',
286
            305 => 'Use Proxy',
287
            306 => 'Switch Proxy',
288
            307 => 'Temporary Redirect',
289
            400 => 'Bad Request',
290
            401 => 'Unauthorized',
291
            402 => 'Payment Required',
292
            403 => 'Forbidden',
293
            404 => 'Not Found',
294
            405 => 'Method Not Allowed',
295
            406 => 'Not Acceptable',
296
            407 => 'Proxy Authentication Required',
297
            408 => 'Request Timeout',
298
            409 => 'Conflict',
299
            410 => 'Gone',
300
            411 => 'Length Required',
301
            412 => 'Precondition Failed',
302
            413 => 'Request Entity Too Large',
303
            414 => 'Request-URI Too Long',
304
            415 => 'Unsupported Media Type',
305
            416 => 'Requested Range Not Satisfiable',
306
            417 => 'Expectation Failed',
307
            418 => 'I\'m a teapot',
308
            422 => 'Unprocessable Entity',
309
            423 => 'Locked',
310
            424 => 'Failed Dependency',
311
            425 => 'Unordered Collection',
312
            426 => 'Upgrade Required',
313
            449 => 'Retry With',
314
            450 => 'Blocked by Windows Parental Controls',
315
            500 => 'Internal Server Error',
316
            501 => 'Not Implemented',
317
            502 => 'Bad Gateway',
318
            503 => 'Service Unavailable',
319
            504 => 'Gateway Timeout',
320
            505 => 'HTTP Version Not Supported',
321
            506 => 'Variant Also Negotiates',
322
            507 => 'Insufficient Storage',
323
            509 => 'Bandwidth Limit Exceeded',
324
            510 => 'Not Extended'
325
        );
326
        
327
        if (!isset($codes[$code])) {
328
            return false;
329
        }
330
        
331
        $message = $this->request->getProtocol() . ' ' . $code . ' ' . $codes[$code];
332
        header($message, true, $code);
333
        
334
        if ($resetOutput) {
335
            $this->setOutput($message);
336
        }
337
    }
338
    
339
    /**
340
     * Returns a full url for the given url parts array
341
     * from the function `parse_url()`.
342
     * 
343
     * @access public
344
     * @param array $urlParts
345
     * @return string
346
     */
347
    public function buildUrl($urlParts)
348
    {
349
        return http_build_url($urlParts);
350
    }
351
    
352
    /**
353
     * Adds the origin and returns the given target url
354
     * with the origin query parameter.
355
     * 
356
     * @param string $target
357
     * @return void|string
358
     */
359
    protected function buildUrlWithOrigin($target)
360
    {
361
        $origin = $this->request->getFullRoute();
362
        $additionalQuery = '_origin=' . base64_encode($origin);
363
        
364
        $parts = parse_url($target);
365
        
366
        if ($parts === false) {
367
            return $target;
368
        }
369
        
370
        if (!isset($parts['query'])) {
371
            $parts['query'] = '';
372
        } else if ($parts['query'] !== '') {
373
            $parts['query'] .= '&';
374
        }
375
        
376
        $parts['query'] .= $additionalQuery;
377
        return $this->buildUrl($parts);
378
    }
379
}
380