Completed
Pull Request — master (#91)
by Anton
05:09
created

Reacter::findReaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 2
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');
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...
56
    }
57
58
    public function getReacterable(): ReacterableContract
59
    {
60
        $reacterable = $this->getAttribute('reacterable');
0 ignored issues
show
Bug introduced by
The attribute reacterable does not seem to exist on Cog\Laravel\Love\Reacter\Models\Reacter. Are you maybe missing a database migration?

If used attribute cannot be found there are two main reasons: 1. there is a creating database migration missing or 2. there is a major typo in either the declaration or the usage.

Loading history...
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
        if ($this->hasReactedTo($reactant, $reactionType)) {
84
            throw new ReactionAlreadyExists(
85
                sprintf('Reaction of type `%s` already exists.', $reactionType->getName())
86
            );
87
        }
88
89
        $this->reactions()->create([
90
            'reaction_type_id' => $reactionType->getId(),
91
            'reactant_id' => $reactant->getId(),
92
            'rate' => $rate,
93
        ]);
94
    }
95
96
    public function unreactTo(
97
        ReactantContract $reactant,
98
        ReactionTypeContract $reactionType
99
    ): void {
100
        if ($reactant->isNull()) {
101
            throw ReactantInvalid::notExists();
102
        }
103
104
        $reaction = $this->findReaction($reactant, $reactionType);
105
106
        if (is_null($reaction)) {
107
            throw new ReactionNotExists(
108
                sprintf('Reaction of type `%s` not exists.', $reactionType->getName())
109
            );
110
        }
111
112
        $reaction->delete();
0 ignored issues
show
Bug introduced by
The method delete() 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

112
        $reaction->/** @scrutinizer ignore-call */ 
113
                   delete();
Loading history...
113
    }
114
115
    public function hasReactedTo(
116
        ReactantContract $reactant,
117
        ?ReactionTypeContract $reactionType = null
118
    ): bool {
119
        if ($reactant->isNull()) {
120
            return false;
121
        }
122
123
        return $reactant->isReactedBy($this, $reactionType);
124
    }
125
126
    public function hasNotReactedTo(
127
        ReactantContract $reactant,
128
        ?ReactionTypeContract $reactionType = null
129
    ): bool {
130
        return $reactant->isNotReactedBy($this, $reactionType);
131
    }
132
133
    public function isEqualTo(
134
        ReacterContract $that
135
    ): bool {
136
        return $that->isNotNull()
137
            && $this->getId() === $that->getId();
138
    }
139
140
    public function isNotEqualTo(
141
        ReacterContract $that
142
    ): bool {
143
        return !$this->isEqualTo($that);
144
    }
145
146
    public function isNull(): bool
147
    {
148
        return !$this->exists;
149
    }
150
151
    public function isNotNull(): bool
152
    {
153
        return $this->exists;
154
    }
155
156
    /**
157
     * @param \Cog\Contracts\Love\Reactant\Models\Reactant $reactant
158
     * @param \Cog\Contracts\Love\ReactionType\Models\ReactionType $reactionType
159
     * @return null|\Cog\Contracts\Love\Reaction\Models\Reaction|\Illuminate\Database\Eloquent\Model
160
     */
161
    private function findReaction(
162
        ReactantContract $reactant,
163
        ReactionTypeContract $reactionType
164
    ): ?ReactionContract {
165
        return $this
166
            ->reactions()
167
            ->where('reactant_id', $reactant->getId())
168
            ->where('reaction_type_id', $reactionType->getId())
169
            ->first();
170
    }
171
}
172