Passed
Pull Request — master (#227)
by Alexander
02:19
created

Application   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 14
c 3
b 0
f 0
dl 0
loc 33
ccs 0
cts 12
cp 0
rs 10
wmc 4

4 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
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