Passed
Push — master ( 4b1ce1...274e82 )
by Caen
03:37 queued 14s
created

BlogPostDataFactory::makeCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Factories;
6
7
use Hyde\Framework\Factories\Concerns\CoreDataObject;
8
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
9
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
10
use Hyde\Markdown\Contracts\FrontMatter\BlogPostSchema;
11
use Hyde\Markdown\Models\FrontMatter;
12
use Hyde\Markdown\Models\Markdown;
13
use Hyde\Support\Models\DateString;
14
15
use function strlen;
16
use function substr;
17
18
/**
19
 * Streamlines the data construction specific to a blog post.
20
 *
21
 * Simply pass along the data the class needs to run, then access the data using the toArray() method.
22
 *
23
 * All data can be set using front matter in the page source file. If no front matter is set for the given key,
24
 * this class will attempt to generate and discover the values based on the page and the project's configuration.
25
 */
26
class BlogPostDataFactory extends Concerns\PageDataFactory implements BlogPostSchema
27
{
28
    /**
29
     * The front matter properties supported by this factory.
30
     *
31
     * Note that this class does not add the title, as that is already added to all pages.
32
     */
33
    final public const SCHEMA = BlogPostSchema::BLOG_POST_SCHEMA;
34
35
    private readonly FrontMatter $matter;
36
    private readonly Markdown $markdown;
37
38
    protected readonly ?string $description;
39
    protected readonly ?string $category;
40
    protected readonly ?DateString $date;
41
    protected readonly ?PostAuthor $author;
42
    protected readonly ?FeaturedImage $image;
43
44
    public function __construct(CoreDataObject $pageData)
45
    {
46
        $this->matter = $pageData->matter;
0 ignored issues
show
Bug introduced by
The property matter is declared read-only in Hyde\Framework\Factories\BlogPostDataFactory.
Loading history...
47
        $this->markdown = $pageData->markdown;
0 ignored issues
show
Bug introduced by
The property markdown is declared read-only in Hyde\Framework\Factories\BlogPostDataFactory.
Loading history...
48
49
        $this->description = $this->makeDescription();
0 ignored issues
show
Bug introduced by
The property description is declared read-only in Hyde\Framework\Factories\BlogPostDataFactory.
Loading history...
50
        $this->category = $this->makeCategory();
0 ignored issues
show
Bug introduced by
The property category is declared read-only in Hyde\Framework\Factories\BlogPostDataFactory.
Loading history...
51
        $this->date = $this->makeDate();
0 ignored issues
show
Bug introduced by
The property date is declared read-only in Hyde\Framework\Factories\BlogPostDataFactory.
Loading history...
52
        $this->author = $this->makeAuthor();
0 ignored issues
show
Bug introduced by
The property author is declared read-only in Hyde\Framework\Factories\BlogPostDataFactory.
Loading history...
53
        $this->image = $this->makeImage();
0 ignored issues
show
Bug introduced by
The property image is declared read-only in Hyde\Framework\Factories\BlogPostDataFactory.
Loading history...
54
    }
55
56
    /**
57
     * @return array{description: string|null, category: string|null, date: \Hyde\Support\Models\DateString|null, author: \Hyde\Framework\Features\Blogging\Models\PostAuthor|null, image: \Hyde\Framework\Features\Blogging\Models\FeaturedImage|null}
58
     */
59
    public function toArray(): array
60
    {
61
        return [
62
            'description' => $this->description,
63
            'category' => $this->category,
64
            'date' => $this->date,
65
            'author' => $this->author,
66
            'image' => $this->image,
67
        ];
68
    }
69
70
    protected function makeDescription(): string
71
    {
72
        return $this->getMatter('description') ?? $this->getTruncatedMarkdown($this->markdown->body());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMatter(...this->markdown->body()) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    protected function makeCategory(): ?string
76
    {
77
        return $this->getMatter('category');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMatter('category') could return the type array which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
78
    }
79
80
    protected function makeDate(): ?DateString
81
    {
82
        if ($this->getMatter('date')) {
83
            return new DateString($this->getMatter('date'));
0 ignored issues
show
Bug introduced by
It seems like $this->getMatter('date') can also be of type array and null; however, parameter $string of Hyde\Support\Models\DateString::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

83
            return new DateString(/** @scrutinizer ignore-type */ $this->getMatter('date'));
Loading history...
84
        }
85
86
        return null;
87
    }
88
89
    protected function makeAuthor(): ?PostAuthor
90
    {
91
        if ($this->getMatter('author')) {
92
            return PostAuthor::getOrCreate($this->getMatter('author'));
0 ignored issues
show
Bug introduced by
It seems like $this->getMatter('author') can also be of type null; however, parameter $data of Hyde\Framework\Features\...stAuthor::getOrCreate() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

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

92
            return PostAuthor::getOrCreate(/** @scrutinizer ignore-type */ $this->getMatter('author'));
Loading history...
93
        }
94
95
        return null;
96
    }
97
98
    protected function makeImage(): ?FeaturedImage
99
    {
100
        if ($this->getMatter('image')) {
101
            return FeaturedImageFactory::make($this->matter);
102
        }
103
104
        return null;
105
    }
106
107
    private function getTruncatedMarkdown(string $markdown): string
108
    {
109
        if (strlen($markdown) >= 128) {
110
            return substr($markdown, 0, 125).'...';
111
        }
112
113
        return $markdown;
114
    }
115
116
    protected function getMatter(string $key): string|null|array
117
    {
118
        return $this->matter->get($key);
119
    }
120
}
121