Passed
Pull Request — master (#1168)
by Aleksei
12:55
created

RoutesBootloader::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Bootloader\Http;
6
7
use Psr\Http\Server\MiddlewareInterface;
8
use Spiral\Boot\Bootloader\Bootloader;
9
use Spiral\Core\BinderInterface;
10
use Spiral\Core\Container\Autowire;
11
use Spiral\Core\FactoryInterface;
12
use Spiral\Http\LazyPipeline;
13
use Spiral\Http\Pipeline;
14
use Spiral\Router\GroupRegistry;
15
use Spiral\Router\Loader\Configurator\RoutingConfigurator;
16
use Spiral\Router\PipelineFactory;
17
18
abstract class RoutesBootloader extends Bootloader
19
{
20 356
    public function init(HttpBootloader $http): void
21
    {
22 356
        foreach ($this->globalMiddleware() as $middleware) {
23 356
            $http->addMiddleware($middleware);
24
        }
25
    }
26
27 356
    public function boot(RoutingConfigurator $routes, BinderInterface $binder, GroupRegistry $groups): void
28
    {
29 356
        $middlewareGroups = $this->middlewareGroups();
30
31 356
        $this->registerMiddlewareGroups($binder, $middlewareGroups);
32 356
        $this->registerMiddlewareForRouteGroups($groups, \array_keys($middlewareGroups));
33
34 356
        $this->configureRouteGroups($groups);
35 356
        $this->defineRoutes($routes);
36
    }
37
38
    /**
39
     * Override this method to configure application routes
40
     */
41
    protected function defineRoutes(RoutingConfigurator $routes): void
0 ignored issues
show
Unused Code introduced by
The parameter $routes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    protected function defineRoutes(/** @scrutinizer ignore-unused */ RoutingConfigurator $routes): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
    }
44
45
    /**
46
     * Override this method to configure route groups
47
     */
48 356
    protected function configureRouteGroups(GroupRegistry $groups): void
0 ignored issues
show
Unused Code introduced by
The parameter $groups is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

48
    protected function configureRouteGroups(/** @scrutinizer ignore-unused */ GroupRegistry $groups): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50 356
    }
51
52
    /**
53
     * @return array<MiddlewareInterface|class-string<MiddlewareInterface>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<MiddlewareInterfac...g<MiddlewareInterface>> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<MiddlewareInterface|class-string<MiddlewareInterface>>.
Loading history...
54
     */
55
    abstract protected function globalMiddleware(): array;
56
57
    /**
58
     * @return array<string,array<MiddlewareInterface|class-string<MiddlewareInterface>|Autowire>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string,array<Middl...reInterface>|Autowire>> at position 8 could not be parsed: Unknown type name 'class-string' at position 8 in array<string,array<MiddlewareInterface|class-string<MiddlewareInterface>|Autowire>>.
Loading history...
59
     */
60
    abstract protected function middlewareGroups(): array;
61
62 356
    private function registerMiddlewareGroups(BinderInterface $binder, array $groups): void
63
    {
64 356
        foreach ($groups as $group => $middleware) {
65 356
            $binder
66 356
                ->getBinder('http')
0 ignored issues
show
Bug introduced by
The method getBinder() does not exist on Spiral\Core\BinderInterface. It seems like you code against a sub-type of Spiral\Core\BinderInterface such as Spiral\Core\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
                ->/** @scrutinizer ignore-call */ 
67
                  getBinder('http')
Loading history...
67 356
                ->bind(
68 356
                    'middleware:' . $group,
69 356
                    static function (FactoryInterface $factory) use ($middleware): LazyPipeline {
70 12
                        return $factory->make(LazyPipeline::class)->withAddedMiddleware(...$middleware);
71 356
                    }
72 356
                );
73
        }
74
    }
75
76 356
    private function registerMiddlewareForRouteGroups(GroupRegistry $registry, array $groups): void
77
    {
78 356
        foreach ($groups as $group) {
79 356
            $registry->getGroup($group)->addMiddleware('middleware:' . $group);
80
        }
81
    }
82
}
83