Completed
Push — master ( 8df946...5e7000 )
by Marco
05:17
created

Redirect::compose()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 29
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 23
nc 10
nop 3
crap 56
1
<?php namespace Comodojo\Dispatcher\Output;
2
3
use \Comodojo\Dispatcher\Request\Model as Request;
4
use \Comodojo\Dispatcher\Response\Model as Response;
5
use \Comodojo\Dispatcher\Router\Route;
6
7
/**
8
 * @package     Comodojo Dispatcher
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @author      Marco Castiello <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
 class Redirect {
25
26
     public static function compose(
27
         Request $request,
28
         Response $response,
29
         Route $route
30
     ) {
31
32
         $status = $response->getStatus();
33
         $content = $response->getContent();
34
         $headers = $response->getHeaders();
35
36
         $code = $route->getRedirectCode();
37
38
         $location = $route->getRedirectLocation();
39
         $uri = empty($location) ? (string) $request->getUri() : $location;
40
41
         $message = $route->getRedirectMessage();
42
43
         if ( $route->getRedirectType() == Route::REDIRECT_REFRESH ) {
44
45
             $output = empty($message) ?
46
                 "Please follow <a href=\"$location\">this link</a>"
47
                 : $message;
48
49
             $status->set(200);
50
             $headers->set("Refresh", "0;url=$location");
51
             $content->set($output);
52
53
         } else {
54
55
             if ( !empty($code) ) {
56
                 $status->set($code);
57
             } else if ( $request->getVersion() === 'HTTP/1.1' ) {
0 ignored issues
show
introduced by
The condition $request->getVersion() === 'HTTP/1.1' is always false.
Loading history...
58
                 $status->set( (string) $request->getMethod() !== 'GET' ? 303 : 307);
59
             } else {
60
                 $status->set(302);
61
             }
62
63
             $content->set($message);
64
             $response->getLocation()->set($uri);
65
66
         }
67
68
     }
69
70
 }
71