Passed
Pull Request — master (#24)
by Anton
04:12
created

Reacter::unreactTo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
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\ReactionType\Models\ReactionType as ReactionTypeContract;
24
use Cog\Laravel\Love\Reaction\Models\Reaction;
25
use Illuminate\Database\Eloquent\Model;
26
use Illuminate\Database\Eloquent\Relations\HasMany;
27
use Illuminate\Database\Eloquent\Relations\MorphTo;
28
29
final class Reacter extends Model implements
30
    ReacterContract
31
{
32
    protected $table = 'love_reacters';
33
34
    protected $fillable = [
35
        'type',
36
    ];
37
38
    protected $casts = [
39
        'id' => 'string',
40
    ];
41
42
    public function reacterable(): MorphTo
43
    {
44
        return $this->morphTo('reacterable', 'type', 'id', 'love_reacter_id');
45
    }
46
47
    public function reactions(): HasMany
48
    {
49
        return $this->hasMany(Reaction::class, 'reacter_id');
50
    }
51
52
    public function getId(): string
53
    {
54
        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...
55
    }
56
57
    public function getReacterable(): ReacterableContract
58
    {
59
        $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...
60
61
        if (is_null($reacterable)) {
62
            throw new NotAssignedToReacterable();
63
        }
64
65
        return $reacterable;
66
    }
67
68
    public function getReactions(): iterable
69
    {
70
        return $this->getAttribute('reactions');
71
    }
72
73
    public function reactTo(
74
        ReactantContract $reactant,
75
        ReactionTypeContract $reactionType
76
    ): void {
77
        if ($reactant->isNull()) {
78
            throw ReactantInvalid::notExists();
79
        }
80
81
        if ($this->isReactedToWithType($reactant, $reactionType)) {
82
            throw new ReactionAlreadyExists(
83
                sprintf('Reaction of type `%s` already exists.', $reactionType->getName())
84
            );
85
        }
86
87
        $this->reactions()->create([
88
            'reaction_type_id' => $reactionType->getId(),
89
            'reactant_id' => $reactant->getId(),
90
        ]);
91
    }
92
93
    public function unreactTo(
94
        ReactantContract $reactant,
95
        ReactionTypeContract $reactionType
96
    ): void {
97
        if ($reactant->isNull()) {
98
            throw ReactantInvalid::notExists();
99
        }
100
101
        $reaction = $this->reactions()->where([
102
            'reaction_type_id' => $reactionType->getId(),
103
            'reactant_id' => $reactant->getId(),
104
        ])->first();
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();
113
    }
114
115
    public function isReactedTo(
116
        ReactantContract $reactant
117
    ): bool {
118
        if ($reactant->isNull()) {
119
            return false;
120
        }
121
122
        return $reactant->isReactedBy($this);
123
    }
124
125
    public function isNotReactedTo(
126
        ReactantContract $reactant
127
    ): bool {
128
        return !$this->isReactedTo($reactant);
129
    }
130
131
    public function isReactedToWithType(
132
        ReactantContract $reactant,
133
        ReactionTypeContract $reactionType
134
    ): bool {
135
        if ($reactant->isNull()) {
136
            return false;
137
        }
138
139
        return $reactant->isReactedByWithType($this, $reactionType);
140
    }
141
142
    public function isNotReactedToWithType(
143
        ReactantContract $reactant,
144
        ReactionTypeContract $reactionType
145
    ): bool {
146
        return !$this->isReactedToWithType($reactant, $reactionType);
147
    }
148
149
    public function isEqualTo(
150
        ReacterContract $that
151
    ): bool {
152
        return $that->isNotNull()
153
            && $this->getId() === $that->getId();
154
    }
155
156
    public function isNotEqualTo(
157
        ReacterContract $that
158
    ): bool {
159
        return !$this->isEqualTo($that);
160
    }
161
162
    public function isNull(): bool
163
    {
164
        return !$this->exists;
165
    }
166
167
    public function isNotNull(): bool
168
    {
169
        return $this->exists;
170
    }
171
}
172