Passed
Pull Request — master (#251)
by Anton
03:04
created

ReactionCounter::setCountAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 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\Reactant\ReactionCounter\Models;
15
16
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantInterface;
17
use Cog\Contracts\Love\Reactant\ReactionCounter\Models\ReactionCounter as ReactionCounterInterface;
18
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeInterface;
19
use Cog\Laravel\Love\Reactant\Models\Reactant;
20
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
21
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
22
use Illuminate\Database\Eloquent\Casts\Attribute;
23
use Illuminate\Database\Eloquent\Factories\HasFactory;
24
use Illuminate\Database\Eloquent\Relations\BelongsTo;
25
26
final class ReactionCounter extends Model implements
27
    ReactionCounterInterface
28
{
29
    use HasFactory;
30
31
    public const COUNT_DEFAULT = 0;
32
33
    public const WEIGHT_DEFAULT = 0.0;
34
35
    protected $table = 'love_reactant_reaction_counters';
36
37
    /**
38
     * @var array<string, int|float>
39
     */
40
    protected $attributes = [
41
        'count' => self::COUNT_DEFAULT,
42
        'weight' => self::WEIGHT_DEFAULT,
43
    ];
44
45
    /**
46
     * @var array<int, string>
47
     */
48
    protected $fillable = [
49
        'reaction_type_id',
50
        'count',
51
        'weight',
52
    ];
53
54
    public function count(): Attribute
55
    {
56
        return new Attribute(
57
            set: fn (int | null $value) => $value ?? self::COUNT_DEFAULT,
58
        );
59
    }
60
61
    public function weight(): Attribute
62
    {
63
        return new Attribute(
64
            set: fn (float | null $value) => $value ?? self::WEIGHT_DEFAULT,
65
        );
66
    }
67
68
    public function reactant(): BelongsTo
69
    {
70
        return $this->belongsTo(Reactant::class, 'reactant_id');
71
    }
72
73
    public function reactionType(): BelongsTo
74
    {
75
        return $this->belongsTo(ReactionType::class, 'reaction_type_id');
76
    }
77
78
    public function getReactant(): ReactantInterface
79
    {
80
        return $this->getAttribute('reactant');
81
    }
82
83
    public function getReactionType(): ReactionTypeInterface
84
    {
85
        return $this->getAttribute('reactionType');
86
    }
87
88
    public function isReactionOfType(
89
        ReactionTypeInterface $reactionType,
90
    ): bool {
91
        return $this->getReactionType()->isEqualTo($reactionType);
92
    }
93
94
    public function isNotReactionOfType(
95
        ReactionTypeInterface $reactionType,
96
    ): bool {
97
        return !$this->isReactionOfType($reactionType);
98
    }
99
100
    public function getCount(): int
101
    {
102
        return $this->getAttributeValue('count');
103
    }
104
105
    public function getWeight(): float
106
    {
107
        return $this->getAttributeValue('weight');
108
    }
109
110
    public function incrementCount(
111
        int $amount,
112
    ): void {
113
        $this->increment('count', $amount);
114
    }
115
116
    public function decrementCount(
117
        int $amount,
118
    ): void {
119
        $this->decrement('count', $amount);
120
    }
121
122
    public function incrementWeight(
123
        float $amount,
124
    ): void {
125
        $this->increment('weight', $amount);
126
    }
127
128
    public function decrementWeight(
129
        float $amount,
130
    ): void {
131
        $this->decrement('weight', $amount);
132
    }
133
}
134