Passed
Push — master ( 5f70c3...c2ea24 )
by Alexander
03:02
created

Application::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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