Passed
Push — master ( e734d9...9b4e67 )
by Caen
05:15 queued 02:36
created

Author::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * The Post Author Object Model.
9
 */
10
class Author
11
{
12
    /**
13
     * The username of the author.
14
     * This is the key used to find authors in the config.
15
     *
16
     * @var string
17
     */
18
    public string $username;
19
20
    /**
21
     * The display name of the author.
22
     *
23
     * @var string|null
24
     */
25
    public ?string $name = null;
26
27
    /**
28
     * The author's website URI.
29
     *
30
     * Could for example, be a Twitter page, website,
31
     * or a hyperlink to more posts by the author.
32
     *
33
     * @var string|null
34
     */
35
    public ?string $website = null;
36
37
    /**
38
     * Construct a new Author object.
39
     *
40
     * Parameters are supplied through an array to make it
41
     * easy to load data from Markdown post front matter.
42
     *
43
     * @param  string  $username
44
     * @param  array|null  $data
45
     */
46
    public function __construct(string $username, ?array $data = [])
47
    {
48
        $this->username = $username;
49
        if (isset($data['name'])) {
50
            $this->name = $data['name'];
51
        }
52
        if (isset($data['website'])) {
53
            $this->website = $data['website'];
54
        }
55
    }
56
57
    /**
58
     * Get the author's preferred name.
59
     *
60
     * @see \Hyde\Framework\Testing\Unit\AuthorGetNameTest
61
     *
62
     * @return string
63
     */
64
    public function getName(): string
65
    {
66
        return $this->name ?? $this->username;
67
    }
68
69
    public static function create(string $username, ?string $name = null, ?string $website = null): static
70
    {
71
        return new static($username, [
72
            'name' => $name,
73
            'website'=> $website,
74
        ]);
75
    }
76
77
    public static function all(): Collection
78
    {
79
        return new Collection(config('authors', []));
80
    }
81
82
    public static function get(string $username): static
83
    {
84
        return static::all()->firstWhere('username', $username)
85
            ?? static::create($username);
86
    }
87
}
88