Passed
Push — master ( 75f29e...00dcc0 )
by Caen
03:06 queued 13s
created

ConstructsPageSchemas::findPriorityInConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

35
            $this->/** @scrutinizer ignore-call */ 
36
                   constructSidebarNavigationData();
Loading history...
Bug introduced by
The method constructSidebarNavigationData() does not exist on Hyde\Framework\Contracts...DocumentationPageSchema. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts...DocumentationPageSchema. ( Ignorable by Annotation )

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

35
            $this->/** @scrutinizer ignore-call */ 
36
                   constructSidebarNavigationData();
Loading history...
36
        } else {
37
            $this->constructNavigationData();
0 ignored issues
show
Bug introduced by
It seems like constructNavigationData() 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

37
            $this->/** @scrutinizer ignore-call */ 
38
                   constructNavigationData();
Loading history...
38
        }
39
    }
40
41
    protected function makeCanonicalUrl(): ?string
42
    {
43
        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

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

48
            return $this->/** @scrutinizer ignore-call */ getRoute()->getQualifiedUrl();
Loading history...
49
        }
50
51
        return null;
52
    }
53
54
    protected function constructBlogPostSchema(): void
55
    {
56
        $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...
57
        $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...
58
        $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...
59
        $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...
60
        $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...
61
    }
62
63
    protected function makeDescription(): string
64
    {
65
        if (strlen($this->markdown) >= 128) {
66
            return substr($this->markdown, 0, 125).'...';
67
        }
68
69
        return (string) $this->markdown;
70
    }
71
72
    protected function getAuthor(): ?Author
73
    {
74
        if ($this->matter('author')) {
75
            return Author::make($this->matter('author'));
76
        }
77
78
        return null;
79
    }
80
81
    protected function getImage(): ?Image
82
    {
83
        if ($this->matter('image')) {
84
            return Image::make($this->matter('image'));
85
        }
86
87
        return null;
88
    }
89
90
    protected function constructDocumentationPageSchema(): void
91
    {
92
        $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...
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