Test Failed
Push — master ( 82d8c9...b85154 )
by Koldo
02:50
created

ReactFastRouter::append()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Antidot\React\Router;
4
5
use Antidot\Application\Http\Middleware\CallableMiddleware;
6
use Antidot\Application\Http\Middleware\PipedRouteMiddleware;
7
use Antidot\Application\Http\Route;
8
use Antidot\Application\Http\Router;
9
use Antidot\Container\MiddlewareFactory;
10
use Antidot\Container\RequestHandlerFactory;
11
use Antidot\React\MiddlewarePipeline;
12
use FastRoute\DataGenerator\GroupCountBased;
0 ignored issues
show
Bug introduced by
The type FastRoute\DataGenerator\GroupCountBased was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use FastRoute\Dispatcher;
0 ignored issues
show
Bug introduced by
The type FastRoute\Dispatcher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use FastRoute\RouteCollector;
0 ignored issues
show
Bug introduced by
The type FastRoute\RouteCollector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use FastRoute\RouteParser\Std;
0 ignored issues
show
Bug introduced by
The type FastRoute\RouteParser\Std was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use LogicException;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use React\EventLoop\LoopInterface;
20
21
class ReactFastRouter implements Router
22
{
23
    private $routeCollector;
24
    private $middlewareFactory;
25
    private $requestHandlerFactory;
26
    private $dispatcher;
27
    private $loadedMiddleware;
28
    private $loop;
29
30
    public function __construct(
31
        MiddlewareFactory $middlewareFactory,
32
        RequestHandlerFactory $requestHandlerFactory,
33
        LoopInterface $loop
34
    ) {
35
        $this->routeCollector = new RouteCollector(new Std(), new GroupCountBased());
36
        $this->middlewareFactory = $middlewareFactory;
37
        $this->requestHandlerFactory = $requestHandlerFactory;
38
        $this->loop = $loop;
39
        $this->loadedMiddleware = [];
40
    }
41
42
    public function append(Route $route): void
43
    {
44
        $this->routeCollector->addRoute($route->method(), $route->path(), $route->pipeline());
45
    }
46
47
    public function match(ServerRequestInterface $request): PipedRouteMiddleware
48
    {
49
        if (null === $this->dispatcher) {
50
            $this->dispatcher = new Dispatcher\GroupCountBased($this->routeCollector->getData());
0 ignored issues
show
Bug introduced by
The type FastRoute\Dispatcher\GroupCountBased was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
        }
52
        $routeInfo = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
53
        $handler = array_key_last($routeInfo[1]);
54
        switch ($routeInfo[0]) {
55
            case Dispatcher::NOT_FOUND:
56
            case Dispatcher::METHOD_NOT_ALLOWED:
57
                if (array_key_exists($handler, $this->loadedMiddleware)) {
58
                    return $this->loadedMiddleware[$handler];
59
                }
60
                $this->loadedMiddleware[$handler] = new PipedRouteMiddleware(
61
                    new MiddlewarePipeline($this->loop),
62
                    true, []
63
                );
64
65
                return $this->loadedMiddleware[$routeInfo[1]];
66
            case Dispatcher::FOUND:
67
                if (array_key_exists($handler, $this->loadedMiddleware)) {
68
                    return $this->loadedMiddleware[$handler];
69
                }
70
71
                $this->loadedMiddleware[$handler] = new PipedRouteMiddleware(
72
                    $this->getPipeline($routeInfo[1]),
73
                    false,
74
                    $routeInfo[2]
75
                );
76
77
                return $this->loadedMiddleware[$handler];
78
        }
79
80
        throw new LogicException('Something went wrong in routing.');
81
    }
82
83
    private function getPipeline(array $middlewarePipeline): MiddlewarePipeline
84
    {
85
        $pipeline = new MiddlewarePipeline($this->loop);
86
        $handler = array_pop($middlewarePipeline);
87
88
        foreach ($middlewarePipeline as $middleware) {
89
            $pipeline->pipe($this->middlewareFactory->create($middleware));
90
        }
91
92
        $requestHandlerFactory = $this->requestHandlerFactory;
93
        $handler = $requestHandlerFactory->create($handler);
94
        $callableHandler = static function (ServerRequestInterface $request) use ($handler): ResponseInterface {
95
            return $handler->handle($request);
96
        };
97
        $pipeline->pipe(new CallableMiddleware($callableHandler));
98
99
        return $pipeline;
100
    }
101
}
102