Passed
Push — master ( 246ca5...f72f04 )
by Caen
03:27 queued 14s
created

Author::create()   A

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 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models\Support;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * The Post Author model object.
9
 *
10
 * @see \Hyde\Framework\Testing\Feature\AuthorTest
11
 */
12
class Author implements \Stringable
13
{
14
    /**
15
     * The username of the author.
16
     * This is the key used to find authors in the config.
17
     *
18
     * @var string
19
     */
20
    public string $username;
21
22
    /**
23
     * The display name of the author.
24
     *
25
     * @var string|null
26
     */
27
    public ?string $name = null;
28
29
    /**
30
     * The author's website URL.
31
     *
32
     * Could for example, be a Twitter page, website,
33
     * or a hyperlink to more posts by the author.
34
     *
35
     * @var string|null
36
     */
37
    public ?string $website = null;
38
39
    /**
40
     * Construct a new Author object.
41
     *
42
     * Parameters are supplied through an array to make it
43
     * easy to load data from Markdown post front matter.
44
     *
45
     * @param  string  $username
46
     * @param  array|null  $data
47
     */
48
    final public function __construct(string $username, ?array $data = [])
49
    {
50
        $this->username = $username;
51
52
        if (isset($data['name'])) {
53
            $this->name = $data['name'];
54
        }
55
56
        if (isset($data['website'])) {
57
            $this->website = $data['website'];
58
        }
59
    }
60
61
    /** Dynamically get or create an author based on a username string or front matter array */
62
    final public static function make(string|array $data): static
63
    {
64
        if (is_string($data)) {
0 ignored issues
show
introduced by
The condition is_string($data) is always false.
Loading history...
65
            return static::get($data);
66
        }
67
68
        return static::create(static::findUsername($data), $data['name'] ?? null, $data['website'] ?? null);
69
    }
70
71
    public static function get(string $username): static
72
    {
73
        return static::all()->firstWhere('username', $username) ?? static::create($username);
74
    }
75
76
    public static function all(): Collection
77
    {
78
        return new Collection(config('authors', []));
79
    }
80
81
    public static function create(string $username, ?string $name = null, ?string $website = null): static
82
    {
83
        return new static($username, ['name' => $name, 'website' => $website]);
84
    }
85
86
    public function __toString(): string
87
    {
88
        return $this->getName();
89
    }
90
91
    public function getName(): string
92
    {
93
        return $this->name ?? $this->username;
94
    }
95
96
    protected static function findUsername(array $data): string
97
    {
98
        return $data['username'] ?? $data['name'] ?? 'Guest';
99
    }
100
}
101