LoveScope   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 3
eloc 4
c 3
b 1
f 1
dl 0
loc 32
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isLovedBy() 0 3 1
A loveTo() 0 3 1
A lovesTo() 0 3 1
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