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

ReactionCounter::getCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
    /**
55
     * @var array<string, string>
56
     */
57
    protected $casts = [
58
        'count' => 'integer',
59
        'weight' => 'float',
60
    ];
61
62
    public function count(): Attribute
63
    {
64
        return new Attribute(
65
            set: fn (int | null $value) => $value ?? self::COUNT_DEFAULT,
66
        );
67
    }
68
69
    public function weight(): Attribute
70
    {
71
        return new Attribute(
72
            set: fn (float | null $value) => $value ?? self::WEIGHT_DEFAULT,
73
        );
74
    }
75
76
    public function reactant(): BelongsTo
77
    {
78
        return $this->belongsTo(Reactant::class, 'reactant_id');
79
    }
80
81
    public function reactionType(): BelongsTo
82
    {
83
        return $this->belongsTo(ReactionType::class, 'reaction_type_id');
84
    }
85
86
    public function getReactant(): ReactantInterface
87
    {
88
        return $this->getAttribute('reactant');
89
    }
90
91
    public function getReactionType(): ReactionTypeInterface
92
    {
93
        return $this->getAttribute('reactionType');
94
    }
95
96
    public function isReactionOfType(
97
        ReactionTypeInterface $reactionType,
98
    ): bool {
99
        return $this->getReactionType()->isEqualTo($reactionType);
100
    }
101
102
    public function isNotReactionOfType(
103
        ReactionTypeInterface $reactionType,
104
    ): bool {
105
        return !$this->isReactionOfType($reactionType);
106
    }
107
108
    public function getCount(): int
109
    {
110
        return $this->getAttributeValue('count');
111
    }
112
113
    public function getWeight(): float
114
    {
115
        return $this->getAttributeValue('weight');
116
    }
117
118
    public function incrementCount(
119
        int $amount,
120
    ): void {
121
        $this->increment('count', $amount);
122
    }
123
124
    public function decrementCount(
125
        int $amount,
126
    ): void {
127
        $this->decrement('count', $amount);
128
    }
129
130
    public function incrementWeight(
131
        float $amount,
132
    ): void {
133
        $this->increment('weight', $amount);
134
    }
135
136
    public function decrementWeight(
137
        float $amount,
138
    ): void {
139
        $this->decrement('weight', $amount);
140
    }
141
}
142