1 | <?php |
||
11 | class Promocodes |
||
12 | { |
||
13 | /** |
||
14 | * Generated codes will be saved here |
||
15 | * to be validated later. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | private $codes = []; |
||
20 | |||
21 | /** |
||
22 | * Length of code will be calculated from asterisks you have |
||
23 | * set as mask in your config file. |
||
24 | * |
||
25 | * @var int |
||
26 | */ |
||
27 | private $length; |
||
28 | |||
29 | /** |
||
30 | * Promocodes constructor. |
||
31 | */ |
||
32 | public function __construct() |
||
37 | |||
38 | /** |
||
39 | * Generates promocodes as many as you wish. |
||
40 | * |
||
41 | * @param int $amount |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public function output($amount = 1) |
||
61 | |||
62 | /** |
||
63 | * Save promocodes into database |
||
64 | * Successful insert returns generated promocodes |
||
65 | * Fail will return empty collection. |
||
66 | * |
||
67 | * @param int $amount |
||
68 | * @param null $reward |
||
69 | * @param array $data |
||
70 | * @param int|null $expires_in |
||
71 | * @param bool $is_disposable |
||
72 | * @param int|null $quantity |
||
73 | * |
||
74 | * @return \Illuminate\Support\Collection |
||
75 | */ |
||
76 | public function create($amount = 1, $reward = null, array $data = [], $expires_in = null, $is_disposable = false, $quantity = null) |
||
101 | |||
102 | /** |
||
103 | * Save one-time use promocodes into database |
||
104 | * Successful insert returns generated promocodes |
||
105 | * Fail will return empty collection. |
||
106 | * |
||
107 | * @param int $amount |
||
108 | * @param null $reward |
||
109 | * @param array $data |
||
110 | * @param int|null $expires_in |
||
111 | * @param int|null $quantity |
||
112 | * |
||
113 | * @return \Illuminate\Support\Collection |
||
114 | */ |
||
115 | public function createDisposable($amount = 1, $reward = null, array $data = [], $expires_in = null, $quantity = null) |
||
119 | |||
120 | /** |
||
121 | * Check promocode in database if it is valid. |
||
122 | * |
||
123 | * @param string $code |
||
124 | * |
||
125 | * @return bool|Promocode |
||
126 | * @throws InvalidPromocodeException |
||
127 | */ |
||
128 | public function check($code) |
||
142 | |||
143 | /** |
||
144 | * Apply promocode to user that it's used from now. |
||
145 | * |
||
146 | * @param string $code |
||
147 | * |
||
148 | * @return bool|Promocode |
||
149 | * @throws AlreadyUsedException |
||
150 | * @throws UnauthenticatedException |
||
151 | */ |
||
152 | public function apply($code) |
||
153 | { |
||
154 | if (!auth()->check()) { |
||
|
|||
155 | throw new UnauthenticatedException; |
||
156 | } |
||
157 | |||
158 | try { |
||
159 | if ($promocode = $this->check($code)) { |
||
160 | if ($this->isSecondUsageAttempt($promocode)) { |
||
161 | throw new AlreadyUsedException; |
||
162 | } |
||
163 | |||
164 | $promocode->users()->attach(auth()->id(), [ |
||
165 | 'promocode_id' => $promocode->id, |
||
166 | 'used_at' => Carbon::now(), |
||
167 | ]); |
||
168 | |||
169 | if (!is_null($promocode->quantity)) { |
||
170 | $promocode->quantity -= 1; |
||
171 | $promocode->save(); |
||
172 | } |
||
173 | |||
174 | return $promocode->load('users'); |
||
175 | } |
||
176 | } catch (InvalidPromocodeException $exception) { |
||
177 | // |
||
178 | } |
||
179 | |||
180 | return false; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Reedem promocode to user that it's used from now. |
||
185 | * |
||
186 | * @param string $code |
||
187 | * |
||
188 | * @return bool|Promocode |
||
189 | * @throws AlreadyUsedException |
||
190 | * @throws UnauthenticatedException |
||
191 | */ |
||
192 | public function redeem($code) |
||
196 | |||
197 | /** |
||
198 | * Expire code as it won't usable anymore. |
||
199 | * |
||
200 | * @param string $code |
||
201 | * @return bool |
||
202 | * @throws InvalidPromocodeException |
||
203 | */ |
||
204 | public function disable($code) |
||
217 | |||
218 | /** |
||
219 | * Clear all expired and used promotion codes |
||
220 | * that can not be used anymore. |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | public function clearRedundant() |
||
233 | |||
234 | /** |
||
235 | * Here will be generated single code using your parameters from config. |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | private function generate() |
||
265 | |||
266 | /** |
||
267 | * Generate prefix with separator for promocode. |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | private function getPrefix() |
||
277 | |||
278 | /** |
||
279 | * Generate suffix with separator for promocode. |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | private function getSuffix() |
||
289 | |||
290 | /** |
||
291 | * Your code will be validated to be unique for one request. |
||
292 | * |
||
293 | * @param $collection |
||
294 | * @param $new |
||
295 | * |
||
296 | * @return bool |
||
297 | */ |
||
298 | private function validate($collection, $new) |
||
302 | |||
303 | /** |
||
304 | * Check if user is trying to apply code again. |
||
305 | * |
||
306 | * @param Promocode $promocode |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | public function isSecondUsageAttempt(Promocode $promocode) |
||
314 | } |
||
315 |
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: