Passed
Branch main (4c8529)
by Tan
28:05
created

InteractionRelationship::withInteractionBy()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 2
nop 2
1
<?php
2
3
namespace CSlant\LaravelLike\Traits;
4
5
use CSlant\LaravelLike\Enums\InteractionTypeEnum;
6
use CSlant\LaravelLike\Models\Like;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\MorphMany;
9
use Illuminate\Database\Eloquent\Relations\MorphOne;
10
11
/**
12
 * Trait InteractionRelationship
13
 *
14
 * @package CSlant\LaravelLike\Traits
15
 * @mixin Model
16
 *
17
 * @method MorphOne morphOne(string $related, string $name, string $type = null, string $id = null, string $localKey = null)
18
 * @method MorphMany morphMany(string $related, string $name, string $type = null, string $id = null, string $localKey = null)
19
 */
20
trait InteractionRelationship
21
{
22
    /**
23
     * Interaction has one relationship with the model.
24
     *
25
     * @return MorphOne
26
     */
27
    public function likeOne(): MorphOne
28
    {
29
        return $this->morphOne((string) config('like.interaction_model') ?? Like::class, 'model');
30
    }
31
32
    /**
33
     * Interaction has many relationship with the model.
34
     *
35
     * @return MorphMany
36
     */
37
    public function likes(): MorphMany
38
    {
39
        return $this->morphMany((string) config('like.interaction_model') ?? Like::class, 'model');
40
    }
41
42
    /**
43
     * Get the interaction of the given user.
44
     *
45
     * @param  int  $userId
46
     * @param  null|InteractionTypeEnum  $interactionType
47
     *
48
     * @return MorphMany
49
     */
50
    public function withInteractionBy(int $userId, ?InteractionTypeEnum $interactionType = null): MorphMany
51
    {
52
        $userForeignKey = (string) (config('like.users.foreign_key') ?? 'user_id');
53
54
        $query = $this->likes()->where($userForeignKey, $userId);
55
56
        if ($interactionType && InteractionTypeEnum::isValid($interactionType)) {
57
            $query->where('type', $interactionType);
58
        }
59
60
        return $query;
61
    }
62
63
    /**
64
     * Check if the model has been interacted by the given user.
65
     *
66
     * @param  int  $userId
67
     * @param  null|InteractionTypeEnum  $interactionType
68
     *
69
     * @return bool
70
     */
71
    public function isInteractedBy(int $userId, ?InteractionTypeEnum $interactionType = null): bool
72
    {
73
        return $this->withInteractionBy($userId, $interactionType)->exists();
74
    }
75
76
    /**
77
     * Check and forget all recorded interactions of the given type.
78
     *
79
     * @param  string  $interactionType
80
     *
81
     * @return static
82
     */
83
    public function forgetInteractionsOfType(string $interactionType): static
84
    {
85
        $this->likes()->where('type', $interactionType)->delete();
86
87
        return $this;
88
    }
89
90
    /**
91
     * Check and forget all recorded interactions.
92
     *
93
     * @param  null|string  $interactionType
94
     *
95
     * @return static
96
     */
97
    public function forgetInteractions(?string $interactionType = null): static
98
    {
99
        if ($interactionType && in_array($interactionType, InteractionTypeEnum::getValuesAsStrings())) {
100
            return $this->forgetInteractionsOfType($interactionType);
101
        }
102
103
        $this->likes()->delete();
104
105
        return $this;
106
    }
107
}
108