Completed
Push — master ( a9c3e9...762030 )
by
unknown
9s
created

SiswaController::create()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 64
nop 0
dl 0
loc 42
rs 6.7272
c 0
b 0
f 0
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
        $users      = $this->user->all();
77
        $sekolahs   = $this->sekolah->all();
78
        $provinces   = \Indonesia::allProvinces();
79
        $citys       = \Indonesia::allCities();
80
        $districts   =\Indonesia::allDistricts();
81
        $villages   = \Indonesia::allVillages();
82
83
        foreach($users as $user){
84
            array_set($user, 'label', $user->name);
85
        }
86
87
        foreach($sekolahs as $sekolah){
88
            array_set($sekolah, 'sekolah', $sekolah->label);
89
        }
90
91
        foreach($provinces as $province){
92
            array_set($province, 'label', $province->name);
93
        }
94
95
        foreach($citys as $city){
96
            array_set($city, 'label', $city->name);
97
        }
98
99
        foreach($districts as $district){
100
            array_set($district, 'label', $district->name);
101
        }
102
103
        foreach($villages as $village){
104
            array_set($village, 'label', $village->name);
105
        }
106
        
107
        $response['sekolah']    = $sekolahs;
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...
108
        $response['user']       = $users;
109
        $response['province']   = $provinces;
110
        $response['city']       = $citys;
111
        $response['district']   = $districts;
112
        $response['village']    = $villages;
113
        $response['status']     = true; 
114
        return response()->json($response);
115
    }
116
117
    /**
118
     * Display the specified resource.
119
     *
120
     * @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...
121
     * @return \Illuminate\Http\Response
122
     */
123
    public function store(Request $request)
124
    {
125
        $siswa = $this->siswa;
126
        $validator = Validator::make($request->all(), [
127
            'user_id'       => 'required|unique:siswas,user_id',
128
            'nomor_un'      => 'required|unique:siswas,nomor_un',
129
            'nik'           => 'required|unique:siswas,nik',
130
            'nama_siswa'    => 'required',
131
            'no_kk'         => 'required|unique:siswas,no_kk',
132
            'alamat_kk'     => 'required',
133
            'province_id'   => 'required',
134
            'city_id'       => 'required',
135
            'district_id'   => 'required',
136
            'village_id'    => 'required',
137
            'tempat_lahir'  => 'required',
138
            'tgl_lahir'     => 'required',
139
            'jenis_kelamin' => 'required',
140
            'agama'         => 'required',
141
            'nisn'          => 'required',
142
            'sekolah_id'    => 'required',
143
            'tahun_lulus'   => 'required',  
144
        ]);
145
        if($validator->fails()){
146
            $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();
147
            if ($check > 0) {
148
                $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...
149 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...
150
                $siswa->user_id         = $request->input('user_id');
151
                $siswa->nomor_un        = $request->input('nomor_un');
152
                $siswa->nik             = $request->input('nik');
153
                $siswa->nama_siswa      = $request->input('nama_siswa');
154
                $siswa->no_kk           = $request->input('no_kk');
155
                $siswa->alamat_kk       = $request->input('alamat_kk');
156
                $siswa->province_id     = $request->input('province_id');
157
                $siswa->city_id         = $request->input('city_id');
158
                $siswa->district_id     = $request->input('district_id');
159
                $siswa->village_id      = $request->input('village_id');
160
                $siswa->tempat_lahir    = $request->input('tempat_lahir');
161
                $siswa->tgl_lahir       = $request->input('tgl_lahir');
162
                $siswa->jenis_kelamin   = $request->input('jenis_kelamin');
163
                $siswa->agama           = $request->input('agama');
164
                $siswa->nisn            = $request->input('nisn');
165
                $siswa->sekolah_id      = $request->input('sekolah_id');
166
                $siswa->tahun_lulus     = $request->input('tahun_lulus');
167
                $siswa->save();
168
                $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...
169
            }
170 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...
171
                $siswa->user_id         = $request->input('user_id');
172
                $siswa->nomor_un        = $request->input('nomor_un');
173
                $siswa->nik             = $request->input('nik');
174
                $siswa->nama_siswa      = $request->input('nama_siswa');
175
                $siswa->no_kk           = $request->input('no_kk');
176
                $siswa->alamat_kk       = $request->input('alamat_kk');
177
                $siswa->province_id     = $request->input('province_id');
178
                $siswa->city_id         = $request->input('city_id');
179
                $siswa->district_id     = $request->input('district_id');
180
                $siswa->village_id      = $request->input('village_id');
181
                $siswa->tempat_lahir    = $request->input('tempat_lahir');
182
                $siswa->tgl_lahir       = $request->input('tgl_lahir');
183
                $siswa->jenis_kelamin   = $request->input('jenis_kelamin');
184
                $siswa->agama           = $request->input('agama');
185
                $siswa->nisn            = $request->input('nisn');
186
                $siswa->sekolah_id      = $request->input('sekolah_id');
187
                $siswa->tahun_lulus     = $request->input('tahun_lulus');
188
                $siswa->save();
189
                $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...
190
        }
191
        $response['status'] = true;
192
        return response()->json($response);
193
    }
