Passed
Push — master ( 00b783...b7fb13 )
by Caen
03:02 queued 11s
created

BlogPostSchema::makeDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Hyde\Framework\Concerns\FrontMatter\Schemas;
4
5
use Hyde\Framework\Models\Author;
6
use Hyde\Framework\Models\DateString;
7
use Hyde\Framework\Models\Image;
8
9
trait BlogPostSchema
10
{
11
    /** @example "My New Post" */
12
    public string $title;
13
14
    /** @example "A short description" */
15
    public ?string $description = null;
16
17
    /** @example "general", "my favorite recipes" */
18
    public ?string $category = null;
19
20
    /**
21
     * The date the post was published.
22
     *
23
     * @example 'YYYY-MM-DD [HH:MM]' (Must be parsable by `strtotime()`)
24
     * @yamlType string|optional
25
     */
26
    public ?DateString $date = null;
27
28
    /**
29
     * @example See author section
30
     * @yamlType string|array|optional
31
     */
32
    public ?Author $author = null;
33
34
    /**
35
     * @yamlType string|array|optional
36
     *
37
     * @example "image.jpg" # Expanded by Hyde to `_media/image.jpg` and is resolved automatically
38
     * @example "https://cdn.example.com/image.jpg" # Full URL starting with `http(s)://`)
39
     * @example ```yaml
40
     * image:
41
     *   path: image.jpg
42
     *   uri: https://cdn.example.com/image.jpg # Takes precedence over `path`
43
     *   description: 'Alt text for image'
44
     *   title: 'Tooltip title'
45
     *   copyright: 'Copyright (c) 2022'
46
     *   license: 'CC-BY-SA-4.0'
47
     *   licenseUrl: https://example.com/license/
48
     *   credit: https://photographer.example.com/
49
     *   author: 'John Doe'
50
     * ```
51
     */
52
    public ?Image $image = null;
53
54
    protected function constructBlogPostSchema(): void
55
    {
56
        $this->category = $this->matter('category');
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

56
        /** @scrutinizer ignore-call */ 
57
        $this->category = $this->matter('category');
Loading history...
57
        $this->description = $this->matter('description', $this->makeDescription());
58
        $this->date = $this->matter('date') !== null ? new DateString($this->matter('date')) : null;
59
        $this->author = $this->getAuthor();
60
        $this->image = $this->getImage();
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 $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