Completed
Push — master ( 097c31...a9c3e9 )
by
unknown
10s
created

SiswaController::update()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 77
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 61
nc 7
nop 2
dl 0
loc 77
rs 6.5755
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 App\User;
11
/* Etc */
12
use Validator;
13
/**
14
 * The SiswaController class.
15
 *
16
 * @package Bantenprov\Siswa
17
 * @author  bantenprov <[email protected]>
18
 */
19
class SiswaController extends Controller
20
{
21
    /**
22
     * Create a new controller instance.
23
     *
24
     * @return void
25
     */
26
    protected $user;
27
    public function __construct(Siswa $siswa, User $user)
28
    {
29
        $this->siswa = $siswa;
30
        $this->user = $user;
31
    }
32
    /**
33
     * Display a listing of the resource.
34
     *
35
     * @return \Illuminate\Http\Response
36
     */
37
    public function index(Request $request)
38
    {
39
        if (request()->has('sort')) {
40
            list($sortCol, $sortDir) = explode('|', request()->sort);
41
            $query = $this->siswa->orderBy($sortCol, $sortDir);
42
        } else {
43
            $query = $this->siswa->orderBy('id', 'asc');
44
        }
45
        if ($request->exists('filter')) {
46
            $query->where(function($q) use($request) {
47
                $value = "%{$request->filter}%";
48
                $q->where('nik', 'like', $value)
49
                    ->orWhere('nama_siswa', 'like', $value);
50
            });
51
        }
52
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
53
        $response = $query->with('user')->paginate($perPage);
54
        
55
        /*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...
56
            array_set($response->data, 'user', $user->user->name);
57
        }*/
58
        
59
        return response()->json($response)
60
            ->header('Access-Control-Allow-Origin', '*')
61
            ->header('Access-Control-Allow-Methods', 'GET');
62
    }
63
    /**
64
     * Show the form for creating a new resource.
65
     *
66
     * @return \Illuminate\Http\Response
67
     */
68
    public function create()
69
    {        
70
        $users = $this->user->all();
71
72
        foreach($users as $user){
73
            array_set($user, 'label', $user->name);
74
        }
75
        
76
        $response['user'] = $users;
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...
77
        $response['status'] = true;
78
        return response()->json($response);
79
    }
80
    /**
81
     * Display the specified resource.
82
     *
83
     * @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...
84
     * @return \Illuminate\Http\Response
85
     */
86
    public function store(Request $request)
87
    {
88
        $siswa = $this->siswa;
89
        $validator = Validator::make($request->all(), [
90
            'user_id'       => 'required|unique:siswas,user_id',
91
            'label'         => 'required',
92
            'description'   => 'required',
93
            'nomor_un'      => 'required|unique:siswas,nomor_un',
94
            'nik'           => 'required|unique:siswas,nik',
95
            'nama_siswa'    => 'required',
96
            'alamat_kk'     => 'required',
97
            'tempat_lahir'  => 'required',
98
            'tgl_lahir'     => 'required',
99
            'jenis_kelamin' => 'required',
100
            'agama'         => 'required',
101
            'nisn'          => 'required',
102
            'tahun_lulus'   => 'required',  
103
        ]);
104
        if($validator->fails()){
105
            $check = $siswa->where('user_id',$request->user_id)->orWhere('nomor_un', $request->nomor_un)->orWhere('nik', $request->nik)->whereNull('deleted_at')->count();
106
            if ($check > 0) {
107
                $response['message'] = 'Failed, Username, nomor un, nik,  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...
108
            } else {
109
                $siswa->user_id         = $request->input('user_id');
110
                $siswa->label           = $request->input('label');
111
                $siswa->description     = $request->input('description');
112
                $siswa->nomor_un        = $request->input('nomor_un');
113
                $siswa->nik             = $request->input('nik');
114
                $siswa->nama_siswa      = $request->input('nama_siswa');
115
                $siswa->alamat_kk       = $request->input('alamat_kk');
116
                $siswa->tempat_lahir    = $request->input('tempat_lahir');
117
                $siswa->tgl_lahir       = $request->input('tgl_lahir');
118
                $siswa->jenis_kelamin   = $request->input('jenis_kelamin');
119
                $siswa->agama           = $request->input('agama');
120
                $siswa->nisn            = $request->input('nisn');
121
                $siswa->tahun_lulus     = $request->input('tahun_lulus');
122
                $siswa->save();
123
                $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...
124
            }
125
        } else {
126
                $siswa->user_id         = $request->input('user_id');
127
                $siswa->label           = $request->input('label');
128
                $siswa->description     = $request->input('description');
129
                $siswa->nomor_un        = $request->input('nomor_un');
130
                $siswa->nik             = $request->input('nik');
131
                $siswa->nama_siswa      = $request->input('nama_siswa');
132
                $siswa->alamat_kk       = $request->input('alamat_kk');
133
                $siswa->tempat_lahir    = $request->input('tempat_lahir');
134
                $siswa->tgl_lahir       = $request->input('tgl_lahir');
135
                $siswa->jenis_kelamin   = $request->input('jenis_kelamin');
136
                $siswa->agama           = $request->input('agama');
137
                $siswa->nisn            = $request->input('nisn');
138
                $siswa->tahun_lulus     = $request->input('tahun_lulus');
139
                $siswa->save();
140
                $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...
141
        }
142
        $response['status'] = true;
143
        return response()->json($response);
144
    }
145
    /**
146
     * Store a newly created resource in storage.
147
     *
148
     * @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...
149
     * @return \Illuminate\Http\Response
150
     */
151
    public function show($id)
152
    {
153
        
154
        $siswa = $this->siswa->findOrFail($id);
155
                    
156
        array_set($siswa, 'user', $siswa->user->name);           
157
        
158
        $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...
159
        $response['status'] = true;
160
161
        
162
163
        return response()->json($response);
164
165
        
166
    }
167
    /**
168
     * Show the form for editing the specified resource.
169
     *
170
     * @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...
171
     * @return \Illuminate\Http\Response
172
     */
173
    public function edit($id)
174
    {
175
        $siswa = $this->siswa->findOrFail($id);
176
        array_set($siswa->user, 'label', $siswa->user->name);
177
        //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...
178
        $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...
179
        $response['user'] = $siswa->user;
180
        $response['status'] = true;
181
        return response()->json($response);
182
    }
183
    /**
184
     * Update the specified resource in storage.
185
     *
186
     * @param  \Illuminate\Http\Request  $request
187
     * @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...
188
     * @return \Illuminate\Http\Response
189
     */
190
    public function update(Request $request, $id)
191
    {
192
        $response = array();
193
        $message  = array();
194
        $siswa = $this->siswa->findOrFail($id);
195
            
196
            $validator = Validator::make($request->all(), [
197
                'user_id'       => 'required|unique:siswas,user_id,'.$id,
198
                'label'         => 'required',
199
                'description'   => 'required',
200
                'nomor_un'      => 'required|unique:siswas,nomor_un,'.$id,
201
                'nik'           => 'required|unique:siswas,nik,'.$id,
202
                'nama_siswa'    => 'required',
203
                'alamat_kk'     => 'required',
204
                'tempat_lahir'  => 'required',
205
                'tgl_lahir'     => 'required',
206
                'jenis_kelamin' => 'required',
207
                'agama'         => 'required',
208
                'nisn'          => 'required',
209
                'tahun_lulus'   => 'required',
210
211
                ]);
212
213
                if($validator->fails()){
214
215
                    foreach($validator->messages()->getMessages() as $key => $error){
216
                        foreach($error AS $error_get) {
217
                            array_push($message, $error_get);
218
                        }                
219
                    } 
220
                
221
                   
222
                    $check_user     = $this->siswa->where('id','!=', $id)->where('user_id', $request->user_id);
223
                    $check_nomor_un = $this->siswa->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
224
                    $check_nik      = $this->siswa->where('id','!=', $id)->where('nik', $request->nik);
225
226
                    if($check_nomor_un->count() > 0 || $check_user->count() > 0 || $check_nik->count() > 0){
227
                        $response['message'] = implode("\n",$message);
228
229
                    }else{
230
                         $siswa->user_id = $request->input('user_id');
231
                        $siswa->label = $request->input('label');
232
                        $siswa->description = $request->input('description');
233
                        $siswa->nomor_un = $request->input('nomor_un');
234
                        $siswa->nik = $request->input('nik');
235
                        $siswa->nama_siswa = $request->input('nama_siswa');
236
                        $siswa->alamat_kk = $request->input('alamat_kk');
237
                        $siswa->tempat_lahir = $request->input('tempat_lahir');
238
                        $siswa->tgl_lahir = $request->input('tgl_lahir');
239
                        $siswa->jenis_kelamin = $request->input('jenis_kelamin');
240
                        $siswa->agama = $request->input('agama');
241
                        $siswa->nisn = $request->input('nisn');
242
                        $siswa->tahun_lulus = $request->input('tahun_lulus');
243
                        $siswa->save();
244
                        $response['message'] = 'success';
245
                    }
246
             }else{
247
                    $siswa->user_id = $request->input('user_id');
248
                    $siswa->label = $request->input('label');
249
                    $siswa->description = $request->input('description');
250
                    $siswa->nomor_un = $request->input('nomor_un');
251
                    $siswa->nik = $request->input('nik');
252
                    $siswa->nama_siswa = $request->input('nama_siswa');
253
                    $siswa->alamat_kk = $request->input('alamat_kk');
254
                    $siswa->tempat_lahir = $request->input('tempat_lahir');
255
                    $siswa->tgl_lahir = $request->input('tgl_lahir');
256
                    $siswa->jenis_kelamin = $request->input('jenis_kelamin');
257
                    $siswa->agama = $request->input('agama');
258
                    $siswa->nisn = $request->input('nisn');
259
                    $siswa->tahun_lulus = $request->input('tahun_lulus');
260
                    $siswa->save();
261
                    $response['message'] = 'success';
262
        }
263
264
        $response['status'] = true;
265
        return response()->json($response);
266
    }
267
    /**
268
     * Remove the specified resource from storage.
269
     *
270
     * @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...
271
     * @return \Illuminate\Http\Response
272
     */
273
    public function destroy($id)
274
    {
275
        $siswa = $this->siswa->findOrFail($id);
276
        if ($siswa->delete()) {
277
            $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...
278
        } else {
279
            $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...
280
        }
281
        return json_encode($response);
282
    }
283
}
284