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

FindsAuthorForPost::__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\Author;
6
use Hyde\Framework\Models\Pages\MarkdownPost;
7
8
/**
9
 * @internal
10
 */
11
class FindsAuthorForPost
12
{
13
    public static function run(MarkdownPost $page): Author|null
14
    {
15
        return (new static($page))->findAuthorForPost();
16
    }
17
18
    protected function __construct(protected MarkdownPost $page)
19
    {
20
    }
21
22
    protected function findAuthorForPost(): Author|null
23
    {
24
        if ($this->page->matter('author') !== null) {
25
            if (is_string($this->page->matter('author'))) {
26
                // If the author is a string, we assume it's a username,
27
                // so we'll try to find the author in the config
28
                return Author::get($this->page->matter('author'));
29
            }
30
            if (is_array($this->page->matter('author'))) {
31
                // If the author is an array, we'll assume it's a user
32
                // with one-off custom data, so we create a new author.
33
                // In the future we may want to merge config data with custom data
34
                return new Author($this->getUsername(), $this->page->matter('author'));
35
            }
36
        }
37
38
        return null;
39
    }
40
41
    protected function getUsername(): string
42
    {
43
        return $this->page->matter('author')['username'] ?? $this->page->matter('author')['name'] ?? 'Guest';
44
    }
45
}
46