cslant /
laravel-like
| 1 | <?php |
||
| 2 | |||
| 3 | namespace CSlant\LaravelLike\Models; |
||
| 4 | |||
| 5 | use CSlant\LaravelLike\Enums\InteractionTypeEnum; |
||
| 6 | use CSlant\LaravelLike\Traits\InteractionRelationship; |
||
|
0 ignored issues
–
show
|
|||
| 7 | use Illuminate\Database\Eloquent\Model; |
||
| 8 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
| 9 | use Illuminate\Database\Eloquent\Relations\MorphTo; |
||
| 10 | use Illuminate\Database\Query\Builder; |
||
| 11 | use Illuminate\Support\Carbon; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class Like |
||
| 15 | * |
||
| 16 | * @package CSlant\LaravelLike\Models |
||
| 17 | * @property int $id |
||
| 18 | * @property int $user_id |
||
| 19 | * @property int $model_id |
||
| 20 | * @property string $model_type |
||
| 21 | * @property InteractionTypeEnum $type |
||
| 22 | * @property Carbon $created_at |
||
| 23 | * @property Carbon $updated_at |
||
| 24 | * @property Model $user |
||
| 25 | * @property Model $model |
||
| 26 | * |
||
| 27 | * @property-read string $interaction_type getInteractionTypeAttribute() - Get the interaction type attribute. Used for the accessor. |
||
| 28 | */ |
||
| 29 | class Like extends Model |
||
| 30 | { |
||
| 31 | protected $fillable = [ |
||
| 32 | 'user_id', |
||
| 33 | 'model_id', |
||
| 34 | 'model_type', |
||
| 35 | 'type', |
||
| 36 | ]; |
||
| 37 | |||
| 38 | protected $casts = [ |
||
| 39 | 'model_type' => 'string', |
||
| 40 | 'type' => InteractionTypeEnum::class, |
||
| 41 | ]; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Get the user that owns the like. |
||
| 45 | * |
||
| 46 | * @return BelongsTo<*, *> |
||
| 47 | */ |
||
| 48 | public function user(): BelongsTo |
||
| 49 | { |
||
| 50 | $userModel = (string) (config('like.users.model') ?? config('auth.providers.users.model')); |
||
| 51 | |||
| 52 | if (!is_a($userModel, Model::class, true)) { |
||
| 53 | throw new \InvalidArgumentException("The user model must be a valid Eloquent model class."); |
||
| 54 | } |
||
| 55 | |||
| 56 | $userForeignKey = (string) (config('like.users.foreign_key') ?? 'user_id'); |
||
| 57 | |||
| 58 | return $this->belongsTo($userModel, $userForeignKey); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the model that the like belongs to. |
||
| 63 | * |
||
| 64 | * @return MorphTo<*, *> |
||
| 65 | */ |
||
| 66 | public function model(): MorphTo |
||
| 67 | { |
||
| 68 | return $this->morphTo(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Check if the record is liked. |
||
| 73 | * |
||
| 74 | * @see InteractionRelationship::likeOne() |
||
| 75 | * |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function isLiked(): bool |
||
| 79 | { |
||
| 80 | // Use with likeOne() relationship. Can't use with likes() relationship. |
||
| 81 | return $this->type->isLike(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Check if the record is disliked. |
||
| 86 | * |
||
| 87 | * @see InteractionRelationship::likeOne() |
||
| 88 | * |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | public function isDisliked(): bool |
||
| 92 | { |
||
| 93 | // Use with likeOne() relationship. Can't use with likes() relationship. |
||
| 94 | return $this->type->isDislike(); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Check if the record is loved. |
||
| 99 | * |
||
| 100 | * @see InteractionRelationship::likeOne() |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function isLove(): bool |
||
| 105 | { |
||
| 106 | // Use with likeOne() relationship. Can't use with likes() relationship. |
||
| 107 | return $this->type->isLove(); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Scope a query to only include records of a given model type. |
||
| 112 | * |
||
| 113 | * @param Builder $query |
||
| 114 | * @param string $modelType The model type. E.g. App\Models\Post::class |
||
| 115 | * |
||
| 116 | * @return Builder |
||
| 117 | */ |
||
| 118 | public function scopeWithModelType(Builder $query, string $modelType): Builder |
||
| 119 | { |
||
| 120 | // Use with likes() relationship. Can't use with likeOne() relationship. |
||
| 121 | return $query->where('model_type', $modelType); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the interaction type attribute. Used for the accessor. |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function getInteractionTypeAttribute(): string |
||
| 130 | { |
||
| 131 | return $this->type->value; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Toggle the like interaction. |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function toggleLikeInteraction(): string |
||
| 140 | { |
||
| 141 | if ($this->isLiked()) { |
||
| 142 | $this->type = InteractionTypeEnum::DISLIKE; |
||
| 143 | } else { |
||
| 144 | $this->type = InteractionTypeEnum::LIKE; |
||
| 145 | } |
||
| 146 | |||
| 147 | return $this->type->value; |
||
| 148 | } |
||
| 149 | } |
||
| 150 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths