Passed
Push — master ( 32a4a2...da9bd5 )
by Caen
04:12 queued 14s
created

PostAuthor::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Blogging\Models;
6
7
use Stringable;
8
use Hyde\Facades\Author;
9
use Hyde\Facades\Config;
10
use Illuminate\Support\Collection;
11
use function is_string;
12
13
/**
14
 * The Post Author model object.
15
 *
16
 * @see \Hyde\Framework\Testing\Unit\PostAuthorTest
17
 */
18
class PostAuthor implements Stringable
19
{
20
    /**
21
     * The username of the author.
22
     * This is the key used to find authors in the config.
23
     */
24
    public readonly string $username;
25
26
    /**
27
     * The display name of the author.
28
     */
29
    public readonly ?string $name;
30
31
    /**
32
     * The author's website URL.
33
     *
34
     * Could for example, be a Twitter page, website, or a hyperlink to more posts by the author.
35
     * Should be a fully qualified link, meaning it starts with http:// or https://.
36
     */
37
    public readonly ?string $website;
38
39
    /**
40
     * Construct a new Post Author object.
41
     *
42
     * If your input is in the form of an array, you may rather want to use the `getOrCreate` method.
43
     *
44
     * @param  string  $username
45
     * @param  string|null  $name
46
     * @param  string|null  $website
47
     */
48
    public function __construct(string $username, ?string $name = null, ?string $website = null)
49
    {
50
        $this->username = $username;
0 ignored issues
show
Bug introduced by
The property username is declared read-only in Hyde\Framework\Features\Blogging\Models\PostAuthor.
Loading history...
51
        $this->name = $name;
0 ignored issues
show
Bug introduced by
The property name is declared read-only in Hyde\Framework\Features\Blogging\Models\PostAuthor.
Loading history...
52
        $this->website = $website;
0 ignored issues
show
Bug introduced by
The property website is declared read-only in Hyde\Framework\Features\Blogging\Models\PostAuthor.
Loading history...
53
    }
54
55
    /** Dynamically get or create an author based on a username string or front matter array */
56
    public static function getOrCreate(string|array $data): static
57
    {
58
        if (is_string($data)) {
0 ignored issues
show
introduced by
The condition is_string($data) is always false.
Loading history...
59
            return static::get($data);
60
        }
61
62
        return Author::create(static::findUsername($data), $data['name'] ?? null, $data['website'] ?? null);
63
    }
64
65
    /** Get an Author from the config, or create it. */
66
    public static function get(string $username): static
67
    {
68
        return static::all()->firstWhere('username', $username) ?? Author::create($username);
69
    }
70
71
    /** @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Blogging\Models\PostAuthor> */
72
    public static function all(): Collection
73
    {
74
        return new Collection(Config::getArray('hyde.authors', []));
0 ignored issues
show
Bug introduced by
Hyde\Facades\Config::get...hyde.authors', array()) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        return new Collection(/** @scrutinizer ignore-type */ Config::getArray('hyde.authors', []));
Loading history...
75
    }
76
77
    public function __toString(): string
78
    {
79
        return $this->getName();
80
    }
81
82
    public function getName(): string
83
    {
84
        return $this->name ?? $this->username;
85
    }
86
87
    protected static function findUsername(array $data): string
88
    {
89
        return $data['username'] ?? $data['name'] ?? 'Guest';
90
    }
91
}
92