194
    /**
195
     * Store a newly created resource in storage.
196
     *
197
     * @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...
198
     * @return \Illuminate\Http\Response
199
     */
200 View Code Duplication
    public function show($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
201
    {
202
        
203
        $siswa = $this->siswa->findOrFail($id);
204
205
                    
206
        array_set($siswa, 'user', $siswa->user->name);
207
        array_set($siswa, 'sekolah', $siswa->sekolah->label);
208
209
        
210
        $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...
211
        $response['sekolah'] = $siswa; 
212
        $response['status'] = true;
213
214
        
215
216
        return response()->json($response);
217
218
        
219
    }
220
    /**
221
     * Show the form for editing the specified resource.
222
     *
223
     * @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...
224
     * @return \Illuminate\Http\Response
225
     */
226 View Code Duplication
    public function edit($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
227
    {
228
        $siswa = $this->siswa->findOrFail($id);
229
        array_set($siswa->user, 'label', $siswa->user->name);
230
        //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...
231
        $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...
232
        $response['user'] = $siswa->user;
233
        $response['status'] = true;
234
        return response()->json($response);
235
    }
236
    /**
237
     * Update the specified resource in storage.
238
     *
239
     * @param  \Illuminate\Http\Request  $request
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 update(Request $request, $id)
244
    {
245
        $response = array();
246
        $message  = array();
247
        $siswa = $this->siswa->findOrFail($id);
248
            
249
            $validator = Validator::make($request->all(), [
250
                'user_id'       => 'required|unique:siswas,user_id,'.$id,
251
                'label'         => 'required',
252
                'description'   => 'required',
253
                'nomor_un'      => 'required|unique:siswas,nomor_un,'.$id,
254
                'nik'           => 'required|unique:siswas,nik,'.$id,
255
                'nama_siswa'    => 'required',
256
                'alamat_kk'     => 'required',
257
                'tempat_lahir'  => 'required',
258
                'tgl_lahir'     => 'required',
259
                'jenis_kelamin' => 'required',
260
                'agama'         => 'required',
261
                'nisn'          => 'required',
262
                'tahun_lulus'   => 'required',
263
264
                ]);
265
266
                if($validator->fails()){
267
268
                    foreach($validator->messages()->getMessages() as $key => $error){
269
                        foreach($error AS $error_get) {
270
                            array_push($message, $error_get);
271
                        }                
272
                    } 
273
                
274
                   
275
                    $check_user     = $this->siswa->where('id','!=', $id)->where('user_id', $request->user_id);
276
                    $check_nomor_un = $this->siswa->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
277
                    $check_nik      = $this->siswa->where('id','!=', $id)->where('nik', $request->nik);
278
279
                    if($check_nomor_un->count() > 0 || $check_user->count() > 0 || $check_nik->count() > 0){
280
                        $response['message'] = implode("\n",$message);
281
282
                    }else{
283
                         $siswa->user_id = $request->input('user_id');
284
                        $siswa->label = $request->input('label');
285
                        $siswa->description = $request->input('description');
286
                        $siswa->nomor_un = $request->input('nomor_un');
287
                        $siswa->nik = $request->input('nik');
288
                        $siswa->nama_siswa = $request->input('nama_siswa');
289
                        $siswa->alamat_kk = $request->input('alamat_kk');
290
                        $siswa->tempat_lahir = $request->input('tempat_lahir');
291
                        $siswa->tgl_lahir = $request->input('tgl_lahir');
292
                        $siswa->jenis_kelamin = $request->input('jenis_kelamin');
293
                        $siswa->agama = $request->input('agama');
294
                        $siswa->nisn = $request->input('nisn');
295
                        $siswa->tahun_lulus = $request->input('tahun_lulus');
296
                        $siswa->save();
297
                        $response['message'] = 'success';
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
317
        $response['status'] = true;
318
        return response()->json($response);
319
    }
320
    /**
321
     * Remove the specified resource from storage.
322
     *
323
     * @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...
324
     * @return \Illuminate\Http\Response
325
     */
326
    public function destroy($id)
327
    {
328
        $siswa = $this->siswa->findOrFail($id);
329
        if ($siswa->delete()) {
330
            $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...
331
        } else {
332
            $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...
333
        }
334
        return json_encode($response);
335
    }
336
}
337