Completed
Push — main ( 407a1d...b509b5 )
by
unknown
17s queued 14s
created

LikeScopes::toggleLikeInteraction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 10
c 1
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 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
     * Implement change like interaction. (like/dislike)
63
     *
64
     * @return string
65
     */
66
    public function toggleLikeInteraction(): string
67
    {
68
        if ($this->likeOne->isLiked()) {
69
            $this->likeOne->type = InteractionTypeEnum::DISLIKE;
70
        } else {
71
            $this->likeOne->type = InteractionTypeEnum::LIKE;
72
        }
73
74
        return $this->likeOne->type->value;
75
    }
76
}
77