Completed
Push — master ( f640ab...96ed2b )
by n
03:55
created

registerHttpApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Providers;
5
6
use Illuminate\Container\Container;
7
use N1215\Http\Router\RouterInterface;
8
use N1215\Http\Router\RoutingErrorResponderInterface;
9
use N1215\Http\Router\RoutingHandler;
10
use N1215\Http\Router\RoutingHandlerInterface;
11
use N1215\Jugoya\RequestHandlerBuilderInterface;
12
use N1215\Tsukuyomi\BootLoader;
13
use N1215\Tsukuyomi\BootLoaderInterface;
14
use N1215\Tsukuyomi\Event\EventManagerInterface;
15
use N1215\Tsukuyomi\FrameworkInterface;
16
use N1215\Tsukuyomi\HttpApplication;
17
use N1215\Tsukuyomi\HttpApplicationInterface;
18
use Zend\Diactoros\Response\SapiEmitter;
19
20
class HttpApplicationServiceProvider
21
{
22 1
    public function register(Container $container)
23
    {
24 1
        $this->registerBootLoader($container);
25 1
        $this->registerRoutingHandler($container);
26 1
        $this->registerHttpApplication($container);
27 1
    }
28
29
    function registerBootLoader(Container $container)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
    {
31 1
        $container->singleton(BootLoaderInterface::class, function (Container $container) {
32
            /** @var FrameworkInterface $framework */
33 1
            $framework = $container->get(FrameworkInterface::class);
34 1
            $bootstrapConfigPath = $framework->path('config/bootstrappers.php');
35 1
            $bootstrapperClasses = require $bootstrapConfigPath;
36 1
            return new BootLoader($bootstrapperClasses);
37 1
        });
38 1
    }
39
40
    private function registerRoutingHandler(Container $container)
41
    {
42 1
        $container->singleton(RoutingHandlerInterface::class, function (Container $container) {
43 1
            return new RoutingHandler(
44 1
                $container->get(RouterInterface::class),
45 1
                $container->get(RoutingErrorResponderInterface::class)
46
            );
47 1
        });
48 1
    }
49
50
    private function registerHttpApplication(Container $container)
51
    {
52 1
        $container->singleton(HttpApplicationInterface::class, function (Container $container) {
53
            /** @var RequestHandlerBuilderInterface $handlerBuilder */
54 1
            $handlerBuilder = $container->get(RequestHandlerBuilderInterface::class);
55
            /** @var FrameworkInterface $framework */
56 1
            $framework = $container->get(FrameworkInterface::class);
57 1
            $middlewareConfigPath = $framework->path('config/middlewares.php');
58 1
            $middlewareClasses = require $middlewareConfigPath;
59 1
            $requestHandler = $handlerBuilder->build(RoutingHandlerInterface::class, $middlewareClasses);
60
61 1
            return new HttpApplication(
62 1
                $container->get(BootLoaderInterface::class),
63 1
                $requestHandler,
64 1
                new SapiEmitter(),
65 1
                $container->get(EventManagerInterface::class)
66
            );
67 1
        });
68 1
    }
69
}
70