LoveScope::loveTo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace CSlant\LaravelLike\Traits\Love;
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 LoveScope
12
 *
13
 * @package CSlant\LaravelLike\Traits\Love
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 LoveScope
21
{
22
    /**
23
     * The scope locale for select love relationship.
24
     *
25
     * @return MorphOne
26
     */
27
    public function loveTo(): MorphOne
28
    {
29
        return $this->likeOne()->where('type', InteractionTypeEnum::LOVE);
30
    }
31
32
    /**
33
     * The scope locale for select loves relationship.
34
     *
35
     * @return MorphMany
36
     */
37
    public function lovesTo(): MorphMany
38
    {
39
        return $this->likes()->where('type', InteractionTypeEnum::LOVE);
40
    }
41
42
    /**
43
     * Check if the model has been loved by the given user.
44
     *
45
     * @param  int  $userId
46
     *
47
     * @return bool
48
     */
49
    public function isLovedBy(int $userId): bool
50
    {
51
        return $this->isInteractedBy($userId, InteractionTypeEnum::LOVE);
52
    }
53
}
54