usamamuneerchaudhary /
commentify
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Usamamuneerchaudhary\Commentify\Scopes; |
||||
| 4 | |||||
| 5 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||||
| 6 | use Usamamuneerchaudhary\Commentify\Models\CommentLike; |
||||
| 7 | use Usamamuneerchaudhary\Commentify\Models\User; |
||||
| 8 | |||||
| 9 | trait HasLikes |
||||
| 10 | { |
||||
| 11 | /** |
||||
| 12 | * @return HasMany |
||||
| 13 | */ |
||||
| 14 | public function likes(): HasMany |
||||
| 15 | { |
||||
| 16 | return $this->hasMany(CommentLike::class); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 17 | } |
||||
| 18 | |||||
| 19 | /** |
||||
| 20 | * @return bool |
||||
| 21 | */ |
||||
| 22 | public function isLiked(): bool |
||||
| 23 | { |
||||
| 24 | $ip = request()->ip(); |
||||
| 25 | $userAgent = request()->userAgent(); |
||||
| 26 | |||||
| 27 | if (auth()->user()) { |
||||
| 28 | if ($this->relationLoaded('likes')) { |
||||
|
0 ignored issues
–
show
It seems like
relationLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 29 | return $this->likes->contains('user_id', auth()->user()->id); |
||||
| 30 | } |
||||
| 31 | |||||
| 32 | return $this->likes()->where('user_id', auth()->user()->id)->exists(); |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | if ($ip && $userAgent) { |
||||
| 36 | if ($this->relationLoaded('likes')) { |
||||
| 37 | return $this->likes->filter(function ($like) use ($ip, $userAgent) { |
||||
| 38 | return $like->ip === $ip && $like->user_agent === $userAgent; |
||||
| 39 | })->isNotEmpty(); |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | return $this->likes()->forIp($ip)->forUserAgent($userAgent)->exists(); |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | return false; |
||||
| 46 | } |
||||
| 47 | |||||
| 48 | /** |
||||
| 49 | * @return bool |
||||
| 50 | */ |
||||
| 51 | public function removeLike(): bool |
||||
| 52 | { |
||||
| 53 | $ip = request()->ip(); |
||||
| 54 | $userAgent = request()->userAgent(); |
||||
| 55 | if (auth()->user()) { |
||||
| 56 | return $this->likes()->where('user_id', auth()->user()->id)->where('comment_id', $this->id)->delete(); |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | if ($ip && $userAgent) { |
||||
| 60 | return $this->likes()->forIp($ip)->forUserAgent($userAgent)->delete(); |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | return false; |
||||
| 64 | } |
||||
| 65 | } |
||||
| 66 |