Passed
Pull Request — main (#2)
by Tan
02:38
created

Like::isLove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace CSlant\LaravelLike\Models;
4
5
use CSlant\LaravelLike\Enums\LikeTypeEnum;
6
use CSlant\LaravelLike\Traits\InteractionRelationship;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Carbon;
9
10
/**
11
 * Class Like
12
 *
13
 * @package CSlant\LaravelLike\Models
14
 * @property int $id
15
 * @property int $user_id
16
 * @property int $model_id
17
 * @property string $model_type
18
 * @property LikeTypeEnum $type
19
 * @property Carbon $created_at
20
 * @property Carbon $updated_at
21
 */
22
class Like extends Model
23
{
24
    /**
25
     * The attributes that are mass assignable.
26
     *
27
     * @var array<int, string>
28
     */
29
    protected $fillable = [
30
        'user_id',
31
        'model_id',
32
        'model_type',
33
        'type',
34
    ];
35
36
    /**
37
     * The attributes that should be cast.
38
     *
39
     * @var array<string, string>
40
     */
41
    protected $casts = [
42
        'model_type' => 'string',
43
        'type' => LikeTypeEnum::class,
44
    ];
45
46
    /**
47
     * Check if the record is liked.
48
     * Use with likeOne() relationship. Can't use with likes() relationship.
49
     *
50
     * @see InteractionRelationship::likeOne()
51
     *
52
     * @return bool
53
     */
54
    public function isLiked(): bool
55
    {
56
        return $this->type->isLike();
57
    }
58
59
    /**
60
     * Check if the record is disliked.
61
     * Use with likeOne() relationship. Can't use with likes() relationship.
62
     *
63
     * @see InteractionRelationship::likeOne()
64
     *
65
     * @return bool
66
     */
67
    public function isDisliked(): bool
68
    {
69
        return $this->type->isDislike();
70
    }
71
72
    /**
73
     * Check if the record is loved.
74
     * Use with likeOne() relationship. Can't use with likes() relationship.
75
     *
76
     * @see InteractionRelationship::likeOne()
77
     *
78
     * @return bool
79
     */
80
    public function isLove(): bool
81
    {
82
        return $this->type->isLove();
83
    }
84
}
85