Passed
Pull Request — master (#91)
by Anton
06:13
created

Reaction::isOfType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 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\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
    const DEFAULT_RATE = 1.0;
30
31
    protected $table = 'love_reactions';
32
33
    protected $attributes = [
34
        'rate' => self::DEFAULT_RATE,
35
    ];
36
37
    protected $fillable = [
38
        'reactant_id',
39
        'reaction_type_id',
40
        'rate',
41
    ];
42
43
    protected $casts = [
44
        'id' => 'string',
45
        'rate' => 'float',
46
    ];
47
48
    public function reactant(): BelongsTo
49
    {
50
        return $this->belongsTo(Reactant::class, 'reactant_id');
51
    }
52
53
    public function reacter(): BelongsTo
54
    {
55
        return $this->belongsTo(Reacter::class, 'reacter_id');
56
    }
57
58
    public function type(): BelongsTo
59
    {
60
        return $this->belongsTo(ReactionType::class, 'reaction_type_id');
61
    }
62
63
    public function getId(): string
64
    {
65
        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...
66
    }
67
68
    public function getReactant(): ReactantContract
69
    {
70
        return $this->getAttribute('reactant');
71
    }
72
73
    public function getReacter(): ReacterContract
74
    {
75
        return $this->getAttribute('reacter');
76
    }
77
78
    public function getType(): ReactionTypeContract
79
    {
80
        return $this->getAttribute('type');
81
    }
82
83
    public function getRate(): float
84
    {
85
        return $this->getAttributeValue('rate');
86
    }
87
88
    public function getWeight(): float
89
    {
90
        return $this->getType()->getMass() * $this->getRate();
91
    }
92
93
    public function isOfType(
94
        ReactionTypeContract $reactionType
95
    ): bool {
96
        return $this->getType()->isEqualTo($reactionType);
97
    }
98
99
    public function isNotOfType(
100
        ReactionTypeContract $reactionType
101
    ): bool {
102
        return $this->getType()->isNotEqualTo($reactionType);
103
    }
104
105
    public function isToReactant(
106
        ReactantContract $reactant
107
    ): bool {
108
        return $this->getReactant()->isEqualTo($reactant);
109
    }
110
111
    public function isNotToReactant(
112
        ReactantContract $reactant
113
    ): bool {
114
        return !$this->isToReactant($reactant);
115
    }
116
117
    public function isByReacter(
118
        ReacterContract $reacter
119
    ): bool {
120
        return $this->getReacter()->isEqualTo($reacter);
121
    }
122
123
    public function isNotByReacter(
124
        ReacterContract $reacter
125
    ): bool {
126
        return !$this->isByReacter($reacter);
127
    }
128
}
129