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\Reacterable\Models\Traits; |
15
|
|
|
|
16
|
|
|
use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableInterface; |
17
|
|
|
use Cog\Contracts\Love\Reacter\Facades\Reacter as ReacterFacadeInterface; |
18
|
|
|
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterInterface; |
19
|
|
|
use Cog\Contracts\Love\Reacterable\Exceptions\AlreadyRegisteredAsLoveReacter; |
20
|
|
|
use Cog\Laravel\Love\Reacter\Facades\Reacter as ReacterFacade; |
21
|
|
|
use Cog\Laravel\Love\Reacter\Models\NullReacter; |
22
|
|
|
use Cog\Laravel\Love\Reacter\Models\Reacter; |
23
|
|
|
use Cog\Laravel\Love\Reacterable\Observers\ReacterableObserver; |
24
|
|
|
use Cog\Laravel\Love\ReactionType\Models\ReactionType; |
25
|
|
|
use Illuminate\Database\Eloquent\Builder; |
26
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @mixin \Cog\Contracts\Love\Reacterable\Models\Reacterable |
30
|
|
|
*/ |
31
|
|
|
trait Reacterable |
32
|
|
|
{ |
33
|
|
|
protected static function bootReacterable(): void |
34
|
|
|
{ |
35
|
|
|
static::observe(ReacterableObserver::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function loveReacter(): BelongsTo |
39
|
|
|
{ |
40
|
|
|
return $this->belongsTo(Reacter::class, 'love_reacter_id'); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getLoveReacter(): ReacterInterface |
44
|
|
|
{ |
45
|
|
|
return $this->getAttribute('loveReacter') ?? new NullReacter($this); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function viaLoveReacter(): ReacterFacadeInterface |
49
|
|
|
{ |
50
|
|
|
return new ReacterFacade($this->getLoveReacter()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function isRegisteredAsLoveReacter(): bool |
54
|
|
|
{ |
55
|
|
|
return !$this->isNotRegisteredAsLoveReacter(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isNotRegisteredAsLoveReacter(): bool |
59
|
|
|
{ |
60
|
|
|
return $this->getAttributeValue('love_reacter_id') === null; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function registerAsLoveReacter(): void |
64
|
|
|
{ |
65
|
|
|
if ($this->isRegisteredAsLoveReacter()) { |
66
|
|
|
throw new AlreadyRegisteredAsLoveReacter(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @var \Cog\Contracts\Love\Reacter\Models\Reacter $reacter */ |
70
|
|
|
$reacter = $this->loveReacter()->create([ |
71
|
|
|
'type' => $this->getMorphClass(), |
|
|
|
|
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
$this->setAttribute('love_reacter_id', $reacter->getId()); |
|
|
|
|
75
|
|
|
$this->save(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function scopeWhereReactedTo( |
79
|
|
|
Builder $query, |
80
|
|
|
ReactableInterface $reactable, |
81
|
|
|
?string $reactionTypeName = null |
82
|
|
|
): Builder { |
83
|
|
|
return $query->whereHas('loveReacter.reactions', function (Builder $reactionsQuery) use ($reactable, $reactionTypeName) { |
84
|
|
|
$reactionsQuery->where('reactant_id', $reactable->getLoveReactant()->getId()); |
85
|
|
|
if ($reactionTypeName !== null) { |
86
|
|
|
$reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId()); |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function scopeWhereNotReactedTo( |
92
|
|
|
Builder $query, |
93
|
|
|
ReactableInterface $reactable, |
94
|
|
|
?string $reactionTypeName = null |
95
|
|
|
): Builder { |
96
|
|
|
return $query->whereDoesntHave('loveReacter.reactions', function (Builder $reactionsQuery) use ($reactable, $reactionTypeName) { |
97
|
|
|
$reactionsQuery->where('reactant_id', $reactable->getLoveReactant()->getId()); |
98
|
|
|
if ($reactionTypeName !== null) { |
99
|
|
|
$reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId()); |
100
|
|
|
} |
101
|
|
|
}); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|