Failed Conditions
Push — master ( 492f30...c3b3a8 )
by Arnold
09:25
created

Router::notFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Jasny;
4
5
use Jasny\Router\Runner;
6
use Jasny\Router\Routes\Glob;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Route pretty URLs to correct controller
12
 */
13
class Router
14
{
15
    /**
16
     * Specific routes
17
     * @var array
18
     */
19
    protected $routes = [];    
20
21
    /**
22
     * Middlewares actions
23
     * @var array
24
     **/
25
    protected $middlewares = [];
26
    
27
    /**
28
     * Class constructor
29
     * 
30
     * @param array $routes
31
     */
32
    public function __construct(array $routes)
33
    {
34
        $this->routes = $routes;
35
    }
36
    
37
    /**
38
     * Get a list of all routes
39
     * 
40
     * @return object
41
     */
42
    public function getRoutes()
43
    {
44
        return $this->routes;
45
    }    
46
47
    /**
48
     * Get middlewares
49
     *
50
     * @return array
51
     */
52
    public function getMiddlewares()
53
    {
54
        return $this->middlewares;
55
    }
56
57
    /**
58
     * Add middleware call to router
59
     *
60
     * @param callback $middleware
61
     * @return Router $this
62
     */
63
    public function add($middleware)
64
    {
65
        if (!is_callable($middleware)) {
66
            throw new \InvalidArgumentException("Middleware should be a callable");
67
        }
68
69
        $this->middlewares[] = $middleware;
70
71
        return $this;
72
    }
73
    
74
    /**
75
     * Run the action for the request
76
     *
77
     * @param ServerRequestInterface $request
78
     * @param ResponseInterface      $response
79
     * @return ResponseInterface
80
     */
81
    final public function run(ServerRequestInterface $request, ResponseInterface $response)
82
    {
83
        return $this->__invoke($request, $response);
84
    }
85
86
    /**
87
     * Run the action for the request (optionally as middleware), previously running middlewares, if any
88
     *
89
     * @param ServerRequestInterface $request
90
     * @param ResponseInterface      $response
91
     * @param callback               $next
92
     * @return ResponseInterface
93
     */
94
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null)
95
    {
96
        $handle = [$this, 'handle'];
97
98
        #Call to $this->handle will be executed last in the chain of middlewares
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
99
        $next = function(ServerRequestInterface $request, ResponseInterface $response) use ($next, $handle) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
100
            return call_user_func($handle, $request, $response, $next);
101
        };
102
103
        #Build middlewares call chain, so that the last added was executed in first place
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
104
        foreach ($this->middlewares as $middleware) {
105
            $next = function(ServerRequestInterface $request, ResponseInterface $response) use ($next, $middleware) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
106
                return $middleware($request, $response, $next);
107
            };
108
        }
109
110
        return $next($request, $response);
111
    }
112
113
    /**
114
     * Run the action
115
     *
116
     * @param ServerRequestInterface $request
117
     * @param ResponseInterface      $response
118
     * @param callback      $next
119
     * @return ResponseInterface
120
     */
121
    protected function handle(ServerRequestInterface $request, ResponseInterface $response, $next = null)
122
    {
123
        $glob = new Glob($this->routes);
124
        $route = $glob->getRoute($request);
125
        
126
        if (!$route) return $this->notFound($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...
127
128
        $runner = Runner::create($route);
0 ignored issues
show
Documentation introduced by
$route is of type array|string, but the function expects a object<Jasny\Router\Route>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
130
        return $runner($request, $response, $next);
131
    }
132
133
    /**
134
     * Return 'Not Found' response
135
     *
136
     * @param ResponseInterface      $response
137
     * @return ResponseInterface 
138
     */
139
    protected function notFound(ResponseInterface $response)
140
    {
141
        $message = 'Not Found';            
142
143
        $body = $response->getBody();        
144
        $body->rewind();
145
        $body->write($message);
146
147
        return $response->withStatus(404, $message)->withBody($body);
148
    }
149
}
150
151