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

ReactionTotal::incrementWeight()   A

Complexity

Conditions 1
Paths 1

Size

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