Passed
Pull Request — master (#840)
by Maxim
12:40 queued 05:56
created

DefaultStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Boot\BootloadManager;
6
7
use Spiral\Boot\Bootloader\BootloaderInterface;
8
use Spiral\Core\InvokerInterface;
9
use Spiral\Core\ResolverInterface;
10
11
final class DefaultStrategy implements InvokerStrategyInterface
12
{
13 342
    public function __construct(
14
        private readonly InitializerInterface $initializer,
15
        private readonly InvokerInterface $invoker,
16
        private readonly ResolverInterface $resolver
17
    ) {
18
    }
19
20 341
    public function invokeBootloaders(array $classes, array $bootingCallbacks, array $bootedCallbacks): void
21
    {
22 341
        $bootloaders = \iterator_to_array($this->initializer->init($classes));
23
24 340
        foreach ($bootloaders as $data) {
25 340
            $this->invokeBootloader($data['bootloader'], Methods::INIT, $data['options']);
26
        }
27
28 340
        $this->fireCallbacks($bootingCallbacks);
29
30 340
        foreach ($bootloaders as $data) {
31 340
            $this->invokeBootloader($data['bootloader'], Methods::BOOT, $data['options']);
32
        }
33
34 340
        $this->fireCallbacks($bootedCallbacks);
35
    }
36
37 340
    private function invokeBootloader(BootloaderInterface $bootloader, Methods $method, array $options): void
38
    {
39 340
        $refl = new \ReflectionClass($bootloader);
40 340
        if (!$refl->hasMethod($method->value)) {
41 339
            return;
42
        }
43
44 334
        $method = $refl->getMethod($method->value);
45
46 334
        $args = $this->resolver->resolveArguments($method);
47 334
        if (!isset($args['boot'])) {
48 334
            $args['boot'] = $options;
49
        }
50
51 334
        $method->invokeArgs($bootloader, \array_values($args));
52
    }
53
54
    /**
55
     * @param array<Closure> $callbacks
56
     */
57 340
    private function fireCallbacks(array $callbacks): void
58
    {
59 340
        foreach ($callbacks as $callback) {
60 330
            $this->invoker->invoke($callback);
61
        }
62
    }
63
}
64