Failed Conditions
Push — master ( 08b0a5...aa31c4 )
by Arnold
07:22
created

ErrorPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Jasny\Router\Middleware;
4
5
use Jasny\Router;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Route to error page on error
11
 */
12
class ErrorPage
13
{    
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
14
    /**
15
     * Router
16
     * @var Router
17
     */
18
    protected $router = null;
19
20
    /**
21
     * Class constructor
22
     * 
23
     * @param Router $routes
0 ignored issues
show
Bug introduced by
There is no parameter named $routes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
24
     */
25
    public function __construct(Router $router)
26
    {
27
        $this->router = $router;
28
    }
29
30
    /**
31
     * Get router connected to middleware
32
     *
33
     * @return Router
34
     */
35
    public function getRouter()
36
    {
37
        return $this->router;
38
    }
39
40
    /**
41
     * Run middleware action
42
     *
43
     * @param ServerRequestInterface $request
44
     * @param ResponseInterface      $response
45
     * @param callback               $next
46
     * @return ResponseInterface
47
     */
48
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null)
49
    {
50
        if ($next && !is_callable($next)) {
51
            throw new \InvalidArgumentException("'next' should be a callback");            
52
        }
53
54
        $response = $next ? call_user_func($next, $request, $response) : $response;    
55
        $status = $response->getStatusCode();
56
57
        if (!$this->isErrorStatus($status)) return $response;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
58
59
        $uri = $request->getUri()->withPath("/$status");
60
        $request = $request->withUri($uri, true);
61
62
        return $this->getRouter()->run($request, $response);
63
    }
64
65
    /**
66
     * Detect if response has error status code
67
     *
68
     * @param int $status
69
     * @return boolean
70
     */
71
    protected function isErrorStatus($status)
72
    {
73
        return $status >= 400 && $status < 600;
74
    }
75
}
76