Passed
Push — master ( 6d6565...6a1810 )
by Caen
03:18 queued 12s
created

ConstructsPageSchemas::constructDocumentationPageSchema()   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 0
dl 0
loc 3
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
13
trait ConstructsPageSchemas
14
{
15
    protected function constructPageSchemas(): void
16
    {
17
        $this->constructPageSchema();
18
19
        if ($this instanceof BlogPostSchema) {
20
            $this->constructBlogPostSchema();
21
        }
22
    }
23
24
    protected function constructPageSchema(): void
25
    {
26
        $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...
27
        $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...
28
29
        if ($this instanceof DocumentationPageSchema) {
30
            $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

30
            $this->/** @scrutinizer ignore-call */ 
31
                   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

30
            $this->/** @scrutinizer ignore-call */ 
31
                   constructSidebarNavigationData();
Loading history...
31
        } else {
32
            $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

32
            $this->/** @scrutinizer ignore-call */ 
33
                   constructNavigationData();
Loading history...
33
        }
34
    }
35
36
    protected function makeCanonicalUrl(): ?string
37
    {
38
        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

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

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