Passed
Push — master ( 6956bd...dff5c5 )
by Caen
03:50 queued 12s
created

AbstractPage::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Contracts;
4
5
use Hyde\Framework\Concerns\CanBeInNavigation;
6
use Hyde\Framework\Concerns\HasPageMetadata;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Models\Route;
9
use Hyde\Framework\Services\DiscoveryService;
10
use Illuminate\Support\Collection;
11
12
/**
13
 * To ensure compatibility with the Hyde Framework, all Page Models should extend this class.
14
 *
15
 * Markdown-based Pages can extend the AbstractMarkdownPage class to get relevant helpers.
16
 *
17
 * To learn about what the methods do, see the PHPDocs in the PageContract.
18
 *
19
 * @see \Hyde\Framework\Contracts\PageContract
20
 * @see \Hyde\Framework\Contracts\AbstractMarkdownPage
21
 * @test \Hyde\Framework\Testing\Feature\AbstractPageTest
22
 */
23
abstract class AbstractPage implements PageContract
24
{
25
    use HasPageMetadata;
0 ignored issues
show
Bug introduced by
The trait Hyde\Framework\Concerns\HasPageMetadata requires the property $title which is not provided by Hyde\Framework\Contracts\AbstractPage.
Loading history...
26
    use CanBeInNavigation;
0 ignored issues
show
introduced by
The trait Hyde\Framework\Concerns\CanBeInNavigation requires some properties which are not provided by Hyde\Framework\Contracts\AbstractPage: $markdown, $title
Loading history...
27
28
    public static string $sourceDirectory;
29
    public static string $outputDirectory;
30
    public static string $fileExtension;
31
    public static string $parserClass;
32
33
    /** @inheritDoc */
34
    final public static function getSourceDirectory(): string
35
    {
36
        return unslash(static::$sourceDirectory);
37
    }
38
39
    /** @inheritDoc */
40
    final public static function getOutputDirectory(): string
41
    {
42
        return unslash(static::$outputDirectory);
43
    }
44
45
    /** @inheritDoc */
46
    final public static function getFileExtension(): string
47
    {
48
        return '.'.ltrim(static::$fileExtension, '.');
49
    }
50
51
    /** @inheritDoc */
52
    final public static function getParserClass(): string
53
    {
54
        return static::$parserClass;
55
    }
56
57
    /** @inheritDoc */
58
    public static function getParser(string $slug): PageParserContract
59
    {
60
        return new static::$parserClass($slug);
61
    }
62
63
    /** @inheritDoc */
64
    public static function parse(string $slug): static
65
    {
66
        return (new static::$parserClass($slug))->get();
67
    }
68
69
    /** @inheritDoc */
70
    public static function files(): array
71
    {
72
        return DiscoveryService::getSourceFileListForModel(static::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Hyde\Framework\Se...ForModel(static::class) could return the type false which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    /** @inheritDoc */
76
    public static function all(): Collection
77
    {
78
        $collection = new Collection();
79
80
        foreach (static::files() as $basename) {
81
            $collection->push(static::parse($basename));
82
        }
83
84
        return $collection;
85
    }
86
87
    /** @inheritDoc */
88
    public static function qualifyBasename(string $basename): string
89
    {
90
        return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension();
91
    }
92
93
    /** @inheritDoc */
94
    public static function getOutputLocation(string $basename): string
95
    {
96
        // Using the trim function we ensure we don't have a leading slash when the output directory is the root directory.
97
        return trim(
98
            static::getOutputDirectory().'/'.unslash($basename),
99
            '/'
100
        ).'.html';
101
    }
102
103
    public string $slug;
104
105
    /** @inheritDoc */
106
    public function getSourcePath(): string
107
    {
108
        return static::qualifyBasename($this->slug);
109
    }
110
111
    /** @inheritDoc */
112
    public function getOutputPath(): string
113
    {
114
        return static::getCurrentPagePath().'.html';
0 ignored issues
show
Bug Best Practice introduced by
The method Hyde\Framework\Contracts...e::getCurrentPagePath() is not static, but was called statically. ( Ignorable by Annotation )

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

114
        return static::/** @scrutinizer ignore-call */ getCurrentPagePath().'.html';
Loading history...
115
    }
116
117
    /** @inheritDoc */
118
    public function getCurrentPagePath(): string
119
    {
120
        return trim(static::getOutputDirectory().'/'.$this->slug, '/');
121
    }
122
123
    /** @inheritDoc */
124
    public function getRoute(): Route
125
    {
126
        return new Route($this);
127
    }
128
129
    /** @inheritDoc */
130
    public function htmlTitle(?string $title = null): string
131
    {
132
        $pageTitle = $title ?? $this->title ?? null;
133
134
        return $pageTitle
135
            ? config('site.name', 'HydePHP').' - '.$pageTitle
136
            : config('site.name', 'HydePHP');
137
    }
138
}
139