Passed
Push — master ( d2630b...9dacbc )
by Caen
09:17 queued 06:37
created

ConstructsPageSchemas::makeDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Concerns\Internal;
4
5
use Hyde\Framework\Actions\Constructors\FindsNavigationDataForPage;
6
use Hyde\Framework\Actions\Constructors\FindsTitleForPage;
7
use Hyde\Framework\Contracts\FrontMatter\BlogPostSchema;
8
use Hyde\Framework\Contracts\FrontMatter\DocumentationPageSchema;
9
use Hyde\Framework\Hyde;
10
use Hyde\Framework\Models\Author;
11
use Hyde\Framework\Models\DateString;
12
use Hyde\Framework\Models\Image;
13
use Illuminate\Support\Str;
14
15
trait ConstructsPageSchemas
16
{
17
    protected function constructPageSchemas(): void
18
    {
19
        $this->constructPageSchema();
20
21
        if ($this instanceof BlogPostSchema) {
22
            $this->constructBlogPostSchema();
23
        }
24
25
        if ($this instanceof DocumentationPageSchema) {
26
            $this->constructDocumentationPageSchema();
27
        }
28
    }
29
30
    protected function constructPageSchema(): void
31
    {
32
        $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...
33
        $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...
34
        $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...
35
    }
36
37
    protected function makeCanonicalUrl(): ?string
38
    {
39
        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

39
        if (! empty($this->/** @scrutinizer ignore-call */ matter('canonicalUrl'))) {
Loading history...
40
            return $this->matter('canonicalUrl');
41
        }
42
43
        if (Hyde::hasSiteUrl() && ! empty($this->identifier)) {
44
            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

44
            return $this->/** @scrutinizer ignore-call */ getRoute()->getQualifiedUrl();
Loading history...
45
        }
46
47
        return null;
48
    }
49
50
    protected function constructBlogPostSchema(): void
51
    {
52
        $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...
53
        $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...
54
        $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...
55
        $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...
56
        $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...
57
    }
58
59
    protected function makeDescription(): string
60
    {
61
        if (strlen($this->markdown) >= 128) {
62
            return substr($this->markdown, 0, 125).'...';
63
        }
64
65
        return (string) $this->markdown;
66
    }
67
68
    protected function getAuthor(): ?Author
69
    {
70
        if ($this->matter('author')) {
71
            return Author::make($this->matter('author'));
72
        }
73
74
        return null;
75
    }
76
77
    protected function getImage(): ?Image
78
    {
79
        if ($this->matter('image')) {
80
            return Image::make($this->matter('image'));
81
        }
82
83
        return null;
84
    }
85
86
    protected function constructDocumentationPageSchema(): void
87
    {
88
        $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...
89
90
        $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...
91
        $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...
92
        $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...
93
    }
94
95
    protected function getDocumentationPageCategory(): ?string
96
    {
97
        // If the documentation page is in a subdirectory,
98
        // then we can use that as the category name.
99
        // Otherwise, we look in the front matter.
100
101
        return str_contains($this->identifier, '/')
102
            ? Str::before($this->identifier, '/')
103
            : $this->matter('category', 'other');
104
    }
105
106
    protected function findPriorityInConfig(): int
107
    {
108
        $orderIndexArray = config('docs.sidebar_order', []);
109
110
        if (! in_array($this->identifier, $orderIndexArray)) {
111
            return 500;
112
        }
113
114
        return array_search($this->identifier, $orderIndexArray) + 250;
115
116
        // Adding 250 makes so that pages with a front matter priority that is lower
117
        // can be shown first. It's lower than the fallback of 500 so that they
118
        // still come first. This is all to make it easier to mix priorities.
119
    }
120
}
121