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

Like::isLiked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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