Test Failed
Push — master ( a309d2...0e4b58 )
by butschster
17:34 queued 09:02
created

BootloaderDeclaration   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 22
dl 0
loc 50
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getInstructions() 0 7 1
A declare() 0 23 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Scaffolder\Declaration;
6
7
use Nette\PhpGenerator\Literal;
8
use Spiral\Boot\Bootloader\Bootloader;
9
use Spiral\Boot\BootloadManager\Methods;
10
use Spiral\Boot\KernelInterface;
11
use Spiral\Bootloader\DomainBootloader;
12
use Spiral\Core\CoreInterface;
13
use Spiral\Scaffolder\Config\ScaffolderConfig;
14
15
class BootloaderDeclaration extends AbstractDeclaration implements HasInstructions
16
{
17
    public const TYPE = 'bootloader';
18 3
19
    public function __construct(
20
        private readonly KernelInterface $kernel,
21
        ScaffolderConfig $config,
22
        string $name,
23
        ?string $comment = null,
24
        ?string $namespace = null,
25 3
        private readonly bool $isDomain = false,
26
    ) {
27
        parent::__construct($config, $name, $comment, $namespace);
28
    }
29
30
    /**
31 3
     * Declare constants and boot method.
32
     */
33 3
    public function declare(): void
34
    {
35 3
        $extends = $this->isDomain ? DomainBootloader::class : Bootloader::class;
36 3
37
        $this->namespace->addUse($extends);
38 3
        $this->class->setExtends($extends);
39
40 3
        $this->class->setFinal();
41 3
42 3
        $this->class->addConstant('BINDINGS', [])->setProtected();
43
        $this->class->addConstant('SINGLETONS', [])->setProtected();
44 3
        $this->class->addConstant('DEPENDENCIES', [])->setProtected();
45 1
46 1
        if ($this->isDomain) {
47 1
            $this->class->addConstant('INTERCEPTORS', [])->setProtected();
48 1
            $this->namespace->addUse(CoreInterface::class);
49 1
            $this->class->getConstant('SINGLETONS')->setValue([
50
                new Literal('CoreInterface::class => [self::class, \'domainCore\']'),
51
            ]);
52 3
        }
53 3
54
        $this->class->addMethod(Methods::INIT->value)->setReturnType('void');
55
        $this->class->addMethod(Methods::BOOT->value)->setReturnType('void');
56
    }
57
58
    public function getInstructions(): array
59
    {
60
        $kernelClass = (new \ReflectionClass($this->kernel))->getName();
61
62
        return [
63
            \sprintf('Don\'t forget to add your bootloader to the bootloader\'s list in \'<comment>%s</comment>\' class', $kernelClass),
64
            'Read more about bootloaders in the documentation: https://spiral.dev/docs/framework-bootloaders',
65
        ];
66
    }
67
}
68