Passed
Push — main ( 4be7ac...c2f14a )
by Richard
04:35
created

RedeemCodeController::index()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
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;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 redeem code resource.
18
     *
19
     * @return \Illuminate\View\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 redeem code resource.
35
     *
36
     * @return \Illuminate\View\View
37
     */
38
    public function create(Request $request)
39
    {
40
        return redirect()->route('redeem-codes.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('redeem-codes.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
41
    }
42
43
    /**
44
     * Generate a random string with given length.
45
     *
46
     * @param  int  $length
47
     * @return string
48
     */
49
    private function generateRandomString($length = 10)
50
    {
51
        $characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
52
        $charactersLength = strlen($characters);
53
        $randomString = '';
54
        for ($i = 0; $i < $length; $i++) {
55
            $randomString .= $characters[rand(0, $charactersLength - 1)];
56
        }
57
        return $randomString;
58
    }
59
60
    /**
61
     * Store a newly created redeem code resource in storage.
62
     *
63
     * @param  Request  $request
64
     * @return \Illuminate\View\View
65
     */
66
    public function store(Request $request)
67
    {
68
        $validator = Validator::make($request->all(), [
69
            'count' => 'required|numeric|min:1|max:500',
70
        ]);
71
72
        if ($validator->fails()) {
73
            return view('redeem-codes::index')->with(['redeemCode' => $request->all(), 'message' => 'Data not valid']);
74
        }
75
76
        $event = new Event;
77
        $event->name = $request->description;
78
        $event->save();
79
80
        $codes = [];
81
82
        if ($request->has('reusable')) { // Make sure reusable only generate one code only
83
            $request->count = 1;
0 ignored issues
show
Bug introduced by
The property count does not seem to exist on Illuminate\Http\Request.
Loading history...
84
        }
85
86
        for ($i = 0; $i < $request->count; $i++) {
87
            $redeemCode = new RedeemCode;
88
            $redeemCode->event_id = $event->id;
89
            if ($request->has('reusable')) {
90
                $redeemCode->reusable = 1;
91
            }
92
            if (empty($request->prefix)) {
93
                $redeemCode->code = $this->generateRandomString(12);
94
            } else {
95
                $redeemCode->code = strtoupper($request->prefix) . $this->generateRandomString(12 - strlen($request->prefix));
96
            }
97
            array_push($codes, $redeemCode->code);
98
            $redeemCode->save();
99
        }
100
101
        for ($i = 0; $i < count($request->reward_types); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
102
            $redeemCodeReward = new RedeemCodeReward;
103
            $redeemCodeReward->event_id = $event->id;
104
            $redeemCodeReward->type = $request->reward_types[$i];
105
            $redeemCodeReward->amount = $request->reward_amounts[$i];
106
            $redeemCodeReward->save();
107
        }
108
109
        return view('redeem-codes::added', compact('codes'));
110
    }
111
112
    /**
113
     * Display the specified redeem code resource.
114
     *
115
     * @param  int  $id
116
     * @return \Illuminate\View\View
117
     */
118
    public function show($id)
119
    {
120
        return redirect()->route('redeem-codes.edit');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('redeem-codes.edit') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
121
    }
122
123
    /**
124
     * Show the form for editing the specified redeem code resource.
125
     *
126
     * @param  int  $id
127
     * @return \Illuminate\View\View
128
     */
129
    public function edit($id)
130
    {
131
        $redeemCode = RedeemCode::findOrFail($id);
132
        $redeemCodesInEvent = $redeemCode->event->redeemCodes;
133
        return view('redeem-codes::edit', compact('redeemCode', 'redeemCodesInEvent'));
134
    }
135
136
    /**
137
     * Update the specified redeem code resource in storage.
138
     *
139
     * @param  Request  $request
140
     * @param  int  $id
141
     * @return \Illuminate\View\View
142
     */
143
    public function update(Request $request, $id)
144
    {
145
        $redeemCode = RedeemCode::findOrFail($id);
146
        $input = $request->all();
147
148
        $redeemCode->fill($input);
149
        if ($request->has('reusable')) {
150
            $redeemCode->reusable = true;
151
        } else {
152
            $redeemCode->reusable = false;
153
        }
154
        if ($request->has('redeemed')) {
155
            $redeemCode->redeemed = $request->redeemed;
156
        } else {
157
            $redeemCode->redeemed = false;
158
        }
159
        $redeemCode->save();
160
161
        if ($request->has('redeem-code-description')) {
162
            $redeemCode->event->name = $request->redeem-code-description;
0 ignored issues
show
Bug introduced by
The constant Furic\RedeemCodes\Http\Controllers\description was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Furic\RedeemCodes\Http\Controllers\code was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
163
            $redeemCode->event->save();
164
        }
165
        if ($request->has('reward_types')) {
166
            $redeemCodeRewards = $redeemCode->rewards;
167
            for ($i = 0; $i < count($request->reward_types); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
168
                $redeemCodeReward = $i < $redeemCodeRewards->count() ? $redeemCodeRewards->slice($i, 1)->first() : new RedeemCodeReward;
169
                $redeemCodeReward->event_id = $redeemCode->event->id;
170
                $redeemCodeReward->type = $request->reward_types[$i];
171
                $redeemCodeReward->amount = $request->reward_amounts[$i];
172
                $redeemCodeReward->save();
173
            }
174
            for ($i = count($request->reward_types); $i < $redeemCodeRewards->count(); $i++) {
175
                $redeemCodeReward = $redeemCodeRewards->slice($i, 1)->first();
176
                $redeemCodeReward->delete();
177
            }
178
        }
179
        return redirect()->route('redeem-codes.index')->with('message', 'Redeem code {$redeemCode->code} updated successfully.');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...updated successfully.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
180
    }
181
182
    /**
183
     * Remove the specified redeem code resource from storage.
184
     *
185
     * @param  int  $id
186
     * @return \Illuminate\View\View
187
     */
188
    public function destroy($id)
189
    {
190
        $redeemCode = RedeemCode::findOrFail($id);
191
        $redeemCode->delete();
192
        return redirect()->route('redeem-codes.index')->with('message', 'Redeem code {$redeemCode->code} deleted successfully.');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...deleted successfully.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
193
    }
194
    
195
}