1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Furic\RedeemCodes\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Furic\RedeemCodes\Models\RedeemCode; |
6
|
|
|
use Furic\RedeemCodes\Models\RedeemCodeHistory; |
7
|
|
|
use App\Http\Controllers\Controller; |
|
|
|
|
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Validator; |
10
|
|
|
|
11
|
|
|
// The controller for redeem code api calls. |
12
|
|
|
class RedeemController extends Controller |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Check validation and redeem a given redeem code. |
17
|
|
|
* |
18
|
|
|
* @param string $code |
19
|
|
|
* @return \Illuminate\Http\Response |
20
|
|
|
*/ |
21
|
|
|
public function redeem($code) |
22
|
|
|
{ |
23
|
|
|
$validator = Validator::make(['code' => $code], ['code' => 'exists:redeem_codes,code']); |
24
|
|
|
if ($validator->fails()) { |
25
|
|
|
return response(['error' => $validator->errors()->first()], 400); // "The selected code is invalid." |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$redeemCode = RedeemCode::findByCode($code); |
29
|
|
|
|
30
|
|
|
if ($redeemCode->redeemed !== false) { |
31
|
|
|
return response(['error' => 'The selected code has already been redeemed.'], 400); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Check event valid date |
35
|
|
|
$event = $redeemCode->event; |
36
|
|
|
if ($event != null) { |
37
|
|
|
if ($event->start_at != null) { |
38
|
|
|
$validator = Validator::make($event->toArray(), ['start_at' => 'before:tomorrow']); |
39
|
|
|
if ($validator->fails()) { |
40
|
|
|
return response(['error' => 'The selected code cannot be used yet.'], 400); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
if ($event->end_at != null) { |
44
|
|
|
$validator = Validator::make($event->toArray(), ['end_at' => 'after:yesterday']); |
45
|
|
|
if ($validator->fails()) { |
46
|
|
|
return response(['error' => 'The selected code has expired.'], 400); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($redeemCode->reusable === false) { |
52
|
|
|
$redeemCode->setRedeemed(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Add a redeem code history |
56
|
|
|
$data = array(); |
57
|
|
|
$data['redeem_code_id'] = $redeemCode->id; |
58
|
|
|
$data['ip'] = filter_input(INPUT_SERVER, "REMOTE_ADDR"); |
59
|
|
|
$data['agent'] = filter_input(INPUT_SERVER, "HTTP_USER_AGENT"); |
60
|
|
|
RedeemCodeHistory::create($data); |
61
|
|
|
|
62
|
|
|
return response($redeemCode, 200); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths