Completed
Pull Request — master (#84)
by Anton
03:14
created

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