|
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\Reactable\Models\Traits; |
|
15
|
|
|
|
|
16
|
|
|
use Cog\Contracts\Love\Reactable\Exceptions\AlreadyRegisteredAsLoveReactant; |
|
17
|
|
|
use Cog\Contracts\Love\Reactant\Facades\Reactant as ReactantFacadeContract; |
|
18
|
|
|
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract; |
|
19
|
|
|
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterContract; |
|
20
|
|
|
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract; |
|
21
|
|
|
use Cog\Laravel\Love\Reactable\Observers\ReactableObserver; |
|
22
|
|
|
use Cog\Laravel\Love\Reactant\Facades\Reactant as ReactantFacade; |
|
23
|
|
|
use Cog\Laravel\Love\Reactant\Models\NullReactant; |
|
24
|
|
|
use Cog\Laravel\Love\Reactant\Models\Reactant; |
|
25
|
|
|
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter; |
|
26
|
|
|
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal; |
|
27
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
28
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
29
|
|
|
use Illuminate\Database\Query\JoinClause; |
|
30
|
|
|
use Illuminate\Support\Facades\DB; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @mixin \Cog\Contracts\Love\Reactable\Models\Reactable |
|
34
|
|
|
*/ |
|
35
|
|
|
trait Reactable |
|
36
|
|
|
{ |
|
37
|
|
|
protected static function bootReactable(): void |
|
38
|
|
|
{ |
|
39
|
|
|
static::observe(ReactableObserver::class); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function loveReactant(): BelongsTo |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->belongsTo(Reactant::class, 'love_reactant_id'); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getLoveReactant(): ReactantContract |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->getAttribute('loveReactant') ?? new NullReactant($this); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function akaLoveReactant(): ReactantFacadeContract |
|
53
|
|
|
{ |
|
54
|
|
|
return new ReactantFacade($this->getLoveReactant()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function isRegisteredAsLoveReactant(): bool |
|
58
|
|
|
{ |
|
59
|
|
|
return !$this->isNotRegisteredAsLoveReactant(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function isNotRegisteredAsLoveReactant(): bool |
|
63
|
|
|
{ |
|
64
|
|
|
return is_null($this->getAttributeValue('love_reactant_id')); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function registerAsLoveReactant(): void |
|
68
|
|
|
{ |
|
69
|
|
|
if ($this->isRegisteredAsLoveReactant()) { |
|
70
|
|
|
throw new AlreadyRegisteredAsLoveReactant(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @var \Cog\Contracts\Love\Reactant\Models\Reactant $reactant */ |
|
74
|
|
|
$reactant = $this->loveReactant()->create([ |
|
75
|
|
|
'type' => $this->getMorphClass(), |
|
|
|
|
|
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
$this->setAttribute('love_reactant_id', $reactant->getId()); |
|
|
|
|
|
|
79
|
|
|
$this->save(); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function scopeWhereReactedBy( |
|
83
|
|
|
Builder $query, |
|
84
|
|
|
ReacterContract $reacter |
|
85
|
|
|
): Builder { |
|
86
|
|
|
return $query->whereHas('loveReactant.reactions', function (Builder $reactionsQuery) use ($reacter) { |
|
87
|
|
|
$reactionsQuery->where('reacter_id', $reacter->getId()); |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function scopeWhereReactedByWithType( |
|
92
|
|
|
Builder $query, |
|
93
|
|
|
ReacterContract $reacter, |
|
94
|
|
|
ReactionTypeContract $reactionType |
|
95
|
|
|
): Builder { |
|
96
|
|
|
return $query->whereHas('loveReactant.reactions', function (Builder $reactionsQuery) use ($reacter, $reactionType) { |
|
97
|
|
|
$reactionsQuery->where('reacter_id', $reacter->getId()); |
|
98
|
|
|
$reactionsQuery->where('reaction_type_id', $reactionType->getId()); |
|
99
|
|
|
}); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function scopeJoinReactionCounterOfType( |
|
103
|
|
|
Builder $query, |
|
104
|
|
|
ReactionTypeContract $reactionType |
|
105
|
|
|
): Builder { |
|
106
|
|
|
$select = $query->getQuery()->columns ?? ["{$this->getTable()}.*"]; |
|
|
|
|
|
|
107
|
|
|
$select[] = DB::raw('COALESCE(lrrc.count, 0) as reactions_count'); |
|
108
|
|
|
$select[] = DB::raw('COALESCE(lrrc.weight, 0) as reactions_weight'); |
|
109
|
|
|
|
|
110
|
|
|
return $query |
|
|
|
|
|
|
111
|
|
|
->leftJoin((new ReactionCounter())->getTable() . ' as lrrc', function (JoinClause $join) use ($reactionType) { |
|
112
|
|
|
$join->on('lrrc.reactant_id', '=', "{$this->getTable()}.love_reactant_id"); |
|
113
|
|
|
$join->where('lrrc.reaction_type_id', $reactionType->getId()); |
|
114
|
|
|
}) |
|
115
|
|
|
->select($select); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function scopeJoinReactionTotal( |
|
119
|
|
|
Builder $query |
|
120
|
|
|
): Builder { |
|
121
|
|
|
$select = $query->getQuery()->columns ?? ["{$this->getTable()}.*"]; |
|
122
|
|
|
$select[] = DB::raw('COALESCE(lrrt.count, 0) as reactions_total_count'); |
|
123
|
|
|
$select[] = DB::raw('COALESCE(lrrt.weight, 0) as reactions_total_weight'); |
|
124
|
|
|
|
|
125
|
|
|
return $query |
|
|
|
|
|
|
126
|
|
|
->leftJoin((new ReactionTotal())->getTable() . ' as lrrt', 'lrrt.reactant_id', '=', "{$this->getTable()}.love_reactant_id") |
|
127
|
|
|
->select($select); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|