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

ReactionTotal::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\ReactionTotal\Models;
15
16
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantInterface;
17
use Cog\Contracts\Love\Reactant\ReactionTotal\Models\ReactionTotal as ReactionTotalInterface;
18
use Cog\Laravel\Love\Reactant\Models\Reactant;
19
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
20
use Illuminate\Database\Eloquent\Casts\Attribute;
21
use Illuminate\Database\Eloquent\Factories\HasFactory;
22
use Illuminate\Database\Eloquent\Relations\BelongsTo;
23
24
final class ReactionTotal extends Model implements
25
    ReactionTotalInterface
26
{
27
    use HasFactory;
28
29
    public const COUNT_DEFAULT = 0;
30
31
    public const WEIGHT_DEFAULT = 0.0;
32
33
    protected $table = 'love_reactant_reaction_totals';
34
35
    /**
36
     * @var array<string, int|float>
37
     */
38
    protected $attributes = [
39
        'count' => self::COUNT_DEFAULT,
40
        'weight' => self::WEIGHT_DEFAULT,
41
    ];
42
43
    /**
44
     * @var array<int, string>
45
     */
46
    protected $fillable = [
47
        'count',
48
        'weight',
49
    ];
50
51
    public function count(): Attribute
52
    {
53
        return new Attribute(
54
            set: fn (int | null $value) => $value ?? self::COUNT_DEFAULT,
55
        );
56
    }
57
58
    public function weight(): Attribute
59
    {
60
        return new Attribute(
61
            set: fn (float | null $value) => $value ?? self::WEIGHT_DEFAULT,
62
        );
63
    }
64
65
    public function reactant(): BelongsTo
66
    {
67
        return $this->belongsTo(Reactant::class, 'reactant_id');
68
    }
69
70
    public function getReactant(): ReactantInterface
71
    {
72
        return $this->getAttribute('reactant');
73
    }
74
75
    public function getCount(): int
76
    {
77
        return $this->getAttributeValue('count');
78
    }
79
80
    public function getWeight(): float
81
    {
82
        return $this->getAttributeValue('weight');
83
    }
84
85
    public function incrementCount(
86
        int $amount,
87
    ): void {
88
        $this->increment('count', $amount);
89
    }
90
91
    public function decrementCount(
92
        int $amount,
93
    ): void {
94
        $this->decrement('count', $amount);
95
    }
96
97
    public function incrementWeight(
98
        float $amount,
99
    ): void {
100
        $this->increment('weight', $amount);
101
    }
102
103
    public function decrementWeight(
104
        float $amount,
105
    ): void {
106
        $this->decrement('weight', $amount);
107
    }
108
}
109