Passed
Pull Request — master (#251)
by Anton
03:49 queued 50s
created

ReactionTotal::setWeightAttribute()   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
    /**
52
     * @var array<string, string>
53
     */
54
    protected $casts = [
55
        'count' => 'integer',
56
        'weight' => 'float',
57
    ];
58
59
    public function count(): Attribute
60
    {
61
        return new Attribute(
62
            set: fn (int | null $value) => $value ?? self::COUNT_DEFAULT,
63
        );
64
    }
65
66
    public function weight(): Attribute
67
    {
68
        return new Attribute(
69
            set: fn (float | null $value) => $value ?? self::WEIGHT_DEFAULT,
70
        );
71
    }
72
73
    public function reactant(): BelongsTo
74
    {
75
        return $this->belongsTo(Reactant::class, 'reactant_id');
76
    }
77
78
    public function getReactant(): ReactantInterface
79
    {
80
        return $this->getAttribute('reactant');
81
    }
82
83
    public function getCount(): int
84
    {
85
        return $this->getAttributeValue('count');
86
    }
87
88
    public function getWeight(): float
89
    {
90
        return $this->getAttributeValue('weight');
91
    }
92
93
    public function incrementCount(
94
        int $amount,
95
    ): void {
96
        $this->increment('count', $amount);
97
    }
98
99
    public function decrementCount(
100
        int $amount,
101
    ): void {
102
        $this->decrement('count', $amount);
103
    }
104
105
    public function incrementWeight(
106
        float $amount,
107
    ): void {
108
        $this->increment('weight', $amount);
109
    }
110
111
    public function decrementWeight(
112
        float $amount,
113
    ): void {
114
        $this->decrement('weight', $amount);
115
    }
116
}
117