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

FindsAuthorForPost   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 33
rs 10
c 3
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findAuthorForPost() 0 17 4
A getUsername() 0 3 1
A __construct() 0 2 1
A run() 0 3 1
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