Passed
Push — master ( 963312...34e999 )
by Caen
03:12 queued 12s
created

ManagesExtensions   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 46
rs 10
c 5
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegisteredExtensions() 0 3 1
A registerExtension() 0 19 4
A getRegisteredPageClasses() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Concerns;
6
7
use BadMethodCallException;
8
use InvalidArgumentException;
9
use function array_map;
10
use function array_merge;
11
use function array_unique;
12
use function in_array;
13
use function is_subclass_of;
14
15
/**
16
 * @internal Single-use trait for the HydeKernel class.
17
 *
18
 * @see \Hyde\Foundation\HydeKernel
19
 */
20
trait ManagesExtensions
21
{
22
    /**
23
     * Register a HydePHP extension within the HydeKernel.
24
     *
25
     * Typically, you would call this method in the register method of a service provider.
26
     * If your package uses the standard Laravel (Composer) package discovery feature,
27
     * the extension will automatically be enabled when the package is installed.
28
     *
29
     * @param  class-string<\Hyde\Foundation\Concerns\HydeExtension>  $extension
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Found...Concerns\HydeExtension> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Foundation\Concerns\HydeExtension>.
Loading history...
30
     */
31
    public function registerExtension(string $extension): void
32
    {
33
        if ($this->booted) {
34
            // We throw an exception here to prevent the developer from registering aa extension after the Kernel has been booted.
35
            // The reason we do this is because at this point all the source files have already been discovered and parsed.
36
            // If we allowed new classes after this point, we would have to reboot everything which adds complexity.
37
38
            throw new BadMethodCallException('Cannot register an extension after the Kernel has been booted.');
39
        }
40
41
        if (! is_subclass_of($extension, HydeExtension::class)) {
42
            // We want to make sure that the extension class extends the HydeExtension class,
43
            // so that we won't have to check the methods we need to call exist later on.
44
45
            throw new InvalidArgumentException('The specified class must extend the HydeExtension class.');
46
        }
47
48
        if (! in_array($extension, $this->extensions, true)) {
49
            $this->extensions[] = $extension;
0 ignored issues
show
Bug Best Practice introduced by
The property extensions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
        }
51
    }
52
53
    /** @return array<class-string<\Hyde\Foundation\Concerns\HydeExtension>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<\Hyde...oncerns\HydeExtension>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<\Hyde\Foundation\Concerns\HydeExtension>>.
Loading history...
54
    public function getRegisteredExtensions(): array
55
    {
56
        return $this->extensions;
57
    }
58
59
    /** @return array<class-string<\Hyde\Pages\Concerns\HydePage>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<\Hyde...ges\Concerns\HydePage>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<\Hyde\Pages\Concerns\HydePage>>.
Loading history...
60
    public function getRegisteredPageClasses(): array
61
    {
62
        return array_unique(array_merge(...array_map(function (string $extension): array {
63
            /** @var <class-string<\Hyde\Foundation\Concerns\HydeExtension>> $extension */
64
            return $extension::getPageClasses();
65
        }, $this->getRegisteredExtensions())));
66
    }
67
}
68