Completed
Push — master ( 448626...67d491 )
by Derek Stephen
01:11
created

RouteFirewall::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php declare(strict_types=1);
2
3
namespace Bone\Firewall;
4
5
use Barnacle\Container;
6
use Bone\Router\Router;
7
use League\Route\Route;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
13
class RouteFirewall implements MiddlewareInterface
14
{
15
    /** @var Router $router */
16
    private $router;
17
18
    /** @var array $blockedRoutes */
19
    private $blockedRoutes;
20
21
    /** @var MiddlewareInterface[] $middlewares */
22
    private $middlewares;
23
24
    /** @var Container $container */
25
    private $container;
26
27
    /**
28
     * RouteFirewall constructor.
29
     * @param Container $container
0 ignored issues
show
Bug introduced by
There is no parameter named $container. 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...
30
     */
31 4
    public function __construct(Container $c)
32
    {
33
        /** @var Router $router */
34 4
        $router = $c->get(Router::class);
35 4
        $blockedRoutes = $c->has('blockedRoutes') ? $c->get('blockedRoutes') : [];
36 4
        $middlewares = $c->has('routeMiddleware') ? $c->get('routeMiddleware') : [];
37 4
        $this->container = $c;
38 4
        $this->router = $router;
39 4
        $this->blockedRoutes = $blockedRoutes;
0 ignored issues
show
Documentation Bug introduced by
It seems like $blockedRoutes of type * is incompatible with the declared type array of property $blockedRoutes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40 4
        $this->middlewares = $middlewares;
0 ignored issues
show
Documentation Bug introduced by
It seems like $middlewares of type * is incompatible with the declared type array<integer,object<Psr...r\MiddlewareInterface>> of property $middlewares.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41 4
    }
42
43
    /**
44
     * @param ServerRequestInterface $request
45
     * @param RequestHandlerInterface $handler
46
     * @return ResponseInterface
47
     */
48 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
49
    {
50 3
        $routes = $this->router->getRoutes();
51
52
        /** @var Route $route */
53 3
        foreach ($routes as $route) {
54 3
            $path = $route->getPath();
55
56 3
            if (in_array($path, $this->blockedRoutes)) {
57 1
                $this->router->removeRoute($route);
58 1
                break;
59
            }
60
61 2
            if (array_key_exists($path, $this->middlewares)) {
62 1
                $this->handleMiddleware($path, $route);
63
            }
64
        }
65
66 3
        return $handler->handle($request);
67
    }
68
69 1
    private function handleMiddleware(string $path, Route $route)
70
    {
71 1
        $routeMiddleware = $this->middlewares[$path];
72
73 1
        if (is_array($routeMiddleware)) {
74 1
            foreach ($routeMiddleware as $middleware) {
75 1
                $this->addMiddleware($route, $middleware);
76
            }
77
        } else {
78 1
            $this->addMiddleware($route, $routeMiddleware);
79
        }
80
81 1
    }
82
83
    /**
84
     * @param Route $route
85
     * @param $middleware
86
     */
87 1
    private function addMiddleware(Route $route, $middleware): void
88
    {
89 1
        if ($middleware instanceof MiddlewareInterface) {
90 1
            $route->middleware($middleware);
91 1
        } elseif (is_string($middleware) && $this->container->has($middleware)) {
92 1
            $middleware = $this->container->get($middleware);
93 1
            $route->middleware($middleware);
94
        }
95
    }
96
}