LikeScopes::likesTo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CSlant\LaravelLike\Traits\Like;
4
5
use CSlant\LaravelLike\Enums\InteractionTypeEnum;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\MorphMany;
8
use Illuminate\Database\Eloquent\Relations\MorphOne;
9
10
/**
11
 * Trait LikeScopes
12
 *
13
 * @package CSlant\LaravelLike\Traits\Like
14
 * @mixin Model
15
 *
16
 * @method MorphOne<self, *> likeOne()
17
 * @method MorphMany<self, *> likes()
18
 * @method bool isInteractedBy(int $userId, null|InteractionTypeEnum $type)
19
 */
20
trait LikeScopes
21
{
22
    /**
23
     * The scope locale for select like relationship with the model (one).
24
     *
25
     * @return MorphOne
26
     */
27
    public function likeTo(): MorphOne
28
    {
29
        return $this->likeOne()->where('type', InteractionTypeEnum::LIKE);
30
    }
31
32
    /**
33
     * The scope locale for select dislike relationship with the model (one).
34
     *
35
     * @return MorphOne
36
     */
37
    public function dislikeTo(): MorphOne
38
    {
39
        return $this->likeOne()->where('type', InteractionTypeEnum::DISLIKE);
40
    }
41
42
    /**
43
     * The scope locale for select likes relationship (all).
44
     *
45
     * @return MorphMany
46
     */
47
    public function likesTo(): MorphMany
48
    {
49
        return $this->likes()->where('type', InteractionTypeEnum::LIKE);
50
    }
51
52
    /**
53
     * The scope locale for select dislikes relationship (all).
54
     *
55
     * @return MorphMany
56
     */
57
    public function dislikesTo(): MorphMany
58
    {
59
        return $this->likes()->where('type', InteractionTypeEnum::DISLIKE);
60
    }
61
62
    /**
63
     * Check if the model has been interacted by the given user.
64
     *
65
     * @param  int  $userId
66
     *
67
     * @return bool
68
     */
69
    public function isLikedBy(int $userId): bool
70
    {
71
        return $this->isInteractedBy($userId, InteractionTypeEnum::LIKE);
72
    }
73
74
    /**
75
     * Check if the model has been interacted by the given user.
76
     *
77
     * @param  int  $userId
78
     *
79
     * @return bool
80
     */
81
    public function isDislikedBy(int $userId): bool
82
    {
83
        return $this->isInteractedBy($userId, InteractionTypeEnum::DISLIKE);
84
    }
85
}
86