ReactApplication::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\React;
6
7
use Antidot\Application\Http\Application;
8
use Antidot\Application\Http\RouteFactory;
9
use Antidot\Application\Http\Router;
10
use Antidot\Container\MiddlewareFactory;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Server\MiddlewareInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
use RuntimeException;
16
use function React\Promise\resolve;
17
18
class ReactApplication implements Application, RequestHandlerInterface, MiddlewareInterface
19
{
20
    private MiddlewarePipeline $pipeline;
21
    private Router $router;
22
    private MiddlewareFactory $middlewareFactory;
23
    private RouteFactory $routeFactory;
24
25 11
    public function __construct(
26
        MiddlewarePipeline $pipeline,
27
        Router $router,
28
        MiddlewareFactory $middlewareFactory,
29
        RouteFactory $routeFactory
30
    ) {
31 11
        $this->routeFactory = $routeFactory;
32 11
        $this->middlewareFactory = $middlewareFactory;
33 11
        $this->router = $router;
34 11
        $this->pipeline = $pipeline;
35 11
    }
36
37 1
    public function pipe(string $middlewareName): void
38
    {
39 1
        $this->pipeline->pipe($this->middlewareFactory->create($middlewareName));
40 1
    }
41
42 1
    public function get(string $uri, array $middleware, string $name): void
43
    {
44 1
        $this->route('GET', $uri, $middleware, $name);
45 1
    }
46
47 1
    public function post(string $uri, array $middleware, string $name): void
48
    {
49 1
        $this->route('POST', $uri, $middleware, $name);
50 1
    }
51
52 1
    public function patch(string $uri, array $middleware, string $name): void
53
    {
54 1
        $this->route('PATCH', $uri, $middleware, $name);
55 1
    }
56
57 1
    public function put(string $uri, array $middleware, string $name): void
58
    {
59 1
        $this->route('PUT', $uri, $middleware, $name);
60 1
    }
61
62 1
    public function delete(string $uri, array $middleware, string $name): void
63
    {
64 1
        $this->route('DELETE', $uri, $middleware, $name);
65 1
    }
66
67 1
    public function options(string $uri, array $middleware, string $name): void
68
    {
69 1
        $this->route('OPTIONS', $uri, $middleware, $name);
70 1
    }
71
72 6
    public function route(string $method, string $uri, array $middleware, string $name): void
73
    {
74 6
        $this->router->append(
75 6
            $this->routeFactory->create([$method], $middleware, $uri, $name)
76
        );
77 6
    }
78
79 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
80
    {
81 1
        return new PromiseResponse(resolve($request)
82 1
            ->then(
83 1
                fn(ServerRequestInterface $request): ResponseInterface  => $this->pipeline->process($request, $handler)
84 1
            ));
85
    }
86
87 1
    public function handle(ServerRequestInterface $request): ResponseInterface
88
    {
89 1
        return new PromiseResponse(resolve($request)
90 1
            ->then(
91 1
                fn (ServerRequestInterface $request): ResponseInterface => $this->pipeline->handle($request)
92 1
            ));
93
    }
94
95 1
    public function run(): void
96
    {
97 1
        throw new RuntimeException('You can\'t run application out of React PHP server.');
98
    }
99
}
100