FastRoute   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 20 4
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use FastRoute\Dispatcher;
9
10
class FastRoute
11
{
12
    use Utils\CallableTrait;
13
14
    /**
15
     * @var Dispatcher FastRoute dispatcher
16
     */
17
    private $router;
18
19
    /**
20
     * Set the Dispatcher instance.
21
     *
22
     * @param Dispatcher|null $router
23
     */
24
    public function __construct(Dispatcher $router)
25
    {
26
        $this->router = $router;
27
    }
28
29
    /**
30
     * Execute the middleware.
31
     *
32
     * @param ServerRequestInterface $request
33
     * @param ResponseInterface      $response
34
     * @param callable               $next
35
     *
36
     * @return ResponseInterface
37
     */
38
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
39
    {
40
        $route = $this->router->dispatch($request->getMethod(), $request->getUri()->getPath());
41
42
        if ($route[0] === Dispatcher::NOT_FOUND) {
43
            return $response->withStatus(404);
44
        }
45
46
        if ($route[0] === Dispatcher::METHOD_NOT_ALLOWED) {
47
            return $response->withStatus(405);
48
        }
49
50
        foreach ($route[2] as $name => $value) {
51
            $request = $request->withAttribute($name, $value);
52
        }
53
54
        $response = $this->executeCallable($route[1], $request, $response);
55
56
        return $next($request, $response);
57
    }
58
}
59