Redirect   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
eloc 24
dl 0
loc 41
c 0
b 0
f 0
ccs 0
cts 29
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B compose() 0 39 7
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 ) {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
44
45
             $output = empty($message) ?
46
                 "Please follow <a href=\"$location\">this link</a>"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $location instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
47
                 : $message;
48
49
             $status->set(200);
50
             $headers->set("Refresh", "0;url=$location");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Refresh does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $location instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
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