Passed
Pull Request — master (#831)
by Maxim
07:40
created

RoutesBootloader::configureRouteGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\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 267
    public function init(HttpBootloader $http): void
18
    {
19 267
        foreach ($this->globalMiddleware() as $middleware) {
20 267
            $http->addMiddleware($middleware);
21
        }
22
    }
23
24 267
    public function boot(RoutingConfigurator $routes, BinderInterface $binder, GroupRegistry $groups): void
25
    {
26 267
        $middlewareGroups = $this->middlewareGroups();
27
28 267
        $this->registerMiddlewareGroups($binder, $middlewareGroups);
29 267
        $this->registerMiddlewareForRouteGroups($groups, \array_keys($middlewareGroups));
30
31 267
        $this->configureRouteGroups($groups);
32 267
        $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
     */
52
    abstract protected function globalMiddleware(): array;
53
54
    /**
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
     */
57
    abstract protected function middlewareGroups(): array;
58
59 267
    private function registerMiddlewareGroups(BinderInterface $binder, array $groups): void
60
    {
61 267
        foreach ($groups as $group => $middleware) {
62 267
            $binder->bind(
63
                'middleware:' . $group,
64 267
                static function (PipelineFactory $factory) use ($middleware): Pipeline {
65 267
                    return $factory->createWithMiddleware($middleware);
66
                }
67
            );
68
        }
69
    }
70
71 267
    private function registerMiddlewareForRouteGroups(GroupRegistry $registry, array $groups): void
72
    {
73 267
        foreach ($groups as $group) {
74 267
            $registry->getGroup($group)->addMiddleware('middleware:' . $group);
75
        }
76
    }
77
}
78