Passed
Pull Request — master (#68)
by Anton
05:41 queued 02:10
created

Reacter::getConnectionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\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 getConnectionName(): ?string
43
    {
44
        return COG_LOVE_DB_CONNECTION ?? $this->connection;
45
    }
46
47
    public function reacterable(): MorphTo
48
    {
49
        return $this->morphTo('reacterable', 'type', 'id', 'love_reacter_id');
50
    }
51
52
    public function reactions(): HasMany
53
    {
54
        return $this->hasMany(Reaction::class, 'reacter_id');
55
    }
56
57
    public function getId(): string
58
    {
59
        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...
60
    }
61
62
    public function getReacterable(): ReacterableContract
63
    {
64
        $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...
65
66
        if (is_null($reacterable)) {
67
            throw new NotAssignedToReacterable();
68
        }
69
70
        return $reacterable;
71
    }
72
73
    public function getReactions(): iterable
74
    {
75
        return $this->getAttribute('reactions');
76
    }
77
78
    public function reactTo(
79
        ReactantContract $reactant,
80
        ReactionTypeContract $reactionType
81
    ): void {
82
        if ($reactant->isNull()) {
83
            throw ReactantInvalid::notExists();
84
        }
85
86
        if ($this->isReactedToWithType($reactant, $reactionType)) {
87
            throw new ReactionAlreadyExists(
88
                sprintf('Reaction of type `%s` already exists.', $reactionType->getName())
89
            );
90
        }
91
92
        $this->reactions()->create([
93
            'reaction_type_id' => $reactionType->getId(),
94
            'reactant_id' => $reactant->getId(),
95
        ]);
96
    }
97
98
    public function unreactTo(
99
        ReactantContract $reactant,
100
        ReactionTypeContract $reactionType
101
    ): void {
102
        if ($reactant->isNull()) {
103
            throw ReactantInvalid::notExists();
104
        }
105
106
        $reaction = $this->reactions()->where([
107
            'reaction_type_id' => $reactionType->getId(),
108
            'reactant_id' => $reactant->getId(),
109
        ])->first();
110
111
        if (is_null($reaction)) {
112
            throw new ReactionNotExists(
113
                sprintf('Reaction of type `%s` not exists.', $reactionType->getName())
114
            );
115
        }
116
117
        $reaction->delete();
118
    }
119
120
    public function isReactedTo(
121
        ReactantContract $reactant
122
    ): bool {
123
        if ($reactant->isNull()) {
124
            return false;
125
        }
126
127
        return $reactant->isReactedBy($this);
128
    }
129
130
    public function isNotReactedTo(
131
        ReactantContract $reactant
132
    ): bool {
133
        return !$this->isReactedTo($reactant);
134
    }
135
136
    public function isReactedToWithType(
137
        ReactantContract $reactant,
138
        ReactionTypeContract $reactionType
139
    ): bool {
140
        if ($reactant->isNull()) {
141
            return false;
142
        }
143
144
        return $reactant->isReactedByWithType($this, $reactionType);
145
    }
146
147
    public function isNotReactedToWithType(
148
        ReactantContract $reactant,
149
        ReactionTypeContract $reactionType
150
    ): bool {
151
        return !$this->isReactedToWithType($reactant, $reactionType);
152
    }
153
154
    public function isEqualTo(
155
        ReacterContract $that
156
    ): bool {
157
        return $that->isNotNull()
158
            && $this->getId() === $that->getId();
159
    }
160
161
    public function isNotEqualTo(
162
        ReacterContract $that
163
    ): bool {
164
        return !$this->isEqualTo($that);
165
    }
166
167
    public function isNull(): bool
168
    {
169
        return !$this->exists;
170
    }
171
172
    public function isNotNull(): bool
173
    {
174
        return $this->exists;
175
    }
176
}
177