sebastiankennedy /
laravel-like
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | /* |
||||||
| 4 | * This file is part of the sebastian-kennedy/laravel-like. |
||||||
| 5 | * |
||||||
| 6 | * (c) SebastianKennedy <[email protected]> |
||||||
| 7 | * |
||||||
| 8 | * This source file is subject to the MIT license that is bundled. |
||||||
| 9 | */ |
||||||
| 10 | |||||||
| 11 | namespace SebastianKennedy\LaravelLike\Behaviors; |
||||||
| 12 | |||||||
| 13 | use Illuminate\Database\Eloquent\Model; |
||||||
| 14 | |||||||
| 15 | trait CanLikeBehavior |
||||||
| 16 | { |
||||||
| 17 | 3 | public function toggleLike(Model $model) |
|||||
| 18 | { |
||||||
| 19 | 3 | return $this->hasLiked($model) ? $this->unlike($model) : $this->like($model); |
|||||
| 20 | } |
||||||
| 21 | |||||||
| 22 | 12 | public function unlike(Model $model) |
|||||
| 23 | { |
||||||
| 24 | 12 | $relation = $model->likes() |
|||||
| 25 | 12 | ->where(config('like.morph_many_id'), $model->getKey()) |
|||||
| 26 | 12 | ->where(config('like.morph_many_type'), $model->getMorphClass()) |
|||||
| 27 | 12 | ->where(config('like.foreign_key'), $this->getKey()) |
|||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 28 | 12 | ->first(); |
|||||
| 29 | |||||||
| 30 | 12 | if ($relation) { |
|||||
| 31 | 12 | $result = $relation->delete(); |
|||||
| 32 | 12 | $this->refresh(); |
|||||
|
0 ignored issues
–
show
It seems like
refresh() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 33 | |||||||
| 34 | 12 | return $result; |
|||||
| 35 | } |
||||||
| 36 | |||||||
| 37 | 3 | return null; |
|||||
| 38 | } |
||||||
| 39 | |||||||
| 40 | 36 | public function like(Model $model) |
|||||
| 41 | { |
||||||
| 42 | 36 | if (!$this->hasLiked($model)) { |
|||||
| 43 | 36 | $like = app(config('like.model')); |
|||||
| 44 | 36 | $like->{config('like.foreign_key')} = $this->getKey(); |
|||||
| 45 | 36 | $like->{config('like.morph_many_id')} = $model->getKey(); |
|||||
| 46 | 36 | $like->{config('like.morph_many_type')} = $model->getMorphClass(); |
|||||
| 47 | |||||||
| 48 | 36 | $like = $this->likes()->save($like); |
|||||
| 49 | 36 | $this->refresh(); |
|||||
| 50 | |||||||
| 51 | 36 | return $like; |
|||||
| 52 | } |
||||||
| 53 | |||||||
| 54 | 3 | return null; |
|||||
| 55 | } |
||||||
| 56 | |||||||
| 57 | 36 | public function hasLiked(Model $model) |
|||||
| 58 | { |
||||||
| 59 | 36 | return ($this->relationLoaded('likes') ? $this->likes : $this->likes()) |
|||||
|
0 ignored issues
–
show
It seems like
relationLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 60 | 36 | ->where(config('like.morph_many_id'), $model->getKey()) |
|||||
| 61 | 36 | ->where(config('like.morph_many_type'), $model->getMorphClass()) |
|||||
| 62 | 36 | ->count() > 0; |
|||||
| 63 | } |
||||||
| 64 | |||||||
| 65 | 36 | public function likes() |
|||||
| 66 | { |
||||||
| 67 | 36 | return $this->hasMany(config('like.model'), config('like.foreign_key'), $this->getKeyName()); |
|||||
|
0 ignored issues
–
show
It seems like
getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 68 | } |
||||||
| 69 | } |
||||||
| 70 |