Completed
Push — master ( 55947d...9a150a )
by
unknown
9s
created

SktmController::create()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 8
nop 0
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Sktm\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Sktm\Facades\SktmFacade;
9
10
/* Models */
11
use Bantenprov\Sktm\Models\Bantenprov\Sktm\Sktm;
12
use Bantenprov\Sktm\Models\Bantenprov\Sktm\MasterSktm;
13
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
14
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai;
15
use App\User;
16
17
/* Etc */
18
use Validator;
19
20
/**
21
 * The SktmController class.
22
 *
23
 * @package Bantenprov\Sktm
24
 * @author  bantenprov <[email protected]>
25
 */
26
class SktmController extends Controller
27
{
28
    /**
29
     * Create a new controller instance.
30
     *
31
     * @return void
32
     */
33
    protected $sktm;
34
    protected $siswa;
35
    protected $nilai;
36
    protected $master_sktm;
37
    protected $user;
38
39
    public function __construct(Sktm $sktm, MasterSktm $master_sktm, User $user, Siswa $siswa, Nilai $nilai)
40
    {
41
        $this->sktm = $sktm;
42
        $this->siswa = $siswa;
43
        $this->master_sktm = $master_sktm;
44
        $this->user = $user;
45
        $this->nilai = $nilai;
46
    }
47
48
    /**
49
     * Display a listing of the resource.
50
     *
51
     * @return \Illuminate\Http\Response
52
     */
53 View Code Duplication
    public function index(Request $request)
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...
54
    {
55
        if (request()->has('sort')) {
56
            list($sortCol, $sortDir) = explode('|', request()->sort);
57
58
            $query = $this->sktm->orderBy($sortCol, $sortDir);
59
        } else {
60
            $query = $this->sktm->orderBy('id', 'asc');
61
        }
62
63
        if ($request->exists('filter')) {
64
            $query->where(function($q) use($request) {
65
                $value = "%{$request->filter}%";
66
                $q->where('id', 'like', $value)
67
                    ->orWhere('nilai_sktm', 'like', $value);
68
            });
69
        }
70
71
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
72
        $response = $query->with('user')->with('master_sktm')->with('siswa')->paginate($perPage);
73
74
        /*foreach($response as $master_sktm){
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
75
            array_set($response->data, 'master_sktm', $master_sktm->master_sktm->nama);
76
        }
77
78
        foreach($response as $user){
79
            array_set($response->data, 'user', $user->user->name);
80
        }*/
81
82
        return response()->json($response)
83
            ->header('Access-Control-Allow-Origin', '*')
84
            ->header('Access-Control-Allow-Methods', 'GET');
85
    }
86
87
    /**
88
     * Show the form for creating a new resource.
89
     *
90
     * @return \Illuminate\Http\Response
91
     */
92
93
    public function create()
94
    {
95
        $users = $this->user->all();
96
        $master_sktms = $this->master_sktm->all();
97
        $siswas = $this->siswa->all();
98
99
        foreach($users as $user){
100
            array_set($user, 'label', $user->name);
101
        }
102
103
        foreach($master_sktms as $master_sktm){
104
            array_set($master_sktm, 'label', $master_sktm->instansi);
105
        }
106
107
        foreach($siswas as $siswa){
108
            array_set($siswa, 'label', $siswa->nama_siswa);
109
        }
110
        
111
        $response['master_sktm'] = $master_sktms;
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...
112
        $response['siswa'] = $siswas;
113
        $response['user'] = $users;
114
        $response['status'] = true;
115
116
        return response()->json($response);
117
    }
118
119
    /**
120
     * Display the specified resource.
121
     *
122
     * @param  \App\Sktm  $sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. 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...
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function store(Request $request)
126
    {
127
        $sktm = $this->sktm;
128
        $nilai_sktm = $request->nilai_sktm;
129
130
        $validator = Validator::make($request->all(), [
131
            'user_id' => 'required|unique:sktms,user_id',
132
            'siswa_id' => 'required|unique:sktms,siswa_id',
133
            'master_sktm_id' => 'required|unique:sktms,master_sktm_id',
134
            'no_sktm' => 'required',
135
            'nilai_sktm' => 'required',
136
        ]);
137
138
        if($validator->fails()){
139
            $check = $sktm->where('user_id', $request->user_id)->orWhere('master_sktm_id',$request->master_sktm_id)->orWhere('siswa_id',$request->siswa_id)->whereNull('deleted_at')->count();
140
141
            if ($check > 0) {
142
                $response['message'] = 'Failed ! Username, Nama Siswa, Master SKTM, 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...
143 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...
144
                $sktm->user_id = $request->input('user_id');
145
                $sktm->siswa_id = $request->input('siswa_id');
146
                $sktm->master_sktm_id = $request->input('master_sktm_id');
147
                $sktm->no_sktm = $request->input('no_sktm');
148
                $sktm->nilai_sktm = $request->input('nilai_sktm');
149
                $sktm->save();
150
151
                $check_sktm = $this->nilai->where('siswa_id', $request->input('siswa_id'));
152
                if($check_sktm->count() > 0){
153
                    $this->nilai->where('siswa_id', $request->input('siswa_id'))->update([
154
                        'user_id' => $request->input('user_id'),
155
                        'siswa_id' => $request->input('siswa_id'),
156
                        'sktm' => $nilai_sktm
157
                    ]);
158
                }else{
159
                    $this->nilai->create([
160
                        'user_id' => $request->input('user_id'),
161
                        'siswa_id' => $request->input('siswa_id'),
162
                        'sktm' => $nilai_sktm
163
                    ]);
164
                }
165
166
                $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...
167
            }
168 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...
169
                $sktm->user_id = $request->input('user_id');
170
                $sktm->siswa_id = $request->input('siswa_id');
171
                $sktm->master_sktm_id = $request->input('master_sktm_id');
172
                $sktm->no_sktm = $request->input('no_sktm');
173
                $sktm->nilai_sktm = $request->input('nilai_sktm');
174
                $sktm->save();
175
176
                $check_sktm = $this->nilai->where('siswa_id', $request->input('siswa_id'));
177
                if($check_sktm->count() > 0){
178
                    $this->nilai->where('siswa_id', $request->input('siswa_id'))->update([
179
                        'user_id' => $request->input('user_id'),
180
                        'siswa_id' => $request->input('siswa_id'),
181
                        'sktm' => $nilai_sktm
182
                    ]);
183
                }else{
184
                    $this->nilai->create([
185
                        'user_id' => $request->input('user_id'),
186
                        'siswa_id' => $request->input('siswa_id'),
187
                        'sktm' => $nilai_sktm
188
                    ]);
189
                }
190
191
                $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...
192
        }
193
194
        $response['status'] = true;
195
196
        return response()->json($response);
197
    }
198
199
    /**
200
     * Store a newly created resource in storage.
201
     *
202
     * @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...
203
     * @return \Illuminate\Http\Response
204
     */
205
    public function show($id)
206
    {
207
        $sktm = $this->sktm->findOrFail($id);
208
        
209
        $response['user'] = $sktm->user;
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...
210
        $response['master_sktm'] = $sktm->master_sktm;
211
        $response['siswa'] = $sktm->siswa;
212
        $response['sktm'] = $sktm;
213
        $response['status'] = true;
214
215
        return response()->json($response);
216
    }
217
218
    /**
219
     * Show the form for editing the specified resource.
220
     *
221
     * @param  \App\Sktm  $sktm
222
     * @return \Illuminate\Http\Response
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. 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...
223
     */
224
225
    public function edit($id)
226
    {
227
        $sktm = $this->sktm->findOrFail($id);
228
229
        array_set($sktm->user, 'label', $sktm->user->name);
230
        array_set($sktm->master_sktm, 'label', $sktm->master_sktm->instansi);
231
        array_set($sktm->siswa, 'label', $sktm->siswa->nama_siswa);
232
        
233
        $response['master_sktm'] = $sktm->master_sktm;
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...
234
        $response['sktm'] = $sktm;
235
        $response['siswa'] = $sktm->siswa;
236
        $response['user'] = $sktm->user;
237
        $response['status'] = true;
238
239
        return response()->json($response);
240
    }
241
242
    /**
243
     * Update the specified resource in storage.
244
     *
245
     * @param  \Illuminate\Http\Request  $request
246
     * @param  \App\Sktm  $sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. 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...
247
     * @return \Illuminate\Http\Response
248
     */
249
    public function update(Request $request, $id)
250
    {   
251
        $response = array();
252
        $message  = array();
253
        
254
        $sktm = $this->sktm->findOrFail($id);
255
        $nilai_sktm = $request->nilai_sktm;
256
257
        /*if ($request->input('master_sktm_id') == $request->input('master_sktm_id'))
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
258
        {*/
259
            $validator = Validator::make($request->all(), [
260
                'user_id' => 'required|unique:sktms,user_id,'.$id,
261
                'siswa_id' => 'required|unique:sktms,siswa_id,'.$id,
262
                'master_sktm_id' => 'required',
263
                'no_sktm' => 'required',
264
                'nilai_sktm' => 'required',
265
                
266
            ]);
267
268
        if ($validator->fails()) {
269
270
            foreach($validator->messages()->getMessages() as $key => $error){
271
                        foreach($error AS $error_get) {
272
                            array_push($message, $error_get);
273
                        }                
274
                    } 
275
276
             $check_user     = $this->sktm->where('id','!=', $id)->where('user_id', $request->user_id);
277
             $check_siswa = $this->sktm->where('id','!=', $id)->where('siswa_id', $request->siswa_id);
278
279
             if($check_user->count() > 0 || $check_siswa->count() > 0){
280
                  $response['message'] = implode("\n",$message);
281
282 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...
283
                $sktm->user_id = $request->input('user_id');
284
                $sktm->siswa_id = $request->input('siswa_id');
285
                $sktm->master_sktm_id = $request->input('master_sktm_id');
286
                $sktm->no_sktm = $request->input('no_sktm');
287
                $sktm->nilai_sktm = $request->input('nilai_sktm');
288
                $sktm->save();
289
290
                $check_sktm = $this->nilai->where('siswa_id', $request->input('siswa_id'));
291
                if($check_sktm->count() > 0){
292
                    $this->nilai->where('siswa_id', $request->input('siswa_id'))->update([
293
                        'user_id' => $request->input('user_id'),
294
                        'siswa_id' => $request->input('siswa_id'),
295
                        'sktm' => $nilai_sktm
296
                    ]);
297
                }
298
299
                $response['message'] = 'success';
300
            }
301 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...
302
                $sktm->user_id = $request->input('user_id');
303
                $sktm->siswa_id = $request->input('siswa_id');
304
                $sktm->master_sktm_id = $request->input('master_sktm_id');
305
                $sktm->no_sktm = $request->input('no_sktm');
306
                $sktm->nilai_sktm = $request->input('nilai_sktm');
307
                $sktm->save();
308
309
                $check_sktm = $this->nilai->where('siswa_id', $request->input('siswa_id'));
310
                if($check_sktm->count() > 0){
311
                    $this->nilai->where('siswa_id', $request->input('siswa_id'))->update([
312
                        'user_id' => $request->input('user_id'),
313
                        'siswa_id' => $request->input('siswa_id'),
314
                        'sktm' => $nilai_sktm
315
                    ]);
316
                }
317
318
               $response['message'] = 'success';
319
        }
320
321
        $response['status'] = true;
322
323
        return response()->json($response);
324
    }
325
326
    /**
327
     * Remove the specified resource from storage.
328
     *
329
     * @param  \App\Sktm  $sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. 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...
330
     * @return \Illuminate\Http\Response
331
     */
332 View Code Duplication
    public function destroy($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...
333
    {
334
        $sktm = $this->sktm->findOrFail($id);
335
336
        if ($sktm->delete()) {
337
            $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...
338
        } else {
339
            $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...
340
        }
341
342
        return json_encode($response);
343
    }
344
}
345