Passed
Push — master ( f2ac3c...abba28 )
by Caen
03:46 queued 12s
created

FeaturedImageFactory::makeCopyrightText()   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
c 0
b 0
f 0
dl 0
loc 3
rs 10
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\Features\Blogging\Models\FeaturedImage;
8
use Hyde\Hyde;
9
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema;
10
use Hyde\Markdown\Models\FrontMatter;
11
use Illuminate\Support\Str;
12
use function is_string;
13
use RuntimeException;
14
use function str_starts_with;
15
16
class FeaturedImageFactory extends Concerns\PageDataFactory implements FeaturedImageSchema
17
{
18
    final public const SCHEMA = FeaturedImageSchema::FEATURED_IMAGE_SCHEMA;
19
20
    protected readonly string $source;
21
    protected readonly ?string $altText;
22
    protected readonly ?string $titleText;
23
    protected readonly ?string $authorName;
24
    protected readonly ?string $authorUrl;
25
    protected readonly ?string $copyrightText;
26
    protected readonly ?string $licenseName;
27
    protected readonly ?string $licenseUrl;
28
29
    public function __construct(
30
        private readonly FrontMatter $matter,
31
    ) {
32
        $this->source = $this->makeSource();
0 ignored issues
show
Bug introduced by
The property source is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
33
        $this->altText = $this->getStringMatter('image.description');
0 ignored issues
show
Bug introduced by
The property altText is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
34
        $this->titleText = $this->getStringMatter('image.title');
0 ignored issues
show
Bug introduced by
The property titleText is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
35
        $this->authorName = $this->getStringMatter('image.author');
0 ignored issues
show
Bug introduced by
The property authorName is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
36
        $this->authorUrl = $this->getStringMatter('image.attributionUrl');
0 ignored issues
show
Bug introduced by
The property authorUrl is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
37
        $this->copyrightText = $this->getStringMatter('image.copyright');
0 ignored issues
show
Bug introduced by
The property copyrightText is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
38
        $this->licenseName = $this->getStringMatter('image.license');
0 ignored issues
show
Bug introduced by
The property licenseName is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
39
        $this->licenseUrl = $this->getStringMatter('image.licenseUrl');
0 ignored issues
show
Bug introduced by
The property licenseUrl is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
40
    }
41
42
    /**
43
     * @return array{source: string, altText: string|null, titleText: string|null, authorName: string|null, authorUrl: string|null, copyrightText: string|null, licenseName: string|null, licenseUrl: string|null}
44
     */
45
    public function toArray(): array
46
    {
47
        return [
0 ignored issues
show
introduced by
The expression return array('source' =>...' => $this->licenseUrl) returns an array which contains values of type string which are incompatible with the return type Illuminate\Contracts\Support\TValue mandated by Illuminate\Contracts\Support\Arrayable::toArray().
Loading history...
48
            'source' => $this->source,
49
            'altText' => $this->altText,
50
            'titleText' => $this->titleText,
51
            'authorName' => $this->authorName,
52
            'authorUrl' => $this->authorUrl,
53
            'copyrightText' => $this->copyrightText,
54
            'licenseName' => $this->licenseName,
55
            'licenseUrl' => $this->licenseUrl,
56
        ];
57
    }
58
59
    public static function make(FrontMatter $matter): FeaturedImage
60
    {
61
        $data = (new static($matter))->toArray();
62
63
        return new FeaturedImage(...$data);
64
    }
65
66
    protected function makeSource(): string
67
    {
68
        $value = $this->getStringMatter('image') ?? $this->getStringMatter('image.source');
69
70
        if (empty($value)) {
71
            // Todo, we might want to add a note about which file caused the error.
72
            // We could also check for these before calling the factory, and just ignore the image if it's not valid.
73
            throw new RuntimeException('No featured image source was found');
74
        }
75
76
        if (FeaturedImage::isRemote($value)) {
77
            return $value;
78
        }
79
80
        return self::normalizeLocalImagePath($value);
81
    }
82
83
    protected static function normalizeLocalImagePath(string $path): string
84
    {
85
        $path = Hyde::pathToRelative($path);
86
87
        $path = Str::after($path, Hyde::getMediaDirectory());
88
        $path = Str::after($path, Hyde::getMediaOutputDirectory());
89
90
        return str_starts_with($path, '//') ? $path : unslash($path);
91
    }
92
93
    protected function getStringMatter(string $key): ?string
94
    {
95
        return is_string($this->matter->get($key)) ? $this->matter->get($key) : null;
96
    }
97
}
98