Passed
Pull Request — master (#20)
by Evgeniy
13:30
created

RoadRunnerWorker   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 56
ccs 24
cts 28
cp 0.8571
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addApplicationStartTimeAttributeToRequest() 0 3 1
A waitRequest() 0 7 3
A respondError() 0 12 2
A respond() 0 3 1
A __construct() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Runner\RoadRunner;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestFactoryInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Message\StreamFactoryInterface;
13
use Psr\Http\Message\UploadedFileFactoryInterface;
14
use Spiral\RoadRunner\Http\PSR7Worker;
15
use Spiral\RoadRunner\Http\PSR7WorkerInterface;
16
use Spiral\RoadRunner\Worker;
17
use Throwable;
18
use Yiisoft\ErrorHandler\ErrorHandler;
19
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
20
use Yiisoft\Http\Status;
21
use Yiisoft\Yii\Http\Handler\ThrowableHandler;
22
23
use function microtime;
24
25
/**
26
 * @internal
27
 */
28
final class RoadRunnerWorker
29
{
30
    private ResponseFactoryInterface $responseFactory;
31
    private PSR7WorkerInterface $worker;
32
    private ErrorCatcher $errorCatcher;
33
    private ErrorHandler $errorHandler;
34
35 13
    public function __construct(ContainerInterface $container, PSR7WorkerInterface $worker = null)
36
    {
37
        /** @psalm-var ResponseFactoryInterface $this->responseFactory */
38 13
        $this->responseFactory = $container->get(ResponseFactoryInterface::class);
39
        /** @psalm-var ErrorCatcher $this->errorCatcher */
40 13
        $this->errorCatcher = $container->get(ErrorCatcher::class);
41
        /** @psalm-var ErrorHandler $this->errorHandler */
42 13
        $this->errorHandler = $container->get(ErrorHandler::class);
43
        /** @psalm-suppress MixedArgument */
44 13
        $this->worker = $worker ?? new PSR7Worker(
45
            Worker::create(),
46
            $container->get(ServerRequestFactoryInterface::class),
47
            $container->get(StreamFactoryInterface::class),
48
            $container->get(UploadedFileFactoryInterface::class),
49
        );
50 13
    }
51
52 9
    public function respond(ResponseInterface $response): void
53
    {
54 9
        $this->worker->respond($response);
55 9
    }
56
57 4
    public function respondError(Throwable $throwable, ServerRequestInterface $request = null): ResponseInterface
58
    {
59 4
        if ($request === null) {
60 2
            $errorData = $this->errorHandler->handle($throwable);
61 2
            $response = $errorData->addToResponse($this->responseFactory->createResponse(Status::BAD_REQUEST));
62
        } else {
63 2
            $request = $this->addApplicationStartTimeAttributeToRequest($request);
64 2
            $response = $this->errorCatcher->process($request, new ThrowableHandler($throwable));
65
        }
66
67 4
        $this->respond($response);
68 4
        return $response;
69
    }
70
71 10
    public function waitRequest(): ServerRequestInterface|Throwable|null
72
    {
73
        try {
74 10
            $request = $this->worker->waitRequest();
75 9
            return $request === null ? null : $this->addApplicationStartTimeAttributeToRequest($request);
76 2
        } catch (Throwable $t) {
77 2
            return $t;
78
        }
79
    }
80
81 7
    private function addApplicationStartTimeAttributeToRequest(ServerRequestInterface $request): ServerRequestInterface
82
    {
83 7
        return $request->withAttribute('applicationStartTime', microtime(true));
84
    }
85
}
86