Passed
Push — master ( 05379a...b02cff )
by Alexander
02:00
created

Application::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Web;
4
5
use Psr\EventDispatcher\EventDispatcherInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Yiisoft\Yii\Web\ErrorHandler\ErrorHandler;
9
use Yiisoft\Yii\Web\Event\AfterEmit;
10
use Yiisoft\Yii\Web\Event\AfterRequest;
11
use Yiisoft\Yii\Web\Event\ApplicationShutdown;
12
use Yiisoft\Yii\Web\Event\ApplicationStartup;
13
use Yiisoft\Yii\Web\Event\BeforeRequest;
14
15
/**
16
 * Application is the entry point for a web application.
17
 * For more details and usage information on Application, see the [guide article on
18
 * applications](guide:structure-applications).
19
 */
20
final class Application
21
{
22
    private MiddlewareDispatcher $dispatcher;
23
    private EventDispatcherInterface $eventDispatcher;
24
    private ErrorHandler $errorHandler;
25
26
    public function __construct(
27
        MiddlewareDispatcher $dispatcher,
28
        ErrorHandler $errorHandler,
29
        EventDispatcherInterface $eventDispatcher
30
    ) {
31
        $this->dispatcher = $dispatcher;
32
        $this->errorHandler = $errorHandler;
33
        $this->eventDispatcher = $eventDispatcher;
34
    }
35
36
    public function start(): void
37
    {
38
        $this->errorHandler->register();
39
        $this->eventDispatcher->dispatch(new ApplicationStartup());
40
    }
41
42
    public function shutdown(): void
43
    {
44
        $this->eventDispatcher->dispatch(new ApplicationShutdown());
45
    }
46
47
    public function afterEmit(ResponseInterface $response): void
48
    {
49
        $this->eventDispatcher->dispatch(new AfterEmit($response));
50
    }
51
52
    public function handle(ServerRequestInterface $request): ResponseInterface
53
    {
54
        $this->eventDispatcher->dispatch(new BeforeRequest($request));
55
        $response = $this->dispatcher->dispatch($request);
56
        $this->eventDispatcher->dispatch(new AfterRequest($response));
57
        return $response;
58
    }
59
}
60