Passed
Branch main (a1fa3a)
by Tan
02:30
created

LikeScopes   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
eloc 7
c 3
b 1
f 0
dl 0
loc 64
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dislikeTo() 0 3 1
A likeTo() 0 3 1
A likesTo() 0 3 1
A dislikesTo() 0 3 1
A isDislikedBy() 0 3 1
A isLikedBy() 0 3 1
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 likeOne()
17
 * @method MorphMany likes()
18
 */
19
trait LikeScopes
20
{
21
    /**
22
     * The scope locale for select like relationship with the model (one).
23
     *
24
     * @return MorphOne
25
     */
26
    public function likeTo(): MorphOne
27
    {
28
        return $this->likeOne()->where('type', InteractionTypeEnum::LIKE);
29
    }
30
31
    /**
32
     * The scope locale for select dislike relationship with the model (one).
33
     *
34
     * @return MorphOne
35
     */
36
    public function dislikeTo(): MorphOne
37
    {
38
        return $this->likeOne()->where('type', InteractionTypeEnum::DISLIKE);
39
    }
40
41
    /**
42
     * The scope locale for select likes relationship (all).
43
     *
44
     * @return MorphMany
45
     */
46
    public function likesTo(): MorphMany
47
    {
48
        return $this->likes()->where('type', InteractionTypeEnum::LIKE);
49
    }
50
51
    /**
52
     * The scope locale for select dislikes relationship (all).
53
     *
54
     * @return MorphMany
55
     */
56
    public function dislikesTo(): MorphMany
57
    {
58
        return $this->likes()->where('type', InteractionTypeEnum::DISLIKE);
59
    }
60
61
    /**
62
     * Check if the model has been interacted by the given user.
63
     *
64
     * @param  int  $userId
65
     *
66
     * @return bool
67
     */
68
    public function isLikedBy(int $userId): bool
69
    {
70
        return $this->isInteractedBy($userId, InteractionTypeEnum::LIKE);
0 ignored issues
show
Bug introduced by
It seems like isInteractedBy() 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 ignore-call  annotation

70
        return $this->/** @scrutinizer ignore-call */ isInteractedBy($userId, InteractionTypeEnum::LIKE);
Loading history...
71
    }
72
73
    /**
74
     * Check if the model has been interacted by the given user.
75
     *
76
     * @param  int  $userId
77
     *
78
     * @return bool
79
     */
80
    public function isDislikedBy(int $userId): bool
81
    {
82
        return $this->isInteractedBy($userId, InteractionTypeEnum::DISLIKE);
83
    }
84
}
85