Completed
Push — master ( 68d5bc...b73ea8 )
by
unknown
10s
created

PendaftaranController::update()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 45
Code Lines 30

Duplication

Lines 16
Ratio 35.56 %

Importance

Changes 0
Metric Value
cc 4
eloc 30
nc 6
nop 2
dl 16
loc 45
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Pendaftaran\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
9
/* Models */
10
use Bantenprov\Pendaftaran\Models\Bantenprov\Pendaftaran\Pendaftaran;
11
use Bantenprov\Kegiatan\Models\Bantenprov\Kegiatan\Kegiatan;
12
use App\User;
13
14
/* Etc */
15
use Validator;
16
17
/**
18
 * The PendaftaranController class.
19
 *
20
 * @package Bantenprov\Pendaftaran
21
 * @author  bantenprov <[email protected]>
22
 */
23
class PendaftaranController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    protected $kegiatanModel;
31
    protected $pendaftaran;
32
    protected $user;
33
34
    public function __construct(Pendaftaran $pendaftaran, Kegiatan $kegiatan, User $user)
35
    {
36
        $this->pendaftaran      = $pendaftaran;
37
        $this->kegiatanModel    = $kegiatan;
38
        $this->user             = $user;
39
    }
40
41
    /**
42
     * Display a listing of the resource.
43
     *
44
     * @return \Illuminate\Http\Response
45
     */
46
    public function index(Request $request)
47
    {
48
        if (request()->has('sort')) {
49
            list($sortCol, $sortDir) = explode('|', request()->sort);
50
51
            $query = $this->pendaftaran->with('kegiatan')->with('user')->orderBy($sortCol, $sortDir);
52
        } else {
53
            $query = $this->pendaftaran->with('kegiatan')->with('user')->orderBy('id', 'asc');
54
        }
55
56
        if ($request->exists('filter')) {
57
            $query->where(function($q) use($request) {
58
                $value = "%{$request->filter}%";
59
                $q->where('tanggal_pendaftaran', 'like', $value)
60
                    ->orWhere('tanggal_pendaftaran', 'like', $value);
61
            });
62
        }
63
64
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
65
        $response = $query->paginate($perPage);
66
67
        return response()->json($response)
68
            ->header('Access-Control-Allow-Origin', '*')
69
            ->header('Access-Control-Allow-Methods', 'GET');
70
    }
71
72
    /**
73
     * Show the form for creating a new resource.
74
     *
75
     * @return \Illuminate\Http\Response
76
     */
77
    public function create()
78
    {
79
        $response = [];
80
81
        $kegiatan = $this->kegiatanModel->all();
82
        $users_special = $this->user->all();
83
        $users_standar = $this->user->find(\Auth::User()->id);
84
        $current_user = \Auth::User();
85
86
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
87
88
        if($role_check){
89
            $response['user_special'] = true;
90
            foreach($users_special as $user){
91
                array_set($user, 'label', $user->name);
92
            }
93
            $response['user'] = $users_special;
94
        }else{
95
            $response['user_special'] = false;
96
            array_set($users_standar, 'label', $users_standar->name);
97
            $response['user'] = $users_standar;
98
        }
99
100
        array_set($current_user, 'label', $current_user->name);
101
102
        $response['current_user'] = $current_user;
103
        $response['kegiatan'] = $kegiatan;
104
        $response['status'] = true;
105
106
        return response()->json($response);
107
    }
108
109
    /**
110
     * Display the specified resource.
111
     *
112
     * @param  \App\Pendaftaran  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
113
     * @return \Illuminate\Http\Response
114
     */
115
    public function store(Request $request)
