Passed
Pull Request — master (#68)
by Anton
05:41 queued 02:10
created

ReactionCounter::getConnectionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 ReactantContract;
17
use Cog\Contracts\Love\Reactant\ReactionCounter\Models\ReactionCounter as ReactionCounterContract;
18
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract;
19
use Cog\Laravel\Love\Reactant\Models\Reactant;
20
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
21
use Illuminate\Database\Eloquent\Model;
22
use Illuminate\Database\Eloquent\Relations\BelongsTo;
23
24
final class ReactionCounter extends Model implements
25
    ReactionCounterContract
26
{
27
    protected $table = 'love_reactant_reaction_counters';
28
29
    protected $fillable = [
30
        'reaction_type_id',
31
        'count',
32
        'weight',
33
    ];
34
35
    protected $casts = [
36
        'count' => 'integer',
37
        'weight' => 'integer',
38
    ];
39
40
    public function getConnectionName(): ?string
41
    {
42
        return COG_LOVE_DB_CONNECTION ?? $this->connection;
43
    }
44
45
    public function reactant(): BelongsTo
46
    {
47
        return $this->belongsTo(Reactant::class, 'reactant_id');
48
    }
49
50
    public function reactionType(): BelongsTo
51
    {
52
        return $this->belongsTo(ReactionType::class, 'reaction_type_id');
53
    }
54
55
    public function getReactant(): ReactantContract
56
    {
57
        return $this->getAttribute('reactant');
58
    }
59
60
    public function getReactionType(): ReactionTypeContract
61
    {
62
        return $this->getAttribute('reactionType');
63
    }
64
65
    public function isReactionOfType(
66
        ReactionTypeContract $reactionType
67
    ): bool {
68
        return $this->getReactionType()->isEqualTo($reactionType);
69
    }
70
71
    public function isNotReactionOfType(
72
        ReactionTypeContract $reactionType
73
    ): bool {
74
        return !$this->isReactionOfType($reactionType);
75
    }
76
77
    public function getCount(): int
78
    {
79
        return $this->getAttributeValue('count') ?? 0;
80
    }
81
82
    public function getWeight(): int
83
    {
84
        return $this->getAttributeValue('weight') ?? 0;
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
        int $amount
101
    ): void {
102
        $this->increment('weight', $amount);
103
    }
104
105
    public function decrementWeight(
106
        int $amount
107
    ): void {
108
        $this->decrement('weight', $amount);
109
    }
110
}
111