Passed
Push — master ( aebcb6...ddb17c )
by Alexander
07:50
created

Application::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

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