1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Furic\RedeemCodes\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Furic\RedeemCodes\Models\Event; |
6
|
|
|
use Furic\RedeemCodes\Models\RedeemCode; |
7
|
|
|
use Furic\RedeemCodes\Models\RedeemCodeReward; |
8
|
|
|
use App\Http\Controllers\Controller; |
|
|
|
|
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Validator; |
11
|
|
|
|
12
|
|
|
// The controller for redeem code web console. |
13
|
|
|
class RedeemCodeController extends Controller |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Display a listing of the resource. |
18
|
|
|
* |
19
|
|
|
* @return View |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public function index() |
22
|
|
|
{ |
23
|
|
|
$redeemCodes = RedeemCode::orderBy('created_at', 'desc')->get(); |
24
|
|
|
foreach ($redeemCodes as $redeemCode) { |
25
|
|
|
$event = Event::find($redeemCode->event_id); |
26
|
|
|
if (!is_null($event)) { |
27
|
|
|
$redeemCode->description = $event->name; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
return view('redeem-codes::index', compact('redeemCodes')); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Show the form for creating a new resource. |
35
|
|
|
* |
36
|
|
|
* @return View |
37
|
|
|
*/ |
38
|
|
|
public function create(Request $request) |
39
|
|
|
{ |
40
|
|
|
$validator = Validator::make($request->all(), [ |
41
|
|
|
'count' => 'required|numeric|min:1|max:500', |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
if ($validator->fails()) { |
45
|
|
|
return response([ |
|
|
|
|
46
|
|
|
'error' => 'Data not valid.' |
47
|
|
|
], 400); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$event = new Event; |
51
|
|
|
$event->name = $request->description; |
52
|
|
|
$event->save(); |
53
|
|
|
|
54
|
|
|
$codes = []; |
55
|
|
|
|
56
|
|
|
if ($request->has('reusable')) { // Make sure reusable only generate one code only |
57
|
|
|
$request->count = 1; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
for ($i = 0; $i < $request->count; $i++) { |
61
|
|
|
$redeemCode = new RedeemCode; |
62
|
|
|
$redeemCode->event_id = $event->id; |
63
|
|
|
if ($request->has('reusable')) { |
64
|
|
|
$redeemCode->reusable = 1; |
65
|
|
|
} |
66
|
|
|
if (empty($request->prefix)) { |
67
|
|
|
$redeemCode->code = $this->generateRandomString(12); |
68
|
|
|
} else { |
69
|
|
|
$redeemCode->code = strtoupper($request->prefix) . $this->generateRandomString(12 - strlen($request->prefix)); |
70
|
|
|
} |
71
|
|
|
array_push($codes, $redeemCode->code); |
72
|
|
|
$redeemCode->save(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
for ($i = 0; $i < count($request->reward_types); $i++) { |
|
|
|
|
76
|
|
|
$redeemCodeReward = new RedeemCodeReward; |
77
|
|
|
$redeemCodeReward->event_id = $event->id; |
78
|
|
|
$redeemCodeReward->type = $request->reward_types[$i]; |
79
|
|
|
$redeemCodeReward->amount = $request->reward_amounts[$i]; |
80
|
|
|
$redeemCodeReward->save(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return view('redeem-codes::added', compact('codes')); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function generateRandomString($length = 10) |
87
|
|
|
{ |
88
|
|
|
$characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; |
89
|
|
|
$charactersLength = strlen($characters); |
90
|
|
|
$randomString = ''; |
91
|
|
|
for ($i = 0; $i < $length; $i++) { |
92
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
93
|
|
|
} |
94
|
|
|
return $randomString; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Store a newly created resource in storage. |
99
|
|
|
* |
100
|
|
|
* @param Request $request |
101
|
|
|
* @return View |
102
|
|
|
*/ |
103
|
|
|
public function store(Request $request) |
104
|
|
|
{ |
105
|
|
|
// |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Display the specified resource. |
110
|
|
|
* |
111
|
|
|
* @param int $id |
112
|
|
|
* @return View |
113
|
|
|
*/ |
114
|
|
|
public function show($id) |
115
|
|
|
{ |
116
|
|
|
// |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Show the form for editing the specified resource. |
121
|
|
|
* |
122
|
|
|
* @param int $id |
123
|
|
|
* @return View |
124
|
|
|
*/ |
125
|
|
|
public function edit($id) |
126
|
|
|
{ |
127
|
|
|
// TODO |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Update the specified resource in storage. |
132
|
|
|
* |
133
|
|
|
* @param Request $request |
134
|
|
|
* @param int $id |
135
|
|
|
* @return View |
136
|
|
|
*/ |
137
|
|
|
public function update(Request $request, $id) |
138
|
|
|
{ |
139
|
|
|
// |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Remove the specified resource from storage. |
144
|
|
|
* |
145
|
|
|
* @param int $id |
146
|
|
|
* @return View |
147
|
|
|
*/ |
148
|
|
|
public function destroy($id) |
149
|
|
|
{ |
150
|
|
|
// |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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