Completed
Push — master ( 97318c...d75021 )
by Changwan
03:35
created

HttpRouterBootstrapper::execute()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.024

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 6
nop 1
dl 0
loc 28
ccs 9
cts 15
cp 0.6
crap 5.024
rs 8.5806
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Foundation\Bootstrapper;
3
4
use Closure;
5
use Psr\Http\Message\ServerRequestInterface;
6
use Throwable;
7
use Wandu\Config\ConfigServiceProvider;
8
use Wandu\DI\ContainerInterface;
9
use Wandu\Foundation\Contracts\Bootstrapper;
10
use Wandu\Foundation\Contracts\HttpErrorHandler;
11
use Wandu\Foundation\Error\DefaultHttpErrorHandler;
12
use Wandu\Http\Factory\ServerRequestFactory;
13
use Wandu\Http\HttpServiceProvider;
14
use Wandu\Http\Sender\ResponseSender;
15
use Wandu\Router\Dispatcher;
16
use Wandu\Router\RouterServiceProvider;
17
18
class HttpRouterBootstrapper implements Bootstrapper
19
{
20
    /** @var \Closure */
21
    protected $routes;
22
23 1
    public function __construct(Closure $routes = null)
24
    {
25 1
        $this->routes = $routes;
26 1
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function providers(): array
32
    {
33
        return [
34 1
            new ConfigServiceProvider(),
35 1
            new HttpServiceProvider(),
36 1
            new RouterServiceProvider(),
37
        ];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    public function boot(ContainerInterface $app)
44
    {
45 1
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function execute(ContainerInterface $app): int
51
    {
52 1
        if (!$app->has(HttpErrorHandler::class)) {
53 1
            $app->bind(HttpErrorHandler::class, DefaultHttpErrorHandler::class);
54
        }
55
56 1
        $requestFactory = $app->get(ServerRequestFactory::class);
57 1
        $responseSender = $app->get(ResponseSender::class);
58
59 1
        $request = $requestFactory->createFromGlobals();
60
        
61
        try {
62 1
            $response = $this->dispatch($app->get(Dispatcher::class), $request);
63
        } catch (Throwable $exception) {
64
65
            // output buffer clean
66
            while (ob_get_level() > 0) {
67
                ob_end_clean();
68
            }
69
70
            $errorHandler = $app->get(HttpErrorHandler::class);
71
            $responseSender->sendToGlobal($errorHandler->handle($request, $exception));
72
            return -1;
73
        }
74
75 1
        $responseSender->sendToGlobal($response);
76 1
        return 0;
77
    }
78
79
    /**
80
     * @param \Wandu\Router\Dispatcher $dispatcher
81
     * @param \Psr\Http\Message\ServerRequestInterface $request
82
     * @return \Psr\Http\Message\ResponseInterface
83
     * @throws \Wandu\Http\Exception\MethodNotAllowedException
84
     * @throws \Wandu\Http\Exception\NotFoundException
85
     */
86 1
    protected function dispatch(Dispatcher $dispatcher, ServerRequestInterface $request)
87
    {
88 1
        if ($this->routes) {
89 1
            $dispatcher->setRoutes($this->routes);
90
        }
91 1
        return $dispatcher->dispatch($request);
92
    }
93
}
94