Completed
Push — master ( 54ccda...232c7c )
by Changwan
04:16
created

HttpRouterBootstrap   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 68.42%

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 13
cts 19
cp 0.6842
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A providers() 0 8 1
A boot() 0 3 1
B execute() 0 24 4
1
<?php
2
namespace Wandu\Foundation\Bootstrap;
3
4
use Throwable;
5
use Wandu\Config\ConfigServiceProvider;
6
use Wandu\DI\ContainerInterface;
7
use Wandu\Foundation\Contracts\Bootstrap;
8
use Wandu\Foundation\Contracts\HttpErrorHandler;
9
use Wandu\Foundation\Error\DefaultHttpErrorHandler;
10
use Wandu\Http\Factory\ServerRequestFactory;
11
use Wandu\Http\HttpServiceProvider;
12
use Wandu\Http\Sender\ResponseSender;
13
use Wandu\Router\Dispatcher;
14
use Wandu\Router\RouterServiceProvider;
15
16
class HttpRouterBootstrap implements Bootstrap
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 1
    public function providers(): array
22
    {
23
        return [
24 1
            new ConfigServiceProvider(),
25 1
            new HttpServiceProvider(),
26 1
            new RouterServiceProvider(),
27
        ];
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function boot(ContainerInterface $app)
34
    {
35 1
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1
    public function execute(ContainerInterface $app): int
41
    {
42 1
        if (!$app->has(HttpErrorHandler::class)) {
43 1
            $app->bind(HttpErrorHandler::class, DefaultHttpErrorHandler::class);
44
        }
45
46 1
        $request = $app->get(ServerRequestFactory::class)->createFromGlobals();
47
        
48
        try {
49 1
            $response = $app->get(Dispatcher::class)->dispatch($request);
50
        } catch (Throwable $exception) {
51
            // output buffer clean
52
            while (ob_get_level() > 0) {
53
                ob_end_clean();
54
            }
55
56
            $errorHandler = $app->get(HttpErrorHandler::class);
57
            $app->get(ResponseSender::class)->sendToGlobal($errorHandler->handle($request, $exception));
58
            return -1;
59
        }
60
61 1
        $app->get(ResponseSender::class)->sendToGlobal($response);
62 1
        return 0;
63
    }
64
}
65