Test Failed
Push — master ( 46a9e3...b06516 )
by Koldo
05:39
created

ReactPHPApplication::__invoke()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\React;
6
7
use Antidot\Application\Http\RouteFactory;
8
use Antidot\Application\Http\Router;
9
use Antidot\Application\Http\WebServerApplication;
0 ignored issues
show
Bug introduced by
The type Antidot\Application\Http\WebServerApplication 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...
10
use Antidot\Container\MiddlewareFactory;
11
use Psr\Http\Message\ServerRequestInterface;
12
use React\EventLoop\LoopInterface;
13
use React\Promise\Promise;
14
use React\Promise\PromiseInterface;
15
use Recoil\React\ReactKernel;
16
use Throwable;
17
use Zend\HttpHandlerRunner\RequestHandlerRunner;
18
use function React\Promise\resolve;
19
20
class ReactPHPApplication extends WebServerApplication
21
{
22
    private $kernel;
23
24
    public function __construct(
25
        RequestHandlerRunner $runner,
26
        Resettable $pipeline,
27
        Router $router,
28
        MiddlewareFactory $middlewareFactory,
29
        RouteFactory $routeFactory,
30
        LoopInterface $loop
31
    ) {
32
        parent::__construct($runner, $pipeline, $router, $middlewareFactory, $routeFactory);
33
        $this->kernel = ReactKernel::create($loop);
34
    }
35
36
    public function __invoke(ServerRequestInterface $request): PromiseInterface
37
    {
38
        return new Promise(function ($resolve, $reject) use ($request) {
39
            $this->kernel->execute(function () use ($resolve, $reject, $request) {
40
                try {
41
                    $response = $this->pipeline->handle($request);
42
                    $this->pipeline->reset();
43
                    $response = resolve($response);
44
                    $response = (yield $response);
45
                    $resolve($response);
46
                } catch (Throwable $throwable) {
47
                    $this->pipeline->reset();
48
                    $reject($throwable);
49
                }
50
            });
51
        });
52
    }
53
}
54