Passed
Push — main ( cee2a9...63dbdd )
by Richard
04:42
created

RedeemCodeController::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 resource.
18
     *
19
     * @return View
0 ignored issues
show
Bug introduced by
The type Furic\RedeemCodes\Http\Controllers\View was not found. Did you mean View? If so, make sure to prefix the type with \.
Loading history...
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'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('redeem-code...compact('redeemCodes')) returns the type Illuminate\View\View which is incompatible with the documented return type Furic\RedeemCodes\Http\Controllers\View.
Loading history...
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([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response(array('e...Data not valid.'), 400) returns the type Illuminate\Http\Response which is incompatible with the documented return type Furic\RedeemCodes\Http\Controllers\View.
Loading history...
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;
0 ignored issues
show
Bug introduced by
The property count does not seem to exist on Illuminate\Http\Request.
Loading history...
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++) {
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...
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'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('redeem-code...ded', compact('codes')) returns the type Illuminate\View\View which is incompatible with the documented return type Furic\RedeemCodes\Http\Controllers\View.
Loading history...
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