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

Application   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 15
c 3
b 0
f 0
dl 0
loc 38
rs 10
ccs 0
cts 12
cp 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 6 1
A shutdown() 0 3 1
A start() 0 4 1
A __construct() 0 8 1
A afterEmit() 0 3 1
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