Passed
Push — master ( 421d3e...530cc9 )
by Caen
04:53 queued 19s
created

BlogPostDataFactory::stripMarkdownFromBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

85
            return new DateString(/** @scrutinizer ignore-type */ $this->getMatter('date'));
Loading history...
86
        }
87
88
        return null;
89
    }
90
91
    protected function makeAuthor(): ?PostAuthor
92
    {
93
        if ($this->getMatter('author')) {
94
            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

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