Completed
Pull Request — master (#84)
by Anton
03:15 queued 11s
created

Reaction::getPower()   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
c 0
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\Reaction\Models;
15
16
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract;
17
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterContract;
18
use Cog\Contracts\Love\Reaction\Models\Reaction as ReactionContract;
19
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract;
20
use Cog\Laravel\Love\Reactant\Models\Reactant;
21
use Cog\Laravel\Love\Reacter\Models\Reacter;
22
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
23
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
24
use Illuminate\Database\Eloquent\Relations\BelongsTo;
25
26
final class Reaction extends Model implements
27
    ReactionContract
28
{
29
    protected $table = 'love_reactions';
30
31
    protected $attributes = [
32
        'power' => 1,
33
    ];
34
35
    protected $fillable = [
36
        'reactant_id',
37
        'reaction_type_id',
38
    ];
39
40
    protected $casts = [
41
        'id' => 'string',
42
        'power' => 'integer',
43
    ];
44
45
    public function reactant(): BelongsTo
46
    {
47
        return $this->belongsTo(Reactant::class, 'reactant_id');
48
    }
49
50
    public function reacter(): BelongsTo
51
    {
52
        return $this->belongsTo(Reacter::class, 'reacter_id');
53
    }
54
55
    public function type(): BelongsTo
56
    {
57
        return $this->belongsTo(ReactionType::class, 'reaction_type_id');
58
    }
59
60
    public function getId(): string
61
    {
62
        return $this->getAttributeValue('id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAttributeValue('id') could return the type boolean|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
63
    }
64
65
    public function getReactant(): ReactantContract
66
    {
67
        return $this->getAttribute('reactant');
68
    }
69
70
    public function getReacter(): ReacterContract
71
    {
72
        return $this->getAttribute('reacter');
73
    }
74
75
    public function getType(): ReactionTypeContract
76
    {
77
        return $this->getAttribute('type');
78
    }
79
80
    public function getWeight(): int
81
    {
82
        return $this->getType()->getWeight() * $this->getPower();
83
    }
84
85
    public function getPower(): int
86
    {
87
        return $this->getAttributeValue('power');
88
    }
89
90
    public function isOfType(
91
        ReactionTypeContract $reactionType
92
    ): bool {
93
        return $this->getType()->isEqualTo($reactionType);
94
    }
95
96
    public function isNotOfType(
97
        ReactionTypeContract $reactionType
98
    ): bool {
99
        return $this->getType()->isNotEqualTo($reactionType);
100
    }
101
102
    public function isToReactant(
103
        ReactantContract $reactant
104
    ): bool {
105
        return $this->getReactant()->isEqualTo($reactant);
106
    }
107
108
    public function isNotToReactant(
109
        ReactantContract $reactant
110
    ): bool {
111
        return !$this->isToReactant($reactant);
112
    }
113
114
    public function isByReacter(
115
        ReacterContract $reacter
116
    ): bool {
117
        return $this->getReacter()->isEqualTo($reacter);
118
    }
119
120
    public function isNotByReacter(
121
        ReacterContract $reacter
122
    ): bool {
123
        return !$this->isByReacter($reacter);
124
    }
125
}
126