Passed
Push — master ( 0cf9a8...478c1f )
by Anton
03:37
created

src/Reacter/Models/Reacter.php (1 issue)

Labels
Severity
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\Reacter\Models;
15
16
use Cog\Contracts\Love\Reactant\Exceptions\ReactantInvalid;
17
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract;
18
use Cog\Contracts\Love\Reacter\Exceptions\NotAssignedToReacterable;
19
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterContract;
20
use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableContract;
21
use Cog\Contracts\Love\Reaction\Exceptions\ReactionAlreadyExists;
22
use Cog\Contracts\Love\Reaction\Exceptions\ReactionNotExists;
23
use Cog\Contracts\Love\Reaction\Models\Reaction as ReactionContract;
24
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract;
25
use Cog\Laravel\Love\Reaction\Models\Reaction;
26
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
27
use Illuminate\Database\Eloquent\Relations\HasMany;
28
use Illuminate\Database\Eloquent\Relations\MorphTo;
29
30
final class Reacter extends Model implements
31
    ReacterContract
32
{
33
    protected $table = 'love_reacters';
34
35
    protected $fillable = [
36
        'type',
37
    ];
38
39
    protected $casts = [
40
        'id' => 'string',
41
    ];
42
43
    public function reacterable(): MorphTo
44
    {
45
        return $this->morphTo('reacterable', 'type', 'id', 'love_reacter_id');
46
    }
47
48
    public function reactions(): HasMany
49
    {
50
        return $this->hasMany(Reaction::class, 'reacter_id');
51
    }
52
53
    public function getId(): string
54
    {
55
        return $this->getAttributeValue('id');
56
    }
57
58
    public function getReacterable(): ReacterableContract
59
    {
60
        $reacterable = $this->getAttribute('reacterable');
61
62
        if (is_null($reacterable)) {
63
            throw new NotAssignedToReacterable();
64
        }
65
66
        return $reacterable;
67
    }
68
69
    public function getReactions(): iterable
70
    {
71
        return $this->getAttribute('reactions');
72
    }
73
74
    public function reactTo(
75
        ReactantContract $reactant,
76
        ReactionTypeContract $reactionType,
77
        ?float $rate = null
78
    ): void {
79
        if ($reactant->isNull()) {
80
            throw ReactantInvalid::notExists();
81
        }
82
83
        $reaction = $this->findReaction($reactant, $reactionType);
84
85
        if (is_null($reaction)) {
86
            $this->createReaction($reactant, $reactionType, $rate);
87
88
            return;
89
        }
90
91
        if (is_null($rate)) {
92
            throw new ReactionAlreadyExists(
93
                sprintf('Reaction of type `%s` already exists.', $reactionType->getName())
94
            );
95
        }
96
97
        if ($reaction->getRate() === $rate) {
98
            throw new ReactionAlreadyExists(
99
                sprintf('Reaction of type `%s` with `%s` rate already exists.', $reactionType->getName(), $rate)
100
            );
101
        }
102
103
        $reaction->update([
0 ignored issues
show
The method update() does not exist on Cog\Contracts\Love\Reaction\Models\Reaction. Since it exists in all sub-types, consider adding an abstract or default implementation to Cog\Contracts\Love\Reaction\Models\Reaction. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        $reaction->/** @scrutinizer ignore-call */ 
104
                   update([
Loading history...
104
            'rate' => $rate,
105
        ]);
106
    }
107
108
    public function unreactTo(
109
        ReactantContract $reactant,
110
        ReactionTypeContract $reactionType
111
    ): void {
112
        if ($reactant->isNull()) {
113
            throw ReactantInvalid::notExists();
114
        }
115
116
        $reaction = $this->findReaction($reactant, $reactionType);
117
118
        if (is_null($reaction)) {
119
            throw new ReactionNotExists(
120
                sprintf('Reaction of type `%s` not exists.', $reactionType->getName())
121
            );
122
        }
123
124
        $reaction->delete();
125
    }
126
127
    public function hasReactedTo(
128
        ReactantContract $reactant,
129
        ?ReactionTypeContract $reactionType = null,
130
        float $rate = null
131
    ): bool {
132
        if ($reactant->isNull()) {
133
            return false;
134
        }
135
136
        return $reactant->isReactedBy($this, $reactionType, $rate);
137
    }
138
139
    public function hasNotReactedTo(
140
        ReactantContract $reactant,
141
        ?ReactionTypeContract $reactionType = null,
142
        float $rate = null
143
    ): bool {
144
        return $reactant->isNotReactedBy($this, $reactionType, $rate);
145
    }
146
147
    public function isEqualTo(
148
        ReacterContract $that
149
    ): bool {
150
        return $that->isNotNull()
151
            && $this->getId() === $that->getId();
152
    }
153
154
    public function isNotEqualTo(
155
        ReacterContract $that
156
    ): bool {
157
        return !$this->isEqualTo($that);
158
    }
159
160
    public function isNull(): bool
161
    {
162
        return !$this->exists;
163
    }
164
165
    public function isNotNull(): bool
166
    {
167
        return $this->exists;
168
    }
169
170
    private function createReaction(
171
        ReactantContract $reactant,
172
        ReactionTypeContract $reactionType,
173
        ?float $rate
174
    ): void {
175
        $this->reactions()->create([
176
            'reaction_type_id' => $reactionType->getId(),
177
            'reactant_id' => $reactant->getId(),
178
            'rate' => $rate,
179
        ]);
180
    }
181
182
    /**
183
     * @param \Cog\Contracts\Love\Reactant\Models\Reactant $reactant
184
     * @param \Cog\Contracts\Love\ReactionType\Models\ReactionType $reactionType
185
     * @return null|\Cog\Contracts\Love\Reaction\Models\Reaction|\Illuminate\Database\Eloquent\Model
186
     */
187
    private function findReaction(
188
        ReactantContract $reactant,
189
        ReactionTypeContract $reactionType
190
    ): ?ReactionContract {
191
        return $this
192
            ->reactions()
193
            ->where('reactant_id', $reactant->getId())
194
            ->where('reaction_type_id', $reactionType->getId())
195
            ->first();
196
    }
197
}
198