Completed
Pull Request — master (#88)
by Anton
02:48
created

ReactionType::getWeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 Cog\Laravel\Love\Support\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 $attributes = [
28
        'mass' => 0,
29
    ];
30
31
    protected $fillable = [
32
        'name',
33
        'mass',
34
    ];
35
36
    protected $casts = [
37
        'id' => 'string',
38
        'mass' => 'integer',
39
    ];
40
41
    private static $nameCache = [];
42
43
    protected static function boot(): void
44
    {
45
        parent::boot();
46
47
        self::saved(function (ReactionTypeContract $reactionType) {
48
            self::$nameCache[$reactionType->getName()] = $reactionType;
49
        });
50
51
        self::deleted(function (ReactionTypeContract $reactionType) {
52
            unset(self::$nameCache[$reactionType->getName()]);
53
        });
54
    }
55
56
    public function reactions(): HasMany
57
    {
58
        return $this->hasMany(Reaction::class, 'reaction_type_id');
59
    }
60
61
    public static function fromName(
62
        string $name
63
    ): ReactionTypeContract {
64
        if (isset(self::$nameCache[$name])) {
65
            return self::$nameCache[$name];
66
        }
67
68
        /** @var \Cog\Laravel\Love\ReactionType\Models\ReactionType $type */
69
        $type = self::query()->where('name', $name)->first();
70
71
        if (is_null($type)) {
72
            throw ReactionTypeInvalid::nameNotExists($name);
73
        }
74
75
        self::$nameCache[$name] = $type;
76
77
        return $type;
78
    }
79
80
    public function getId(): string
81
    {
82
        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...
83
    }
84
85
    public function getName(): string
86
    {
87
        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...
88
    }
89
90
    public function getMass(): int
91
    {
92
        return $this->getAttributeValue('mass');
93
    }
94
95
    public function isEqualTo(
96
        ReactionTypeContract $that
97
    ): bool {
98
        return $this->getId() === $that->getId();
99
    }
100
101
    public function isNotEqualTo(
102
        ReactionTypeContract $that
103
    ): bool {
104
        return !$this->isEqualTo($that);
105
    }
106
}
107