compubel /
laravel-rating
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Compubel\Rating; |
||
| 4 | |||
| 5 | use Compubel\Rating\Contracts\Rateable; |
||
| 6 | use Compubel\Rating\Contracts\Rating; |
||
| 7 | use Compubel\Rating\Exceptions\ModelNotRateable; |
||
| 8 | use Compubel\Rating\Exceptions\RatingAlreadyExists; |
||
| 9 | |||
| 10 | trait CanRate |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Relationship for models that this model currently rated. |
||
| 14 | * |
||
| 15 | * @param Model $model The model types of the results. |
||
| 16 | * @return morphToMany The relationship. |
||
| 17 | */ |
||
| 18 | public function ratings($model = null) |
||
| 19 | { |
||
| 20 | return $this->morphToMany(($model) ?: $this->getMorphClass(), 'rater', 'ratings', 'rater_id', 'rateable_id') |
||
| 21 | ->withPivot('rateable_type', 'rating') |
||
| 22 | ->wherePivot('rateable_type', ($model) ?: $this->getMorphClass()) |
||
| 23 | ->wherePivot('rater_type', $this->getMorphClass()); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Check if a model can be rated. |
||
| 28 | * |
||
| 29 | * @param Model $model The model to check |
||
| 30 | * @return bool |
||
| 31 | */ |
||
| 32 | public function isRateable($model): bool |
||
| 33 | { |
||
| 34 | if (! $model instanceof Rateable && ! $model instanceof Rating) { |
||
| 35 | throw ModelNotRateable::create($model); |
||
| 36 | } |
||
| 37 | |||
| 38 | return true; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Check if the current model is rating another model. |
||
| 43 | * |
||
| 44 | * @param Model $model The model which will be checked against. |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | public function hasRated($model): bool |
||
| 48 | { |
||
| 49 | if (! $this->isRateable($model)) { |
||
| 50 | return false; |
||
| 51 | } |
||
| 52 | |||
| 53 | return (bool) ! is_null($this->ratings($model->getMorphClass())->find($model->getKey())); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Add rating for a certain model. |
||
| 58 | * |
||
| 59 | * @param Model $model The model which will be rated. |
||
| 60 | * @param float $rating The rate amount. |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | public function addRatingFor($model, $rating): bool |
||
| 64 | { |
||
| 65 | if (! $this->isRateable($model)) { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($this->hasRated($model)) { |
||
| 70 | throw RatingAlreadyExists::create($model); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 71 | } |
||
| 72 | |||
| 73 | $this->ratings()->attach($this->getKey(), [ |
||
| 74 | 'rater_id' => $this->getKey(), |
||
| 75 | 'rateable_type' => $model->getMorphClass(), |
||
| 76 | 'rateable_id' => $model->getKey(), |
||
| 77 | 'rating' => (float) $rating, |
||
| 78 | ]); |
||
| 79 | |||
| 80 | return true; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Update or add rating for a certain model. |
||
| 85 | * |
||
| 86 | * @param Model $model The model which will be rated. |
||
| 87 | * @param float $rating The rate amount. |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | public function updateRatingFor($model, $rating): bool |
||
| 91 | { |
||
| 92 | if (! $this->isRateable($model)) { |
||
| 93 | return false; |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($this->hasRated($model)) { |
||
| 97 | $this->deleteRatingFor($model); |
||
| 98 | } |
||
| 99 | |||
| 100 | return $this->addRatingFor($model, $rating); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Rate a certain model. |
||
| 105 | * |
||
| 106 | * @param Model $model The model which will be rated. |
||
| 107 | * @param float $rating The rate amount. |
||
| 108 | * @param bool $can_update Set to false if a rating can only be added once |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function rate($model, $rating, $can_update = true): bool |
||
| 112 | { |
||
| 113 | if ($can_update === true) { |
||
| 114 | return $this->updateRatingFor($model, $rating); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $this->addRatingFor($model, $rating); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Delete rating for a certain model. |
||
| 122 | * |
||
| 123 | * @param Model $model The model which will be unrated. |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | public function deleteRatingFor($model): bool |
||
| 127 | { |
||
| 128 | if (! $this->isRateable($model)) { |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | |||
| 132 | if (! $this->hasRated($model)) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | |||
| 136 | return (bool) $this->ratings($model->getMorphClass())->detach($model->getKey()); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Unrate a certain model. |
||
| 141 | * |
||
| 142 | * @param Model $model The model which will be unrated. |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function unrate($model): bool |
||
| 146 | { |
||
| 147 | return $this->deleteRatingFor($model); |
||
| 148 | } |
||
| 149 | } |
||
| 150 |