Passed
Pull Request — master (#4)
by Evgeniy
12:51 queued 10:40
created

ServerRequestHandler::start()   A

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 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Runner\Http;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
12
use Yiisoft\Yii\Runner\Http\Event\AfterEmit;
13
use Yiisoft\Yii\Runner\Http\Event\AfterRequest;
14
use Yiisoft\Yii\Runner\Http\Event\ApplicationShutdown;
15
use Yiisoft\Yii\Runner\Http\Event\ApplicationStartup;
16
use Yiisoft\Yii\Runner\Http\Event\BeforeRequest;
17
18
/**
19
 * ServerRequestHandler is the entry point for an HTTP application.
20
 *
21
 * For more details and usage information on Application, see the guide article on applications:
22
 *
23
 * @see https://github.com/yiisoft/docs/blob/master/guide/en/structure/application.md.
24
 */
25
final class ServerRequestHandler
26
{
27
    private MiddlewareDispatcher $dispatcher;
28
    private EventDispatcherInterface $eventDispatcher;
29
    private RequestHandlerInterface $fallbackHandler;
30
31 12
    public function __construct(
32
        MiddlewareDispatcher $dispatcher,
33
        EventDispatcherInterface $eventDispatcher,
34
        RequestHandlerInterface $fallbackHandler
35
    ) {
36 12
        $this->dispatcher = $dispatcher;
37 12
        $this->eventDispatcher = $eventDispatcher;
38 12
        $this->fallbackHandler = $fallbackHandler;
39 12
    }
40
41 5
    public function start(): void
42
    {
43 5
        $this->eventDispatcher->dispatch(new ApplicationStartup());
44 5
    }
45
46 5
    public function shutdown(): void
47
    {
48 5
        $this->eventDispatcher->dispatch(new ApplicationShutdown());
49 5
    }
50
51 6
    public function afterEmit(?ResponseInterface $response): void
52
    {
53 6
        $this->eventDispatcher->dispatch(new AfterEmit($response));
54 6
    }
55
56 8
    public function handle(ServerRequestInterface $request): ResponseInterface
57
    {
58 8
        $this->eventDispatcher->dispatch(new BeforeRequest($request));
59
60
        try {
61 8
            return $response = $this->dispatcher->dispatch($request, $this->fallbackHandler);
62
        } finally {
63 8
            $this->eventDispatcher->dispatch(new AfterRequest($response ?? null));
64
        }
65
    }
66
}
67