Test Failed
Pull Request — master (#831)
by Maxim
13:01
created

RoutesBootloader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 15
dl 0
loc 60
ccs 16
cts 16
cp 1
rs 10
c 2
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 2
A configureRouteGroups() 0 2 1
A registerMiddlewareGroups() 0 7 2
A defineRoutes() 0 2 1
A registerMiddlewareForRouteGroups() 0 4 2
A boot() 0 9 1
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\Http\Pipeline;
11
use Spiral\Router\GroupRegistry;
12
use Spiral\Router\Loader\Configurator\RoutingConfigurator;
13
use Spiral\Router\PipelineFactory;
14
15
abstract class RoutesBootloader extends Bootloader
16
{
17 266
    public function init(HttpBootloader $http): void
18
    {
19 266
        foreach ($this->globalMiddleware() as $middleware) {
20 266
            $http->addMiddleware($middleware);
21
        }
22
    }
23
24 266
    public function boot(RoutingConfigurator $routes, BinderInterface $binder, GroupRegistry $groups): void
25
    {
26 266
        $middlewareGroups = $this->middlewareGroups();
27
28 266
        $this->registerMiddlewareGroups($binder, $middlewareGroups);
29 266
        $this->registerMiddlewareForRouteGroups($groups, \array_keys($middlewareGroups));
30
31 266
        $this->configureRouteGroups($groups);
32
        $this->defineRoutes($routes);
33
    }
34
35
    /**
36
     * Override this method to configure application routes
37
     */
38
    protected function defineRoutes(RoutingConfigurator $routes): void
39
    {
40
    }
41
42
    /**
43
     * Override this method to configure route groups
44
     */
45
    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

45
    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...
46
    {
47
    }
48
49
    /**
50
     * @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...
51 266
     */
52
    abstract protected function globalMiddleware(): array;
53 266
54 266
    /**
55
     * @return array<string,array<MiddlewareInterface|class-string<MiddlewareInterface>>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string,array<Middl...<MiddlewareInterface>>> at position 8 could not be parsed: Unknown type name 'class-string' at position 8 in array<string,array<MiddlewareInterface|class-string<MiddlewareInterface>>>.
Loading history...
56 266
     */
57 266
    abstract protected function middlewareGroups(): array;
58
59
    private function registerMiddlewareGroups(BinderInterface $binder, array $groups): void
60
    {
61
        foreach ($groups as $group => $middleware) {
62
            $binder->bind(
63 266
                'middleware:' . $group,
64
                static function (PipelineFactory $factory) use ($middleware): Pipeline {
65 266
                    return $factory->createWithMiddleware($middleware);
66 266
                }
67
            );
68
        }
69
    }
70
71
    private function registerMiddlewareForRouteGroups(GroupRegistry $registry, array $groups): void
72
    {
73
        foreach ($groups as $group) {
74
            $registry->getGroup($group)->addMiddleware('middleware:' . $group);
75
        }
76
    }
77
}
78