116
    {
117
        $pendaftaran = $this->pendaftaran;
118
        $current_user_id = \Auth::user()->id;
119
120
        $validator = Validator::make($request->all(), [
121
            'kegiatan_id' => 'required',
122
            'user_id' => 'required|max:16|unique:pendaftarans,user_id',
123
            'tanggal_pendaftaran' => 'required',
124
        ]);
125
126
        if($validator->fails()){
127
128
            $check = $pendaftaran->where('user_id', $current_user_id)->whereNull('deleted_at')->count();
129
130
            if ($check > 0) {
131
                $response['message'] = 'Failed, User already exists';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
132 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
                $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
134
                $pendaftaran->user_id = $current_user_id;
135
                $pendaftaran->tanggal_pendaftaran = $request->input('tanggal_pendaftaran');
136
                $pendaftaran->save();
137
138
                $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
139
            }
140 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
            $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
142
            $pendaftaran->user_id = $current_user_id;
143
            $pendaftaran->tanggal_pendaftaran = $request->input('tanggal_pendaftaran');
144
            $pendaftaran->save();
145
            $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
146
        }
147
148
        $response['status'] = true;
149
150
        return response()->json($response);
151
    }
152
153
    /**
154
     * Store a newly created resource in storage.
155
     *
156
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
157
     * @return \Illuminate\Http\Response
158
     */
159
    public function show($id)
160
    {
161
        $pendaftaran = $this->pendaftaran->findOrFail($id);
162
163
        $response['pendaftaran'] = $pendaftaran;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
164
        $response['kegiatan'] = $pendaftaran->kegiatan;
165
        $response['user'] = $pendaftaran->user;
166
        $response['status'] = true;
167
168
        return response()->json($response);
169
    }
170
171
    /**
172
     * Show the form for editing the specified resource.
173
     *
174
     * @param  \App\Pendaftaran  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
175
     * @return \Illuminate\Http\Response
176
     */
177
    public function edit($id)
178
    {
179
        $pendaftaran = $this->pendaftaran->findOrFail($id);
180
181
        array_set($pendaftaran->user, 'label', $pendaftaran->user->name);
182
183
        $response['pendaftaran'] = $pendaftaran;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
184
        $response['kegiatan'] = $pendaftaran->kegiatan;
185
        $response['user'] = $pendaftaran->user;
186
        $response['status'] = true;
187
188
        return response()->json($response);
189
    }
190
191
    /**
192
     * Update the specified resource in storage.
193
     *
194
     * @param  \Illuminate\Http\Request  $request
195
     * @param  \App\Pendaftaran  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
196
     * @return \Illuminate\Http\Response
197
     */
198
    public function update(Request $request, $id)
199
    {
200
        $pendaftaran = $this->pendaftaran->findOrFail($id);
201
202
        if ($request->input('old_user_id') == $request->input('user_id'))
203
        {
204
            $validator = Validator::make($request->all(), [
205
                'user_id' => 'required',
206
                'kegiatan_id' => 'required',
207
                'tanggal_pendaftaran' => 'required',
208
            ]);
209
        } else {
210
            $validator = Validator::make($request->all(), [
211
                'user_id' => 'required|unique:pendaftarans,user_id',
212
                'kegiatan_id' => 'required',
213
                'tanggal_pendaftaran' => 'required',
214
            ]);
215
        }
216
217
        if ($validator->fails()) {
218
            $check = $pendaftaran->where('user_id',$request->user_id)->whereNull('deleted_at')->count();
219
220
            if ($check > 0) {
221
                $response['message'] = 'Failed, username ' . $request->user_id . ' already exists';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
222 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
                $pendaftaran->user_id = $request->input('user_id');
224
                $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
225
                $pendaftaran->tanggal_pendaftaran = $request->input('tanggal_pendaftaran');
226
                $pendaftaran->save();
227
228
                $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
229
            }
230 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
            $pendaftaran->user_id = $request->input('user_id');
232
                $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
233
                $pendaftaran->tanggal_pendaftaran = $request->input('tanggal_pendaftaran');
234
                $pendaftaran->save();
235
236
            $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
237
        }
238
239
        $response['status'] = true;
240
241
        return response()->json($response);
242
    }
243
244
    /**
245
     * Remove the specified resource from storage.
246
     *
247
     * @param  \App\Pendaftaran  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
248
     * @return \Illuminate\Http\Response
249
     */
250
    public function destroy($id)
251
    {
252
        $pendaftaran = $this->pendaftaran->findOrFail($id);
253
254
        if ($pendaftaran->delete()) {
255
            $response['status'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
256
        } else {
257
            $response['status'] = false;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
258
        }
259
260
        return json_encode($response);
261
    }
262
}
263