Passed
Push — master ( 9b4e67...25f483 )
by Caen
02:57 queued 12s
created

ConfiguresFeaturedImageForPost::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Actions\Constructors;
4
5
use Hyde\Framework\Models\Image;
6
use Hyde\Framework\Models\Pages\MarkdownPost;
7
8
/**
9
 * @internal
10
 *
11
 * @see \Hyde\Framework\Testing\Unit\ConfiguresFeaturedImageForPostTest
12
 */
13
class ConfiguresFeaturedImageForPost
14
{
15
    public static function run(MarkdownPost $page): Image|null
16
    {
17
        return (new static($page))->constructImage();
18
    }
19
20
    protected function __construct(protected MarkdownPost $page)
21
    {
22
    }
23
24
    private function constructImage(): Image|null
25
    {
26
        if ($this->page->matter('image') !== null) {
27
            if (is_string($this->page->matter('image'))) {
28
                return $this->constructBaseImage($this->page->matter('image'));
29
            }
30
            if (is_array($this->page->matter('image'))) {
31
                return new Image($this->page->matter('image'));
32
            }
33
        }
34
35
        return null;
36
    }
37
38
    private function constructBaseImage(string $image): Image
39
    {
40
        return str_starts_with($image, 'http')
41
            ? new Image(['uri' => $image])
42
            : new Image(['path' => $image]);
43
    }
44
}
45