Completed
Push — main ( 13c4b6...f71106 )
by Tan
18s queued 15s
created

Like::model()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 1
c 2
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Query\Builder;
10
use Illuminate\Support\Carbon;
11
12
/**
13
 * Class Like
14
 *
15
 * @package CSlant\LaravelLike\Models
16
 * @property int $id
17
 * @property int $user_id
18
 * @property int $model_id
19
 * @property string $model_type
20
 * @property InteractionTypeEnum $type
21
 * @property Carbon $created_at
22
 * @property Carbon $updated_at
23
 * @property Model $user
24
 * @property Model $model
25
 *
26
 * @property-read string $interaction_type getInteractionTypeAttribute()
27
 */
28
class Like extends Model
29
{
30
    /**
31
     * The attributes that are mass assignable.
32
     *
33
     * @var array<int, string>
34
     */
35
    protected $fillable = [
36
        'user_id',
37
        'model_id',
38
        'model_type',
39
        'type',
40
    ];
41
42
    /**
43
     * The attributes that should be cast.
44
     *
45
     * @var array<string, string>
46
     */
47
    protected $casts = [
48
        'model_type' => 'string',
49
        'type' => InteractionTypeEnum::class,
50
    ];
51
52
    /**
53
     * Get the user that owns the like.
54
     *
55
     * @return BelongsTo
56
     */
57
    public function user(): BelongsTo
58
    {
59
        $userModel = (string) (config('like.users.model') ?? config('auth.providers.users.model'));
60
        $userForeignKey = (string) (config('like.users.foreign_key') ?? 'user_id');
61
62
        return $this->belongsTo($userModel, $userForeignKey);
63
    }
64
65
    /**
66
     * Get the model that the like belongs to.
67
     *
68
     * @return BelongsTo<Model, self>
69
     */
70
    public function model(): BelongsTo
71
    {
72
        return $this->morphTo();
73
    }
74
75
    /**
76
     * Check if the record is liked.
77
     *
78
     * @see InteractionRelationship::likeOne()
79
     *
80
     * @return bool
81
     */
82
    public function isLiked(): bool
83
    {
84
        // Use with likeOne() relationship. Can't use with likes() relationship.
85
        return $this->type->isLike();
86
    }
87
88
    /**
89
     * Check if the record is disliked.
90
     *
91
     * @see InteractionRelationship::likeOne()
92
     *
93
     * @return bool
94
     */
95
    public function isDisliked(): bool
96
    {
97
        // Use with likeOne() relationship. Can't use with likes() relationship.
98
        return $this->type->isDislike();
99
    }
100
101
    /**
102
     * Check if the record is loved.
103
     *
104
     * @see InteractionRelationship::likeOne()
105
     *
106
     * @return bool
107
     */
108
    public function isLove(): bool
109
    {
110
        // Use with likeOne() relationship. Can't use with likes() relationship.
111
        return $this->type->isLove();
112
    }
113
114
    /**
115
     * Scope a query to only include records of a given model type.
116
     *
117
     * @param  Builder  $query
118
     * @param  string  $modelType The model type. E.g. App\Models\Post::class
119
     *
120
     * @return Builder
121
     */
122
    public function scopeWithModelType(Builder $query, string $modelType): Builder
123
    {
124
        // Use with likes() relationship. Can't use with likeOne() relationship.
125
        return $query->where('model_type', $modelType);
126
    }
127
128
    /**
129
     * Get the interaction type attribute. Used for the accessor.
130
     *
131
     * @return string
132
     */
133
    public function getInteractionTypeAttribute(): string
134
    {
135
        return $this->type->value;
136
    }
137
138
    /**
139
     * Toggle the like interaction.
140
     *
141
     * @return string
142
     */
143
    public function toggleLikeInteraction(): string
144
    {
145
        if ($this->isLiked()) {
146
            $this->type = InteractionTypeEnum::DISLIKE;
147
        } else {
148
            $this->type = InteractionTypeEnum::LIKE;
149
        }
150
151
        return $this->type->value;
152
    }
153
}
154