Completed
Push — master ( c0f250...321e24 )
by Filipe
15:10
created

RouterMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 12 2
A handleFailedRoute() 0 17 3
1
<?php
2
3
/**
4
 * This file is part of slick/web_stack package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\WebStack\Http\Server;
11
12
use Aura\Router\Route;
13
use Aura\Router\RouterContainer;
14
use Interop\Http\Server\MiddlewareInterface;
15
use Interop\Http\Server\RequestHandlerInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Slick\Http\Message\Response;
19
20
/**
21
 * RouterMiddleware
22
 *
23
 * @package Slick\WebStack\Http\Server
24
 */
25
class RouterMiddleware implements MiddlewareInterface
26
{
27
    /**
28
     * @var RouterContainer
29
     */
30
    private $routerContainer;
31
32
    /**
33
     * Creates a Router Middleware
34
     *
35
     * @param RouterContainer $routerContainer
36
     */
37
    public function __construct(RouterContainer $routerContainer)
38
    {
39
        $this->routerContainer = $routerContainer;
40
    }
41
42
    /**
43
     * Process an incoming server request and return a response, optionally delegating
44
     * response creation to a handler.
45
     *
46
     * @param ServerRequestInterface $request
47
     * @param RequestHandlerInterface $handler
48
     *
49
     * @return ResponseInterface
50
     */
51
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
52
    {
53
        $matcher = $this->routerContainer->getMatcher();
54
        $route = $matcher->match($request);
55
56
        if (!$route instanceof Route) {
57
            return $this->handleFailedRoute($matcher->getFailedRoute());
0 ignored issues
show
Bug introduced by
It seems like $matcher->getFailedRoute() can be null; however, handleFailedRoute() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
        }
59
60
        $request = $request->withAttribute('route', $route);
61
        return $handler->handle($request);
62
    }
63
64
    private function handleFailedRoute(Route $failedRoute)
65
    {
66
        switch ($failedRoute->failedRule) {
67
            case 'Aura\Router\Rule\Allows':
68
                $response = (new Response(405))
69
                    ->withHeader('Allow', $failedRoute->allows);
70
                break;
71
72
            case 'Aura\Router\Rule\Accepts':
73
                $response = new Response(406);
74
                break;
75
76
            default:
77
                $response = new Response(404);
78
        }
79
        return $response;
80
    }
81
}
82