Passed
Branch decouple-schema-constructors (79d259)
by Caen
02:51
created

ConstructsPageSchemas::makeCanonicalUrl()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Hyde\Framework\Concerns\FrontMatter\Schemas\Constructors;
4
5
use Hyde\Framework\Actions\Constructors\FindsNavigationDataForPage;
6
use Hyde\Framework\Actions\Constructors\FindsTitleForPage;
7
use Hyde\Framework\Concerns\FrontMatter\Schemas\BlogPostSchema;
8
use Hyde\Framework\Concerns\FrontMatter\Schemas\DocumentationPageSchema;
9
use Hyde\Framework\Concerns\FrontMatter\Schemas\PageSchema;
10
use Hyde\Framework\Hyde;
11
use Hyde\Framework\Models\Author;
12
use Hyde\Framework\Models\DateString;
13
use Hyde\Framework\Models\Image;
14
use Illuminate\Support\Str;
15
16
trait ConstructsPageSchemas
17
{
18
    protected function constructPageSchemas(): void
19
    {
20
        if ($this->usesSchema(PageSchema::class)) {
21
            $this->constructPageSchema();
22
        }
23
24
        if ($this->usesSchema(BlogPostSchema::class)) {
25
            $this->constructBlogPostSchema();
26
        }
27
28
        if ($this->usesSchema(DocumentationPageSchema::class)) {
29
            $this->constructDocumentationPageSchema();
30
        }
31
    }
32
33
    protected function constructPageSchema(): void
34
    {
35
        $this->title = FindsTitleForPage::run($this);
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
        $this->navigation = FindsNavigationDataForPage::run($this);
0 ignored issues
show
Bug Best Practice introduced by
The property navigation does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $this->canonicalUrl = $this->makeCanonicalUrl();
0 ignored issues
show
Bug Best Practice introduced by
The property canonicalUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
    }
39
40
    protected function makeCanonicalUrl(): ?string
41
    {
42
        if (! empty($this->matter('canonicalUrl'))) {
0 ignored issues
show
Bug introduced by
It seems like matter() 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

42
        if (! empty($this->/** @scrutinizer ignore-call */ matter('canonicalUrl'))) {
Loading history...
43
            return $this->matter('canonicalUrl');
44
        }
45
46
        if (Hyde::hasSiteUrl() && ! empty($this->identifier)) {
47
            return $this->getRoute()->getQualifiedUrl();
0 ignored issues
show
Bug introduced by
It seems like getRoute() 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

47
            return $this->/** @scrutinizer ignore-call */ getRoute()->getQualifiedUrl();
Loading history...
48
        }
49
50
        return null;
51
    }
52
53
    protected function constructBlogPostSchema(): void
54
    {
55
        $this->category = $this->matter('category');
0 ignored issues
show
Bug Best Practice introduced by
The property category does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
        $this->description = $this->matter('description', $this->makeDescription());
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57
        $this->date = $this->matter('date') !== null ? new DateString($this->matter('date')) : null;
0 ignored issues
show
Bug Best Practice introduced by
The property date does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
        $this->author = $this->getAuthor();
0 ignored issues
show
Bug Best Practice introduced by
The property author does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
        $this->image = $this->getImage();
0 ignored issues
show
Bug Best Practice introduced by
The property image does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
    }
61
62
    protected function makeDescription(): string
63
    {
64
        if (strlen($this->markdown) >= 128) {
65
            return substr($this->markdown, 0, 125).'...';
66
        }
67
68
        return (string) $this->markdown;
69
    }
70
71
    protected function getAuthor(): ?Author
72
    {
73
        if ($this->matter('author')) {
74
            return Author::make($this->matter('author'));
75
        }
76
77
        return null;
78
    }
79
80
    protected function getImage(): ?Image
81
    {
82
        if ($this->matter('image')) {
83
            return Image::make($this->matter('image'));
84
        }
85
86
        return null;
87
    }
88
89
    protected function constructDocumentationPageSchema(): void
90
    {
91
        $this->category = $this->getDocumentationPageCategory();
0 ignored issues
show
Bug Best Practice introduced by
The property category does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
92
93
        $this->label = $this->matter('label', Hyde::makeTitle(basename($this->identifier)));
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
94
        $this->hidden = $this->matter('hidden', $this->identifier === 'index');
0 ignored issues
show
Bug Best Practice introduced by
The property hidden does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
95
        $this->priority = $this->matter('priority', $this->findPriorityInConfig());
0 ignored issues
show
Bug Best Practice introduced by
The property priority does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
96
    }
97
98
    protected function getDocumentationPageCategory(): ?string
99
    {
100
        // If the documentation page is in a subdirectory,
101
        // then we can use that as the category name.
102
        // Otherwise, we look in the front matter.
103
104
        return str_contains($this->identifier, '/')
105
            ? Str::before($this->identifier, '/')
106
            : $this->matter('category', 'other');
107
    }
108
109
    protected function findPriorityInConfig(): int
110
    {
111
        $orderIndexArray = config('docs.sidebar_order', []);
112
113
        if (! in_array($this->identifier, $orderIndexArray)) {
114
            return 500;
115
        }
116
117
        return array_search($this->identifier, $orderIndexArray) + 250;
118
119
        // Adding 250 makes so that pages with a front matter priority that is lower
120
        // can be shown first. It's lower than the fallback of 500 so that they
121
        // still come first. This is all to make it easier to mix priorities.
122
    }
123
124
    protected function usesSchema(string $schema): bool
125
    {
126
        return in_array($schema, class_uses_recursive($this));
127
    }
128
}
129