FeaturedImageFactory::make()   A
last analyzed

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 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Factories;
6
7
use Hyde\Hyde;
8
use RuntimeException;
9
use Illuminate\Support\Str;
10
use Hyde\Markdown\Models\FrontMatter;
11
use Hyde\Foundation\Kernel\Hyperlinks;
12
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
13
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema;
14
15
use function str_starts_with;
16
use function is_string;
17
use function Hyde\unslash;
18
19
class FeaturedImageFactory extends Concerns\PageDataFactory implements FeaturedImageSchema
20
{
21
    final public const SCHEMA = FeaturedImageSchema::FEATURED_IMAGE_SCHEMA;
22
23
    protected readonly string $source;
24
    protected readonly ?string $altText;
25
    protected readonly ?string $titleText;
26
    protected readonly ?string $authorName;
27
    protected readonly ?string $authorUrl;
28
    protected readonly ?string $copyrightText;
29
    protected readonly ?string $licenseName;
30
    protected readonly ?string $licenseUrl;
31
    protected readonly ?string $caption;
32
33
    public function __construct(
34
        private readonly FrontMatter $matter,
35
        private readonly ?string $filePath = null,
36
    ) {
37
        $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...
38
        $this->altText = $this->getStringMatter('image.altText') ?? $this->getStringMatter('image.alt');
0 ignored issues
show
Bug introduced by
The property altText is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
39
        $this->titleText = $this->getStringMatter('image.titleText');
0 ignored issues
show
Bug introduced by
The property titleText is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
40
        $this->authorName = $this->getStringMatter('image.authorName');
0 ignored issues
show
Bug introduced by
The property authorName is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
41
        $this->authorUrl = $this->getStringMatter('image.authorUrl');
0 ignored issues
show
Bug introduced by
The property authorUrl is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
42
        $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...
43
        $this->licenseName = $this->getStringMatter('image.licenseName');
0 ignored issues
show
Bug introduced by
The property licenseName is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
44
        $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...
45
        $this->caption = $this->getStringMatter('image.caption');
0 ignored issues
show
Bug introduced by
The property caption is declared read-only in Hyde\Framework\Factories\FeaturedImageFactory.
Loading history...
46
    }
47
48
    /**
49
     * @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, caption: string|null}
50
     */
51
    public function toArray(): array
52
    {
53
        return [
0 ignored issues
show
introduced by
The expression return array('source' =>...ion' => $this->caption) 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...
54
            'source' => $this->source,
55
            'altText' => $this->altText,
56
            'titleText' => $this->titleText,
57
            'authorName' => $this->authorName,
58
            'authorUrl' => $this->authorUrl,
59
            'copyrightText' => $this->copyrightText,
60
            'licenseName' => $this->licenseName,
61
            'licenseUrl' => $this->licenseUrl,
62
            'caption' => $this->caption,
63
        ];
64
    }
65
66
    public static function make(FrontMatter $matter, ?string $filePath = null): FeaturedImage
67
    {
68
        return new FeaturedImage(...(new static($matter, $filePath))->toArray());
69
    }
70
71
    protected function makeSource(): string
72
    {
73
        $value = $this->getStringMatter('image') ?? $this->getStringMatter('image.source');
74
75
        if (empty($value)) {
76
            throw new RuntimeException(sprintf('No featured image source was found in "%s"', $this->filePath ?? 'unknown file'));
77
        }
78
79
        if (Hyperlinks::isRemote($value)) {
80
            return $value;
81
        }
82
83
        return self::normalizeLocalImagePath($value);
84
    }
85
86
    protected static function normalizeLocalImagePath(string $path): string
87
    {
88
        $path = Hyde::pathToRelative($path);
0 ignored issues
show
Bug introduced by
The method pathToRelative() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

88
        /** @scrutinizer ignore-call */ 
89
        $path = Hyde::pathToRelative($path);
Loading history...
89
90
        $path = Str::after($path, Hyde::getMediaDirectory());
0 ignored issues
show
Bug introduced by
The method getMediaDirectory() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

90
        $path = Str::after($path, Hyde::/** @scrutinizer ignore-call */ getMediaDirectory());
Loading history...
91
        $path = Str::after($path, Hyde::getMediaOutputDirectory());
0 ignored issues
show
Bug introduced by
The method getMediaOutputDirectory() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

91
        $path = Str::after($path, Hyde::/** @scrutinizer ignore-call */ getMediaOutputDirectory());
Loading history...
92
93
        return str_starts_with($path, '//') ? $path : unslash($path);
94
    }
95
96
    protected function getStringMatter(string $key): ?string
97
    {
98
        /** @var string|null $value */
99
        $value = $this->matter->get($key);
100
101
        return is_string($value) ? $value : null;
102
    }
103
}
104