|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yoeunes\Rateable\Builders; |
|
4
|
|
|
|
|
5
|
|
|
use Yoeunes\Rateable\Models\Rating; |
|
6
|
|
|
use Yoeunes\Rateable\Traits\Rateable; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Yoeunes\Rateable\Exceptions\EmptyUser; |
|
9
|
|
|
use Yoeunes\Rateable\Exceptions\InvalidRatingValue; |
|
10
|
|
|
use Yoeunes\Rateable\Exceptions\UserDoestNotHaveID; |
|
11
|
|
|
use Yoeunes\Rateable\Exceptions\RateableModelNotFound; |
|
12
|
|
|
use Yoeunes\Rateable\Exceptions\ModelDoesNotUseRateableTrait; |
|
13
|
|
|
|
|
14
|
|
|
class RatingBuilder |
|
15
|
|
|
{ |
|
16
|
|
|
protected $user; |
|
17
|
|
|
|
|
18
|
|
|
protected $rateable; |
|
19
|
|
|
|
|
20
|
|
|
protected $uniqueRatingForUsers = true; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
if (config('rateable.auth_user')) { |
|
25
|
|
|
$this->user = auth()->id(); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if (config('rateable.user_rate_once')) { |
|
29
|
|
|
$this->uniqueVoteForUsers = true; |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Model|int $user |
|
35
|
|
|
* |
|
36
|
|
|
* @return RatingBuilder |
|
37
|
|
|
* |
|
38
|
|
|
* @throws \Throwable |
|
39
|
|
|
*/ |
|
40
|
|
|
public function user($user) |
|
41
|
|
|
{ |
|
42
|
|
|
throw_if($user instanceof Model && empty($user->id), UserDoestNotHaveID::class, 'User object does not have ID'); |
|
43
|
|
|
|
|
44
|
|
|
$this->user = $user instanceof Model ? $user->id : $user; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param Model $rateable |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \Throwable |
|
55
|
|
|
*/ |
|
56
|
|
|
public function rateable(Model $rateable) |
|
57
|
|
|
{ |
|
58
|
|
|
throw_unless(in_array(Rateable::class, class_uses_recursive($rateable)), ModelDoesNotUseRateableTrait::class, get_class($rateable) . ' does not use the Rateable Trait'); |
|
59
|
|
|
|
|
60
|
|
|
$this->rateable = $rateable; |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function uniqueRatingForUsers(bool $unique) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->uniqueRatingForUsers = $unique; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param int $value |
|
74
|
|
|
* |
|
75
|
|
|
* @return Rating |
|
76
|
|
|
* |
|
77
|
|
|
* @throws \Throwable |
|
78
|
|
|
*/ |
|
79
|
|
|
public function rate(int $value) |
|
80
|
|
|
{ |
|
81
|
|
|
throw_if($value < config('rateable.min_rating') || $value > config('rateable.max_rating'), InvalidRatingValue::class, 'Invalid rating value'); |
|
82
|
|
|
|
|
83
|
|
|
throw_if(empty($this->user), EmptyUser::class, 'Empty user'); |
|
84
|
|
|
|
|
85
|
|
|
throw_if(empty($this->rateable->id), RateableModelNotFound::class, 'Rateable model not found'); |
|
86
|
|
|
|
|
87
|
|
|
$data = [ |
|
88
|
|
|
'user_id' => $this->user, |
|
89
|
|
|
'rateable_id' => $this->rateable->id, |
|
90
|
|
|
'rateable_type' => morph_type($this->rateable), |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
$ratingModel = config('rateable.rating'); |
|
94
|
|
|
|
|
95
|
|
|
$rating = $this->uniqueRatingForUsers ? (new $ratingModel)->firstOrNew($data) : (new $ratingModel)->fill($data); |
|
96
|
|
|
|
|
97
|
|
|
$rating->value = $value; |
|
98
|
|
|
|
|
99
|
|
|
$rating->save(); |
|
100
|
|
|
|
|
101
|
|
|
return $rating; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: