Passed
Pull Request — master (#653)
by Aleksei
07:43
created

AppBootloader::boot()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 152
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 95
c 0
b 0
f 0
nc 1
nop 7
dl 0
loc 152
rs 8.109

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\App\Bootloader;
13
14
use Psr\Container\ContainerInterface;
15
use Spiral\App\Checker\MyChecker;
16
use Spiral\App\Condition\MyCondition;
17
use Spiral\App\Controller\AuthController;
18
use Spiral\App\Controller\InterceptedController;
19
use Spiral\App\Controller\TestController;
20
use Spiral\App\Interceptor;
21
use Spiral\App\User\UserRepository;
22
use Spiral\App\ViewEngine\TestEngine;
23
use Spiral\Bootloader\DomainBootloader;
24
use Spiral\Bootloader\Http\JsonPayloadsBootloader;
25
use Spiral\Bootloader\Security\ValidationBootloader;
26
use Spiral\Bootloader\Views\ViewsBootloader;
27
use Spiral\Core\Core;
28
use Spiral\Core\CoreInterface;
29
use Spiral\Core\InterceptableCore;
30
use Spiral\Domain\CycleInterceptor;
31
use Spiral\Domain\FilterInterceptor;
32
use Spiral\Domain\GuardInterceptor;
33
use Spiral\Domain\PipelineInterceptor;
34
use Spiral\Router\Route;
35
use Spiral\Router\RouterInterface;
36
use Spiral\Router\Target\Action;
37
use Spiral\Router\Target\Controller;
38
use Spiral\Security\PermissionsInterface;
39
40
class AppBootloader extends DomainBootloader
41
{
42
    protected const SINGLETONS = [
43
        CoreInterface::class => [self::class, 'domainCore']
44
    ];
45
46
    protected const INTERCEPTORS = [
47
        CycleInterceptor::class,
48
        GuardInterceptor::class,
49
        FilterInterceptor::class
50
    ];
51
    /** @var ContainerInterface */
52
    private $container;
53
54
    /** @var Core */
55
    private $core;
56
57
    public function __construct(ContainerInterface $container, Core $core)
58
    {
59
        $this->container = $container;
60
        $this->core = $core;
61
    }
62
63
    public function boot(
64
        \Spiral\Bootloader\Auth\AuthBootloader $authBootloader,
65
        RouterInterface $router,
66
        PermissionsInterface $rbac,
67
        ViewsBootloader $views,
68
        ValidationBootloader $validation,
69
        JsonPayloadsBootloader $json,
70
        PipelineInterceptor $pipelineInterceptor
71
    ): void {
72
        $authBootloader->addActorProvider(UserRepository::class);
73
74
        $rbac->addRole('user');
75
        $rbac->associate('user', '*');
76
77
        $rbac->addRole('demo');
78
        $rbac->associate('demo', 'demo.*');
79
80
        $route = new Route(
81
            '/<action>[/<name>]',
82
            new Controller(TestController::class)
83
        );
84
85
        $router->setDefault($route->withDefaults(['name' => 'Dave']));
86
87
        $router->setRoute(
88
            'auth',
89
            new Route('/auth/<action>', new Controller(AuthController::class))
90
        );
91
92
        $views->addDirectory('custom', __DIR__ . '/../../views/custom/');
93
        $views->addDirectory('stempler', __DIR__ . '/../../views/stempler/');
94
        $views->addEngine(TestEngine::class);
95
96
        $validation->addAlias('aliased', 'notEmpty');
97
        $validation->addChecker('my', MyChecker::class);
98
        $validation->addCondition('cond', MyCondition::class);
99
100
        $json->addContentType('application/vnd.api+json');
101
102
        $this->registerInterceptedRoute(
103
            $router,
104
            'without',
105
            [
106
                new Interceptor\Append('one'),
107
                new Interceptor\Append('two'),
108
                new Interceptor\Append('three'),
109
            ]
110
        );
111
112
        $this->registerInterceptedRoute(
113
            $router,
114
            'with',
115
            [
116
                $pipelineInterceptor,
117
            ]
118
        );
119
        $this->registerInterceptedRoute(
120
            $router,
121
            'mix',
122
            [
123
                new Interceptor\Append('four'),
124
                new Interceptor\Append('five'),
125
                $pipelineInterceptor,
126
                new Interceptor\Append('six'),
127
            ]
128
        );
129
        $this->registerInterceptedRoute(
130
            $router,
131
            'dup',
132
            [
133
                $pipelineInterceptor,
134
                new Interceptor\Append('one'),
135
                new Interceptor\Append('two'),
136
                new Interceptor\Append('three'),
137
            ]
138
        );
139
        $this->registerInterceptedRoute(
140
            $router,
141
            'skip',
142
            [
143
                new Interceptor\Append('one'),
144
                $pipelineInterceptor,
145
                new Interceptor\Append('two'),
146
                new Interceptor\Append('three'),
147
            ]
148
        );
149
        $this->registerInterceptedRoute(
150
            $router,
151
            'first',
152
            [
153
                $pipelineInterceptor,
154
                new Interceptor\Append('four'),
155
                new Interceptor\Append('five'),
156
                new Interceptor\Append('six'),
157
            ]
158
        );
159
160
        $this->registerInterceptedRoute(
161
            $router,
162
            'withoutAttribute',
163
            [
164
                new Interceptor\Append('one'),
165
                new Interceptor\Append('two'),
166
                new Interceptor\Append('three'),
167
            ]
168
        );
169
170
        $this->registerInterceptedRoute(
171
            $router,
172
            'withAttribute',
173
            [
174
                $pipelineInterceptor,
175
            ]
176
        );
177
        $this->registerInterceptedRoute(
178
            $router,
179
            'mixAttribute',
180
            [
181
                new Interceptor\Append('four'),
182
                new Interceptor\Append('five'),
183
                $pipelineInterceptor,
184
                new Interceptor\Append('six'),
185
            ]
186
        );
187
        $this->registerInterceptedRoute(
188
            $router,
189
            'dupAttribute',
190
            [
191
                $pipelineInterceptor,
192
                new Interceptor\Append('one'),
193
                new Interceptor\Append('two'),
194
                new Interceptor\Append('three'),
195
            ]
196
        );
197
        $this->registerInterceptedRoute(
198
            $router,
199
            'skipAttribute',
200
            [
201
                new Interceptor\Append('one'),
202
                $pipelineInterceptor,
203
                new Interceptor\Append('two'),
204
                new Interceptor\Append('three'),
205
            ]
206
        );
207
        $this->registerInterceptedRoute(
208
            $router,
209
            'firstAttribute',
210
            [
211
                $pipelineInterceptor,
212
                new Interceptor\Append('four'),
213
                new Interceptor\Append('five'),
214
                new Interceptor\Append('six'),
215
            ]
216
        );
217
    }
218
219
    private function registerInterceptedRoute(RouterInterface $router, string $action, array $interceptors): void
220
    {
221
        $target = new Action(InterceptedController::class, $action);
222
        $router->setRoute(
223
            "intercepted:$action",
224
            new Route(
225
                "/intercepted/$action",
226
                $target->withCore($this->getInterceptedCore($interceptors))
227
            )
228
        );
229
    }
230
231
    private function getInterceptedCore(array $interceptors = []): InterceptableCore
232
    {
233
        $core = new InterceptableCore($this->core);
234
235
        foreach ($interceptors as $interceptor) {
236
            $core->addInterceptor(is_object($interceptor) ? $interceptor : $this->container->get($interceptor));
237
        }
238
239
        return $core;
240
    }
241
}
242