Passed
Push — master ( 83b89e...48455b )
by Hannes
02:13
created

HttpRouterTrait::process()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 25
nc 8
nop 2
dl 0
loc 49
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace inroutephp\inroute\Runtime\Aura;
6
7
use inroutephp\inroute\Runtime\DispatchingMiddleware;
8
use inroutephp\inroute\Runtime\Environment;
9
use inroutephp\inroute\Runtime\NaiveContainer;
10
use inroutephp\inroute\Runtime\Exception\RouteNotFoundException;
11
use inroutephp\inroute\Runtime\Exception\MethodNotAllowedException;
12
use Psr\Container\ContainerInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ResponseFactoryInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Server\RequestHandlerInterface;
17
use Aura\Router\RouterContainer;
18
use Aura\Router\Map;
19
use mindplay\middleman\Dispatcher;
20
use mindplay\middleman\ContainerResolver;
21
22
trait HttpRouterTrait
23
{
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    public function __construct()
30
    {
31
        $this->container = new NaiveContainer;
32
    }
33
34
    abstract protected function loadRoutes(Map $map): void;
35
36
    public function setContainer(ContainerInterface $container): void
37
    {
38
        $this->container = $container;
39
    }
40
41
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
0 ignored issues
show
Unused Code introduced by
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

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