Passed
Push — master ( e3b121...219a49 )
by Caen
03:57 queued 14s
created

ManagesHydeKernel::getBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Concerns;
6
7
use BadMethodCallException;
8
use Hyde\Foundation\HydeKernel;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Pages\Contracts\DynamicPage;
11
use function in_array;
12
use InvalidArgumentException;
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 ManagesHydeKernel
21
{
22
    public static function getInstance(): HydeKernel
23
    {
24
        return static::$instance;
25
    }
26
27
    public static function setInstance(HydeKernel $instance): void
28
    {
29
        static::$instance = $instance;
0 ignored issues
show
Bug Best Practice introduced by
The property instance does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
    }
31
32
    public function getBasePath(): string
33
    {
34
        return $this->basePath;
35
    }
36
37
    public function setBasePath(string $basePath): void
38
    {
39
        $this->basePath = rtrim($basePath, '/\\');
0 ignored issues
show
Bug Best Practice introduced by
The property basePath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
    }
41
42
    public function getSourceRoot(): string
43
    {
44
        return $this->sourceRoot;
45
    }
46
47
    public function setSourceRoot(string $sourceRoot): void
48
    {
49
        $this->sourceRoot = $this->normalizeSourcePath($sourceRoot);
0 ignored issues
show
Bug Best Practice introduced by
The property sourceRoot does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
    }
51
52
    public function getOutputDirectory(): string
53
    {
54
        return $this->outputDirectory;
55
    }
56
57
    public function setOutputDirectory(string $outputDirectory): void
58
    {
59
        $this->outputDirectory = $this->normalizeSourcePath($outputDirectory);
0 ignored issues
show
Bug Best Practice introduced by
The property outputDirectory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
    }
61
62
    public function getMediaDirectory(): string
63
    {
64
        return $this->mediaDirectory;
65
    }
66
67
    public function setMediaDirectory(string $mediaDirectory): void
68
    {
69
        $this->mediaDirectory = $this->normalizeSourcePath($mediaDirectory);
0 ignored issues
show
Bug Best Practice introduced by
The property mediaDirectory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
    }
71
72
    public function getMediaOutputDirectory(): string
73
    {
74
        return ltrim($this->getMediaDirectory(), '_');
75
    }
76
77
    /**
78
     * Developer Information.
79
     *
80
     * @experimental This feature is experimental and may change substantially before the 1.0.0 release.
81
     *
82
     * @deprecated This feature may be replaced by the {@see \Hyde\Foundation\Concerns\HydeExtension} system.
83
     *
84
     * If you are a package developer, and want a custom page class to be discovered,
85
     * you'll need to register it sometime before the boot process, before discovery is run.
86
     * Typically, you would do this by calling this method in the register method of a service provider.
87
     * Hyde will then automatically discover source files for the new page class, and compile them during the build process.
88
     *
89
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
90
     */
91
    public function registerPageClass(string $pageClass): void
92
    {
93
        if ($this->booted) {
94
            // We throw an exception here to prevent the developer from registering a page class after the Kernel has been booted.
95
            // The reason we do this is because at this point all the source files have already been discovered and parsed.
96
            // If we allowed new classes after this point, we would have to reboot everything which adds complexity.
97
98
            throw new BadMethodCallException('Cannot register a page class after the Kernel has been booted.');
99
        }
100
101
        if (! is_subclass_of($pageClass, HydePage::class)) {
102
            throw new InvalidArgumentException('The specified class must be a subclass of HydePage.');
103
        }
104
105
        if (is_subclass_of($pageClass, DynamicPage::class)) {
106
            throw new InvalidArgumentException('The specified class must not be a subclass of DynamicPage.');
107
        }
108
109
        if (! in_array($pageClass, $this->pageClasses, true)) {
110
            $this->pageClasses[] = $pageClass;
0 ignored issues
show
Bug Best Practice introduced by
The property pageClasses does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
111
        }
112
    }
113
114
    /** @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...
115
    public function getRegisteredPageClasses(): array
116
    {
117
        return $this->pageClasses;
118
    }
119
120
    /** @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...
121
    public function registerExtension(string $extension): void
122
    {
123
        if (! in_array($extension, $this->extensions, true)) {
124
            $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...
125
        }
126
    }
127
128
    /** @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...
129
    public function getRegisteredExtensions(): array
130
    {
131
        return $this->extensions;
132
    }
133
134
    protected function normalizeSourcePath(string $outputDirectory): string
135
    {
136
        return $this->pathToRelative(rtrim($outputDirectory, '/\\'));
0 ignored issues
show
Bug introduced by
It seems like pathToRelative() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
        return $this->/** @scrutinizer ignore-call */ pathToRelative(rtrim($outputDirectory, '/\\'));
Loading history...
137
    }
138
}
139