Completed
Pull Request — master (#24)
by Anton
02:57 queued 39s
created

ReactionType   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 28
dl 0
loc 79
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A fromName() 0 17 3
A reactions() 0 3 1
A getWeight() 0 3 1
A isNotEqualTo() 0 4 1
A boot() 0 10 1
A isEqualTo() 0 4 1
A getId() 0 3 1
A getName() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of Laravel Love.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Love\ReactionType\Models;
15
16
use Cog\Contracts\Love\ReactionType\Exceptions\ReactionTypeInvalid;
17
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract;
18
use Cog\Laravel\Love\Reaction\Models\Reaction;
19
use Illuminate\Database\Eloquent\Model;
20
use Illuminate\Database\Eloquent\Relations\HasMany;
21
22
final class ReactionType extends Model implements
23
    ReactionTypeContract
24
{
25
    protected $table = 'love_reaction_types';
26
27
    protected $fillable = [
28
        'name',
29
        'weight',
30
    ];
31
32
    protected $casts = [
33
        'id' => 'string',
34
        'weight' => 'integer',
35
    ];
36
37
    private static $nameCache = [];
38
39
    protected static function boot(): void
40
    {
41
        parent::boot();
42
43
        self::saved(function (ReactionTypeContract $reactionType) {
44
            self::$nameCache[$reactionType->getName()] = $reactionType;
45
        });
46
47
        self::deleted(function (ReactionTypeContract $reactionType) {
48
            unset(self::$nameCache[$reactionType->getName()]);
49
        });
50
    }
51
52
    public function reactions(): HasMany
53
    {
54
        return $this->hasMany(Reaction::class, 'reaction_type_id');
55
    }
56
57
    public static function fromName(
58
        string $name
59
    ): ReactionTypeContract {
60
        if (isset(self::$nameCache[$name])) {
61
            return self::$nameCache[$name];
62
        }
63
64
        /** @var \Cog\Laravel\Love\ReactionType\Models\ReactionType $type */
65
        $type = self::query()->where('name', $name)->first();
66
67
        if (is_null($type)) {
68
            throw ReactionTypeInvalid::nameNotExists($name);
69
        }
70
71
        self::$nameCache[$name] = $type;
72
73
        return $type;
74
    }
75
76
    public function getId(): string
77
    {
78
        return $this->getAttributeValue('id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAttributeValue('id') could return the type boolean|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
79
    }
80
81
    public function getName(): string
82
    {
83
        return $this->getAttributeValue('name');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAttributeValue('name') could return the type boolean|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
84
    }
85
86
    public function getWeight(): int
87
    {
88
        return $this->getAttributeValue('weight') ?? 0;
89
    }
90
91
    public function isEqualTo(
92
        ReactionTypeContract $that
93
    ): bool {
94
        return $this->getId() === $that->getId();
95
    }
96
97
    public function isNotEqualTo(
98
        ReactionTypeContract $that
99
    ): bool {
100
        return !$this->isEqualTo($that);
101
    }
102
}
103