Completed
Push — master ( 762030...2ebc0c )
by
unknown
10s
created

SiswaController::create()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 59
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 39
nc 96
nop 0
dl 0
loc 59
rs 7.132
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Bantenprov\Siswa\Http\Controllers;
3
/* Require */
4
use App\Http\Controllers\Controller;
5
use Illuminate\Http\Request;
6
use Bantenprov\Siswa\Facades\SiswaFacade;
7
/* Models */
8
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
9
use Bantenprov\Pendaftaran\Models\Bantenprov\Pendaftaran\Pendaftaran;
10
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
11
use App\User;
12
//use Laravolt\Indonesia\Facade\Indonesia;
13
14
/* Etc */
15
use Validator;
16
/**
17
 * The SiswaController class.
18
 *
19
 * @package Bantenprov\Siswa
20
 * @author  bantenprov <[email protected]>
21
 */
22
class SiswaController extends Controller
23
{
24
    /**
25
     * Create a new controller instance.
26
     *
27
     * @return void
28
     */
29
    protected $user;
30
    protected $sekolah;
31
32
    public function __construct(Siswa $siswa, User $user, Sekolah $sekolah)
33
    {
34
        $this->siswa    = $siswa;
35
        $this->sekolah  = $sekolah;
36
        $this->user     = $user;
37
    }
38
    /**
39
     * Display a listing of the resource.
40
     *
41
     * @return \Illuminate\Http\Response
42
     */
43
    public function index(Request $request)
44
    {
45
        if (request()->has('sort')) {
46
            list($sortCol, $sortDir) = explode('|', request()->sort);
47
            $query = $this->siswa->orderBy($sortCol, $sortDir);
48
        } else {
49
            $query = $this->siswa->orderBy('id', 'asc');
50
        }
51
        if ($request->exists('filter')) {
52
            $query->where(function($q) use($request) {
53
                $value = "%{$request->filter}%";
54
                $q->where('nik', 'like', $value)
55
                    ->orWhere('nama_siswa', 'like', $value);
56
            });
57
        }
58
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
59
        $response = $query->with('user')->with('sekolah')->paginate($perPage);
60
61
        /*foreach($response as $user){
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
            array_set($response->data, 'user', $user->user->name);
63
        }*/
64
65
        return response()->json($response)
66
            ->header('Access-Control-Allow-Origin', '*')
67
            ->header('Access-Control-Allow-Methods', 'GET');
68
    }
69
    /**
70
     * Show the form for creating a new resource.
71
     *
72
     * @return \Illuminate\Http\Response
73
     */
74
    public function create()
75
    {
76
        $response = [];
77
78
        $sekolahs   = $this->sekolah->all();
79
        $provinces   = \Indonesia::allProvinces();
80
        $citys       = \Indonesia::allCities();
81
        $districts   =\Indonesia::allDistricts();
82
        $villages   = \Indonesia::allVillages();
83
        $users_special = $this->user->all();
84
        $users_standar = $this->user->find(\Auth::User()->id);
85
        $current_user = \Auth::User();
86
87
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
88
89
        if($role_check){
90
            $response['user_special'] = true;
91
            foreach($users_special as $user){
92
                array_set($user, 'label', $user->name);
93
            }
94
            $response['user'] = $users_special;
95
        }else{
96
            $response['user_special'] = false;
97
            array_set($users_standar, 'label', $users_standar->name);
98
            $response['user'] = $users_standar;
99
        }
100
101
        array_set($current_user, 'label', $current_user->name);
102
103
        $response['current_user'] = $current_user;
104
105
        foreach($sekolahs as $sekolah){
106
            array_set($sekolah, 'sekolah', $sekolah->label);
107
        }
108
109
        foreach($provinces as $province){
110
            array_set($province, 'label', $province->name);
111
        }
112
113
        foreach($citys as $city){
114
            array_set($city, 'label', $city->name);
115
        }
116
117
        foreach($districts as $district){
118
            array_set($district, 'label', $district->name);
119
        }
120
121
        foreach($villages as $village){
122
            array_set($village, 'label', $village->name);
123
        }
124
125
        $response['sekolah']    = $sekolahs;
126
        $response['province']   = $provinces;
127
        $response['city']       = $citys;
128
        $response['district']   = $districts;
129
        $response['village']    = $villages;
130
        $response['status']     = true;
131
        return response()->json($response);
132
    }
133
134
    /**
135
     * Display the specified resource.
136
     *
137
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. 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...
138
     * @return \Illuminate\Http\Response
139
     */
140
    public function store(Request $request)
141
    {
142
        $siswa = $this->siswa;
143
        $validator = Validator::make($request->all(), [
144
            'user_id'       => 'required|unique:siswas,user_id',
145
            'nomor_un'      => 'required|unique:siswas,nomor_un',
146
            'nik'           => 'required|unique:siswas,nik',
147
            'nama_siswa'    => 'required',
148
            'no_kk'         => 'required|unique:siswas,no_kk',
149
            'alamat_kk'     => 'required',
150
            'province_id'   => 'required',
151
            'city_id'       => 'required',
152
            'district_id'   => 'required',
153
            'village_id'    => 'required',
154
            'tempat_lahir'  => 'required',
155
            'tgl_lahir'     => 'required',
156
            'jenis_kelamin' => 'required',
157
            'agama'         => 'required',
158
            'nisn'          => 'required',
159
            'sekolah_id'    => 'required',
160
            'tahun_lulus'   => 'required',
161
        ]);
162
        if($validator->fails()){
163
            $check = $siswa->where('user_id',$request->user_id)->orWhere('nomor_un', $request->nomor_un)->orWhere('nik', $request->nik)->orWhere('no_kk', $request->no_kk)->whereNull('deleted_at')->count();
164
            if ($check > 0) {
165
                $response['message'] = 'Failed, Username, nomor un, nik, no kk  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...
166 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...
167
                $siswa->user_id         = $request->input('user_id');
168
                $siswa->nomor_un        = $request->input('nomor_un');
169
                $siswa->nik             = $request->input('nik');
170
                $siswa->nama_siswa      = $request->input('nama_siswa');
171
                $siswa->no_kk           = $request->input('no_kk');
172
                $siswa->alamat_kk       = $request->input('alamat_kk');
173
                $siswa->province_id     = $request->input('province_id');
174
                $siswa->city_id         = $request->input('city_id');
175
                $siswa->district_id     = $request->input('district_id');
176
                $siswa->village_id      = $request->input('village_id');
177
                $siswa->tempat_lahir    = $request->input('tempat_lahir');
178
                $siswa->tgl_lahir       = $request->input('tgl_lahir');
179
                $siswa->jenis_kelamin   = $request->input('jenis_kelamin');
180
                $siswa->agama           = $request->input('agama');
181
                $siswa->nisn            = $request->input('nisn');
182
                $siswa->sekolah_id      = $request->input('sekolah_id');
183
                $siswa->tahun_lulus     = $request->input('tahun_lulus');
184
                $siswa->save();
185
                $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...
186
            }
187 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...
188
                $siswa->user_id         = $request->input('user_id');
189
                $siswa->nomor_un        = $request->input('nomor_un');
190
                $siswa->nik             = $request->input('nik');
191
                $siswa->nama_siswa      = $request->input('nama_siswa');
192
                $siswa->no_kk           = $request->input('no_kk');
193
                $siswa->alamat_kk       = $request->input('alamat_kk');
194
                $siswa->province_id     = $request->input('province_id');
195
                $siswa->city_id         = $request->input('city_id');
196
                $siswa->district_id     = $request->input('district_id');
197
                $siswa->village_id      = $request->input('village_id');
198
                $siswa->tempat_lahir    = $request->input('tempat_lahir');
199
                $siswa->tgl_lahir       = $request->input('tgl_lahir');
200
                $siswa->jenis_kelamin   = $request->input('jenis_kelamin');
201
                $siswa->agama           = $request->input('agama');
202
                $siswa->nisn            = $request->input('nisn');
203
                $siswa->sekolah_id      = $request->input('sekolah_id');
204
                $siswa->tahun_lulus     = $request->input('tahun_lulus');
205
                $siswa->save();
206
                $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...
207
        }
208
        $response['status'] = true;
209
        return response()->json($response);
210
    }
211
    /**
212
     * Store a newly created resource in storage.
213
     *
214
     * @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...
215
     * @return \Illuminate\Http\Response
216
     */
217
    public function show($id)
218
    {
219
220
        $siswa = $this->siswa->findOrFail($id);
221
222
223
        array_set($siswa, 'user', $siswa->user->name);
224
        array_set($siswa, 'sekolah', $siswa->sekolah->label);
225
226
227
        $response['siswa'] = $siswa;
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...
228
        $response['sekolah'] = $siswa;
229
        $response['status'] = true;
230
231
232
233
        return response()->json($response);
234
235
236
    }
237
    /**
238
     * Show the form for editing the specified resource.
239
     *
240
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. 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...
241
     * @return \Illuminate\Http\Response
242
     */
243
    public function edit($id)
244
    {
245
        $siswa = $this->siswa->findOrFail($id);
246
        array_set($siswa->user, 'label', $siswa->user->name);
247
        //dd($siswa->user);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
248
        $response['siswa'] = $siswa;
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...
249
        $response['user'] = $siswa->user;
250
        $response['status'] = true;
251
        return response()->json($response);
252
    }
253
    /**
254
     * Update the specified resource in storage.
255
     *
256
     * @param  \Illuminate\Http\Request  $request
257
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. 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...
258
     * @return \Illuminate\Http\Response
259
     */
260
    public function update(Request $request, $id)
261
    {
262
        $response = array();
263
        $message  = array();
264
        $siswa = $this->siswa->findOrFail($id);
265
266
            $validator = Validator::make($request->all(), [
267
                'user_id'       => 'required|unique:siswas,user_id,'.$id,
268
                'label'         => 'required',
269
                'description'   => 'required',
270
                'nomor_un'      => 'required|unique:siswas,nomor_un,'.$id,
271
                'nik'           => 'required|unique:siswas,nik,'.$id,
272
                'nama_siswa'    => 'required',
273
                'alamat_kk'     => 'required',
274
                'tempat_lahir'  => 'required',
275
                'tgl_lahir'     => 'required',
276
                'jenis_kelamin' => 'required',
277
                'agama'         => 'required',
278
                'nisn'          => 'required',
279
                'tahun_lulus'   => 'required',
280
281
                ]);
282
283
                if($validator->fails()){
284
285
                    foreach($validator->messages()->getMessages() as $key => $error){
286
                        foreach($error AS $error_get) {
287
                            array_push($message, $error_get);
288
                        }
289
                    }
290
291
292
                    $check_user     = $this->siswa->where('id','!=', $id)->where('user_id', $request->user_id);
293
                    $check_nomor_un = $this->siswa->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
294
                    $check_nik      = $this->siswa->where('id','!=', $id)->where('nik', $request->nik);
295
296
                    if($check_nomor_un->count() > 0 || $check_user->count() > 0 || $check_nik->count() > 0){
297
                        $response['message'] = implode("\n",$message);
298
299
                    }else{
300
                         $siswa->user_id = $request->input('user_id');
301
                        $siswa->label = $request->input('label');
302
                        $siswa->description = $request->input('description');
303
                        $siswa->nomor_un = $request->input('nomor_un');
304
                        $siswa->nik = $request->input('nik');
305
                        $siswa->nama_siswa = $request->input('nama_siswa');
306
                        $siswa->alamat_kk = $request->input('alamat_kk');
307
                        $siswa->tempat_lahir = $request->input('tempat_lahir');
308
                        $siswa->tgl_lahir = $request->input('tgl_lahir');
309
                        $siswa->jenis_kelamin = $request->input('jenis_kelamin');
310
                        $siswa->agama = $request->input('agama');
311
                        $siswa->nisn = $request->input('nisn');
312
                        $siswa->tahun_lulus = $request->input('tahun_lulus');
313
                        $siswa->save();
314
                        $response['message'] = 'success';
315
                    }
316
             }else{
317
                    $siswa->user_id = $request->input('user_id');
318
                    $siswa->label = $request->input('label');
319
                    $siswa->description = $request->input('description');
320
                    $siswa->nomor_un = $request->input('nomor_un');
321
                    $siswa->nik = $request->input('nik');
322
                    $siswa->nama_siswa = $request->input('nama_siswa');
323
                    $siswa->alamat_kk = $request->input('alamat_kk');
324
                    $siswa->tempat_lahir = $request->input('tempat_lahir');
325
                    $siswa->tgl_lahir = $request->input('tgl_lahir');
326
                    $siswa->jenis_kelamin = $request->input('jenis_kelamin');
327
                    $siswa->agama = $request->input('agama');
328
                    $siswa->nisn = $request->input('nisn');
329
                    $siswa->tahun_lulus = $request->input('tahun_lulus');
330
                    $siswa->save();
331
                    $response['message'] = 'success';
332
        }
333
334
        $response['status'] = true;
335
        return response()->json($response);
336
    }
337
    /**
338
     * Remove the specified resource from storage.
339
     *
340
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. 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...
341
     * @return \Illuminate\Http\Response
342
     */
343
    public function destroy($id)
344
    {
345
        $siswa = $this->siswa->findOrFail($id);
346
        if ($siswa->delete()) {
347
            $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...
348
        } else {
349
            $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...
350
        }
351
        return json_encode($response);
352
    }
353
}
354