Passed
Push — master ( 00b783...b7fb13 )
by Caen
03:02 queued 11s
created

Author   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 96
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A all() 0 3 1
A make() 0 7 2
A get() 0 4 1
A create() 0 5 1
A findUsername() 0 3 1
A __construct() 0 8 3
A __toString() 0 3 1
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * The Post Author Object Model.
9
 *
10
 * @todo Refactor to use same format for create method as constructor
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 URI.
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
    public function __construct(string $username, ?array $data = [])
49
    {
50
        $this->username = $username;
51
        if (isset($data['name'])) {
52
            $this->name = $data['name'];
53
        }
54
        if (isset($data['website'])) {
55
            $this->website = $data['website'];
56
        }
57
    }
58
59
    public function __toString(): string
60
    {
61
        return $this->getName();
62
    }
63
64
    /**
65
     * Get the author's preferred name.
66
     *
67
     * @see \Hyde\Framework\Testing\Unit\AuthorGetNameTest
68
     *
69
     * @return string
70
     */
71
    public function getName(): string
72
    {
73
        return $this->name ?? $this->username;
74
    }
75
76
    public static function create(string $username, ?string $name = null, ?string $website = null): static
77
    {
78
        return new static($username, [
79
            'name' => $name,
80
            'website'=> $website,
81
        ]);
82
    }
83
84
    /** Dynamically get or create an author based on string or front matter array */
85
    public static function make(string|array $data): static
86
    {
87
        if (is_string($data)) {
0 ignored issues
show
introduced by
The condition is_string($data) is always false.
Loading history...
88
            return static::get($data);
89
        }
90
91
        return static::create(static::findUsername($data), $data['name'] ?? null, $data['website'] ?? null);
92
    }
93
94
    public static function all(): Collection
95
    {
96
        return new Collection(config('authors', []));
97
    }
98
99
    public static function get(string $username): static
100
    {
101
        return static::all()->firstWhere('username', $username)
102
            ?? static::create($username);
103
    }
104
105
    protected static function findUsername(array $data): string
106
    {
107
        return $data['username'] ?? $data['name'] ?? 'Guest';
108
    }
109
}
110