Passed
Push — master ( f75363...6bf6cd )
by Anton
03:58 queued 01:05
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\Factories\HasFactory;
23
use Illuminate\Database\Eloquent\Relations\BelongsTo;
24
25
final class ReactionCounter extends Model implements
26
    ReactionCounterInterface
27
{
28
    use HasFactory;
29
30
    public const COUNT_DEFAULT = 0;
31
32
    public const WEIGHT_DEFAULT = 0.0;
33
34
    protected $table = 'love_reactant_reaction_counters';
35
36
    /**
37
     * @var array<int|float>
38
     */
39
    protected $attributes = [
40
        'count' => self::COUNT_DEFAULT,
41
        'weight' => self::WEIGHT_DEFAULT,
42
    ];
43
44
    /**
45
     * @var string[]
46
     */
47
    protected $fillable = [
48
        'reaction_type_id',
49
        'count',
50
        'weight',
51
    ];
52
53
    /**
54
     * @var string[]
55
     */
56
    protected $casts = [
57
        'count' => 'integer',
58
        'weight' => 'float',
59
    ];
60
61
    public function reactant(): BelongsTo
62
    {
63
        return $this->belongsTo(Reactant::class, 'reactant_id');
64
    }
65
66
    public function reactionType(): BelongsTo
67
    {
68
        return $this->belongsTo(ReactionType::class, 'reaction_type_id');
69
    }
70
71
    public function getReactant(): ReactantInterface
72
    {
73
        return $this->getAttribute('reactant');
74
    }
75
76
    public function getReactionType(): ReactionTypeInterface
77
    {
78
        return $this->getAttribute('reactionType');
79
    }
80
81
    public function isReactionOfType(
82
        ReactionTypeInterface $reactionType,
83
    ): bool {
84
        return $this->getReactionType()->isEqualTo($reactionType);
85
    }
86
87
    public function isNotReactionOfType(
88
        ReactionTypeInterface $reactionType,
89
    ): bool {
90
        return !$this->isReactionOfType($reactionType);
91
    }
92
93
    public function getCount(): int
94
    {
95
        return $this->getAttributeValue('count');
96
    }
97
98
    public function getWeight(): float
99
    {
100
        return $this->getAttributeValue('weight');
101
    }
102
103
    public function incrementCount(
104
        int $amount,
105
    ): void {
106
        $this->increment('count', $amount);
107
    }
108
109
    public function decrementCount(
110
        int $amount,
111
    ): void {
112
        $this->decrement('count', $amount);
113
    }
114
115
    public function incrementWeight(
116
        float $amount,
117
    ): void {
118
        $this->increment('weight', $amount);
119
    }
120
121
    public function decrementWeight(
122
        float $amount,
123
    ): void {
124
        $this->decrement('weight', $amount);
125
    }
126
127
    public function setCountAttribute(
128
        int | null $count,
129
    ): void {
130
        $this->attributes['count'] = $count ?? self::COUNT_DEFAULT;
131
    }
132
133
    public function setWeightAttribute(
134
        float | null $weight,
135
    ): void {
136
        $this->attributes['weight'] = $weight ?? self::WEIGHT_DEFAULT;
137
    }
138
}
139