Passed
Pull Request — master (#234)
by Alexander
02:24
created

Application::afterEmit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
crap 2
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