Issues (13)

src/Runtime/Aura/HttpRouterTrait.php (2 issues)

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace inroutephp\inroute\Runtime\Aura;
6
7
use inroutephp\inroute\Runtime\Environment;
8
use inroutephp\inroute\Runtime\NaiveContainer;
9
use inroutephp\inroute\Runtime\Exception\RouteNotFoundException;
10
use inroutephp\inroute\Runtime\Exception\MethodNotAllowedException;
11
use inroutephp\inroute\Runtime\Middleware\DispatchingMiddleware;
12
use inroutephp\inroute\Runtime\Middleware\Pipeline;
13
use Psr\Container\ContainerInterface;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ResponseFactoryInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use Aura\Router\RouterContainer;
19
use Aura\Router\Map;
20
21
trait HttpRouterTrait
22
{
23
    /**
24
     * @var ContainerInterface
25
     */
26
    private $container;
27
28
    public function __construct()
29
    {
30
        $this->container = new NaiveContainer;
31
    }
32
33
    abstract protected function loadRoutes(Map $map): void;
34
35
    public function setContainer(ContainerInterface $container): void
36
    {
37
        $this->container = $container;
38
    }
39
40
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
0 ignored issues
show
The parameter $handler is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
    public function process(ServerRequestInterface $request, /** @scrutinizer ignore-unused */ RequestHandlerInterface $handler): ResponseInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        $router = new RouterContainer;
43
44
        $this->loadRoutes($router->getMap());
45
46
        $matcher = $router->getMatcher();
47
48
        $match = $matcher->match($request);
49
50
        if (!$match) {
51
            $failedRoute = $matcher->getFailedRoute();
52
53
            if ($failedRoute && $failedRoute->failedRule == 'Aura\Router\Rule\Allows') {
54
                if ($this->container->has(ResponseFactoryInterface::CLASS)) {
0 ignored issues
show
The constant Psr\Http\Message\ResponseFactoryInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
                    return $this->container->get(ResponseFactoryInterface::CLASS)
56
                        ->createResponse(405, 'Method Not Allowed')
57
                        ->withHeader('Allow', implode(', ', $failedRoute->allows));
58
                }
59
60
                throw new MethodNotAllowedException($request, ['allows' => $failedRoute->allows]);
61
            }
62
63
            if ($this->container->has(ResponseFactoryInterface::CLASS)) {
64
                return $this->container->get(ResponseFactoryInterface::CLASS)->createResponse(404, 'Not Found');
65
            }
66
67
            throw new RouteNotFoundException($request);
68
        }
69
70
        /** @var RouteInterface $route */
71
        $route = $match->handler;
72
73
        foreach ($route->getAttributes() as $name => $val) {
74
            $request = $request->withAttribute($name, $val);
75
        }
76
77
        foreach ($match->attributes as $name => $val) {
78
            $request = $request->withAttribute($name, $val);
79
        }
80
81
        $middlewares = [];
82
83
        foreach ($route->getMiddlewareServiceIds() as $serviceId) {
84
            $middlewares[] = $this->container->get($serviceId);
85
        }
86
87
        $middlewares[] = new DispatchingMiddleware(
88
            [$this->container->get($route->getServiceId()), $route->getServiceMethod()],
89
            new Environment($route, new UrlGenerator($router->getGenerator()))
90
        );
91
92
        return (new Pipeline(...$middlewares))->handle($request);
93
    }
94
}
95