1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yoeunes\Voteable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
7
|
|
|
use Yoeunes\Voteable\Exceptions\EmptyUser; |
8
|
|
|
use Yoeunes\Voteable\Exceptions\ModelDoesNotUseVoteableTrait; |
9
|
|
|
use Yoeunes\Voteable\Exceptions\UserDoestNotHaveID; |
10
|
|
|
use Yoeunes\Voteable\Exceptions\VoteableModelNotFound; |
11
|
|
|
use Yoeunes\Voteable\Models\Vote; |
12
|
|
|
use Yoeunes\Voteable\Traits\Voteable; |
13
|
|
|
|
14
|
|
|
class VoteBuilder |
15
|
|
|
{ |
16
|
|
|
protected $user; |
17
|
|
|
|
18
|
|
|
protected $voteable; |
19
|
|
|
|
20
|
|
|
protected $uniqueVoteForUsers = true; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
if (config('voteable.auth_user')) { |
25
|
|
|
$this->user = auth()->id(); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (config('voteable.user_vote_once')) { |
29
|
|
|
$this->uniqueVoteForUsers = true; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Model|int $user |
35
|
|
|
* |
36
|
|
|
* @return VoteBuilder |
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 $voteable |
51
|
|
|
* |
52
|
|
|
* @return VoteBuilder |
53
|
|
|
* |
54
|
|
|
* @throws \Throwable |
55
|
|
|
*/ |
56
|
|
|
public function voteable(Model $voteable) |
57
|
|
|
{ |
58
|
|
|
throw_unless(in_array(Voteable::class, class_uses_recursive($voteable)), ModelDoesNotUseVoteableTrait::class, get_class($voteable).' does not use the Voteable Trait'); |
59
|
|
|
|
60
|
|
|
$this->voteable = $voteable; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param bool $unique |
67
|
|
|
* |
68
|
|
|
* @return VoteBuilder |
69
|
|
|
*/ |
70
|
|
|
public function uniqueVoteForUsers(bool $unique) |
71
|
|
|
{ |
72
|
|
|
$this->uniqueVoteForUsers = $unique; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $amount |
79
|
|
|
* |
80
|
|
|
* @return Vote |
81
|
|
|
* |
82
|
|
|
* @throws \Throwable |
83
|
|
|
*/ |
84
|
|
|
public function amount(int $amount) |
85
|
|
|
{ |
86
|
|
|
throw_if(empty($this->user), EmptyUser::class, 'Empty user'); |
87
|
|
|
|
88
|
|
|
throw_if(empty($this->voteable->id), VoteableModelNotFound::class, 'Voteable model not found'); |
89
|
|
|
|
90
|
|
|
$data = [ |
91
|
|
|
'user_id' => $this->user, |
92
|
|
|
'voteable_id' => $this->voteable->id, |
93
|
|
|
'voteable_type' => in_array(get_class($this->voteable), Relation::morphMap()) ? array_search(get_class($this->voteable), Relation::morphMap()) : get_class($this->voteable), |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$voteModel = config('voteable.vote'); |
97
|
|
|
|
98
|
|
|
$vote = $this->uniqueVoteForUsers ? (new $voteModel)->firstOrNew($data) : (new $voteModel)->fill($data); |
99
|
|
|
|
100
|
|
|
$vote->amount = $amount; |
101
|
|
|
|
102
|
|
|
$vote->save(); |
103
|
|
|
|
104
|
|
|
return $vote; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Vote |
109
|
|
|
* |
110
|
|
|
* @throws \Throwable |
111
|
|
|
*/ |
112
|
|
|
public function voteUp() |
113
|
|
|
{ |
114
|
|
|
return $this->amount(config('voteable.amount.up')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return Vote |
119
|
|
|
* |
120
|
|
|
* @throws \Throwable |
121
|
|
|
*/ |
122
|
|
|
public function voteDown() |
123
|
|
|
{ |
124
|
|
|
return $this->amount(config('voteable.amount.down')); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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: