ReactRequestHandlerRunner::printResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
ccs 0
cts 16
cp 0
crap 6
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\React\Runner;
6
7
use Antidot\React\CallablePipeline;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use React\EventLoop\LoopInterface;
11
use React\Http\Server;
12
use React\Socket\ServerInterface;
13
use Throwable;
14
use Zend\HttpHandlerRunner\Emitter\EmitterStack;
0 ignored issues
show
Bug introduced by
The type Zend\HttpHandlerRunner\Emitter\EmitterStack 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 Zend\HttpHandlerRunner\RequestHandlerRunner;
0 ignored issues
show
Bug introduced by
The type Zend\HttpHandlerRunner\RequestHandlerRunner 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
17
use function React\Promise\resolve;
18
use function sprintf;
19
use function strpos;
20
21
class ReactRequestHandlerRunner extends RequestHandlerRunner
22
{
23
    /**
24
     * A request handler to run as the application.
25
     */
26
    private CallablePipeline $handler;
27
28
    /**
29
     * React Http Server
30
     */
31
    private ServerInterface $socketServer;
32
33
    private LoopInterface $loop;
34
    /** @var callable * */
35
    private $errorResponseGenerator;
36
37
    public function __construct(
38
        CallablePipeline $handler,
39
        EmitterStack $emitterStack,
40
        callable $errorResponseGenerator,
41
        ServerInterface $socketServer,
42
        LoopInterface $loop
43
    ) {
44
        parent::__construct(
45
            $handler,
46
            $emitterStack,
47
            function () {
48
            },
49
            $errorResponseGenerator
50
        );
51
52
        $this->handler = $handler;
53
        $this->loop = $loop;
54
        $this->socketServer = $socketServer;
55
        $this->errorResponseGenerator = $errorResponseGenerator;
56
    }
57
58
    public function run(): void
59
    {
60
        $handler = $this->handler;
61
        $errorResponseGenerator = $this->errorResponseGenerator;
62
        $server = new Server(static function (ServerRequestInterface $request) use ($handler, $errorResponseGenerator) {
63
            $next = clone $handler;
64
            return resolve($handler->__invoke($request, $next)->then(static function (ResponseInterface $response) use (
65
                $request
66
            ) {
67
                self::printResponse($request, $response);
68
                return $response;
69
            }, static function (Throwable $e) use ($request, $errorResponseGenerator) {
70
                self::printErrorResponse($request);
71
72
                return $errorResponseGenerator($e->getPrevious() ?? $e);
73
            }));
74
        });
75
        $server->on('error', static function (Throwable $e) {
76
            self::printServerError($e);
77
        });
78
        $server->listen($this->socketServer);
79
80
        $this->loop->run();
81
    }
82
83
    private function printResponse(ServerRequestInterface $request, ResponseInterface $response): void
84
    {
85
        if (0 === strpos((string)$response->getStatusCode(), '20')) {
86
            echo sprintf(
87
                "[%s] \033[0;32m%s\033[0m - %s",
88
                $request->getMethod(),
89
                $response->getStatusCode(),
90
                $request->getUri()->getPath()
91
            ) . PHP_EOL;
92
        } else {
93
            echo sprintf(
94
                "[%s] \033[0;33m%s\033[0m - %s",
95
                $request->getMethod(),
96
                $response->getStatusCode(),
97
                $request->getUri()->getPath()
98
            ) . PHP_EOL;
99
        }
100
    }
101
102
    private function printErrorResponse(ServerRequestInterface $request): void
103
    {
104
        echo sprintf(
105
            "[%s] \033[0;31m500\033[0m - %s",
106
            $request->getMethod(),
107
            $request->getUri()->getPath()
108
        ) . PHP_EOL;
109
    }
110
111
    private function printServerError(Throwable $e): void
112
    {
113
        $e = $e->getPrevious() ?? $e;
114
115
        echo sprintf(
116
            '[%s]: Server error occurred: %s, in file %s in line %s',
117
            get_class($e),
118
            $e->getMessage(),
119
            $e->getFile(),
120
            $e->getLine()
121
        ) . PHP_EOL;
122
        echo sprintf(
123
            '[%s]: %s',
124
            get_class($e),
125
            $e->getTraceAsString()
126
        ) . PHP_EOL;
127
    }
128
}
129