Completed
Push — master ( cd3ce6...16c880 )
by
unknown
10s
created

ZonaController::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Zona\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\DB;
9
use Bantenprov\Zona\Facades\ZonaFacade;
10
11
/* Models */
12
use Bantenprov\Zona\Models\Bantenprov\Zona\Zona;
13
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
14
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
15
use App\User;
16
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai;
17
18
/* Etc */
19
use Validator;
20
use Auth;
21
22
/**
23
 * The ZonaController class.
24
 *
25
 * @package Bantenprov\Zona
26
 * @author  bantenprov <[email protected]>
27
 */
28
class ZonaController extends Controller
29
{
30
    protected $zona;
31
    protected $siswa;
32
    protected $sekolah;
33
    protected $user;
34
    protected $nilai;
35
36
    /**
37
     * Create a new controller instance.
38
     *
39
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
40
     */
41
    public function __construct()
42
    {
43
        $this->zona     = new Zona;
44
        $this->siswa    = new Siswa;
45
        $this->sekolah  = new Sekolah;
46
        $this->user     = new User;
47
        $this->nilai    = new Nilai;
48
    }
49
50
    /**
51
     * Display a listing of the resource.
52
     *
53
     * @return \Illuminate\Http\Response
54
     */
55
    public function index(Request $request)
56
    {
57 View Code Duplication
        if (request()->has('sort')) {
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...
58
            list($sortCol, $sortDir) = explode('|', request()->sort);
59
60
            $query = $this->zona->orderBy($sortCol, $sortDir);
61
        } else {
62
            $query = $this->zona->orderBy('id', 'asc');
63
        }
64
65
        if ($request->exists('filter')) {
66
            $query->where(function($q) use($request) {
67
                $value = "%{$request->filter}%";
68
69
                $q->where('nomor_un', 'like', $value)
70
                    ->orWhere('lokasi_siswa', 'like', $value)
71
                    ->orWhere('lokasi_sekolah', 'like', $value)
72
                    ->orWhere('nilai', 'like', $value);
73
            });
74
        }
75
76
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
77
78
        $response   = $query->with(['siswa', 'sekolah', 'user'])->paginate($perPage);
79
80
        return response()->json($response)
81
            ->header('Access-Control-Allow-Origin', '*')
82
            ->header('Access-Control-Allow-Methods', 'GET');
83
    }
84
85
    /**
86
     * Display a listing of the resource.
87
     *
88
     * @return \Illuminate\Http\Response
89
     */
90
    public function get()
91
    {
92
        $zonas = $this->zona->with(['siswa', 'sekolah', 'user'])->get();
93
94
        foreach ($zonas as $zona) {
95
            if ($zona->siswa !== null) {
96
                array_set($zona, 'label', $zona->siswa->nomor_un.' - '.$zona->siswa->nama_siswa);
97
            } else {
98
                array_set($zona, 'label', $zona->nomor_un.' - ');
99
            }
100
        }
101
102
        $response['zonas']      = $zonas;
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...
103
        $response['error']      = false;
104
        $response['message']    = 'Success';
105
        $response['status']     = true;
106
107
        return response()->json($response);
108
    }
109
110
    /**
111
     * Show the form for creating a new resource.
112
     *
113
     * @return \Illuminate\Http\Response
114
     */
115
    public function create()
116
    {
117
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
118
        $zona           = $this->zona->getAttributes();
119
        $siswas         = $this->siswa->getAttributes();
120
        $sekolahs       = $this->sekolah->getAttributes();
121
        $users          = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
        $users_special  = $this->user->all();
123
        $users_standar  = $this->user->findOrFail($user_id);
124
        $current_user   = Auth::User();
125
126
        foreach ($siswas as $siswa) {
127
            array_set($siswa, 'label', $siswa->nomor_un.' - '.$siswa->nama_siswa);
128
        }
129
130
        foreach ($sekolahs as $sekolah) {
131
            array_set($sekolah, 'label', $sekolah->nama);
132
        }
133
134
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
135
136 View Code Duplication
        if ($role_check) {
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...
137
            $user_special = true;
138
139
            foreach($users_special as $user){
140
                array_set($user, 'label', $user->name);
141
            }
142
143
            $users = $users_special;
144
        } else {
145
            $user_special = false;
146
147
            array_set($users_standar, 'label', $users_standar->name);
148
149
            $users = $users_standar;
150
        }
151
152
        array_set($current_user, 'label', $current_user->name);
153
154
        $response['zona']           = $zona;
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...
155
        $response['siswas']         = $siswas;
156
        $response['sekolahs']       = $sekolahs;
157
        $response['users']          = $users;
158
        $response['user_special']   = $user_special;
159
        $response['current_user']   = $current_user;
160
        $response['error']          = false;
161
        $response['message']        = 'Success';
162
        $response['status']         = true;
163
164
        return response()->json($response);
165
    }
166
167
    /**
168
     * Store a newly created resource in storage.
169
     *
170
     * @param  \Illuminate\Http\Request  $request
171
     * @return \Illuminate\Http\Response
172
     */
173
    public function store(Request $request)
174
    {
175
        $zona = $this->zona;
176
177
        $validator = Validator::make($request->all(), [
178
            'nomor_un'          => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,NULL,id,deleted_at,NULL",
179
            // 'sekolah_id'        => "required|exists:{$this->sekolah->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
180
            // 'zona_siswa'     => "required|exists:{$this->city->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
181
            // 'zona_sekolah'   => "required|exists:{$this->village->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
182
            // 'lokasi_siswa'   => "required|exists:{$this->district->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
183
            // 'lokasi_sekolah' => "required|exists:{$this->village->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
184
            // 'nilai'             => 'required|numeric',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
185
            'user_id'           => "required|exists:{$this->user->getTable()},id",
186
        ]);
187
188 View Code Duplication
        if ($validator->fails()) {
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...
189
            $error      = true;
190
            $message    = $validator->errors()->first();
191
        } else {
192
            $nomor_un       = $request->input('nomor_un');
193
            $siswa          = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first();
194
            $zona_siswa     = substr($siswa->village_id, 0, 6);
195
            $zona_sekolah   = substr($siswa->sekolah->village_id, 0, 6);
196
            $lokasi_siswa   = $siswa->village_id;
197
            $lokasi_sekolah = $siswa->sekolah->village_id;
198
199
            $zona->nomor_un         = $nomor_un;
200
            $zona->sekolah_id       = $siswa->sekolah->id;
201
            $zona->zona_siswa       = $zona_siswa;
202
            $zona->zona_sekolah     = $zona_sekolah;
203
            $zona->lokasi_siswa     = $lokasi_siswa;
204
            $zona->lokasi_sekolah   = $lokasi_sekolah;
205
            $zona->nilai            = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah);
206
            $zona->user_id          = $request->input('user_id');
207
208
            $nilai = $this->nilai->updateOrCreate(
209
                [
210
                    'nomor_un'  => $zona->nomor_un,
211
                ],
212
                [
213
                    'nomor_un'  => $zona->nomor_un,
214
                    'zona'      => $zona->nilai,
215
                    'total'     => null,
216
                    'user_id'   => $zona->user_id,
217
                ]
218
            );
219
220
            DB::beginTransaction();
221
222
            if ($zona->save() && $nilai->save())
223
            {
224
                DB::commit();
225
226
                $error      = false;
227
                $message    = 'Success';
228
            } else {
229
                DB::rollBack();
230
231
                $error      = true;
232
                $message    = 'Failed';
233
            }
234
        }
235
236
        $response['zona']       = $zona;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

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

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

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

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

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

Loading history...
248
     * @return \Illuminate\Http\Response
249
     */
250 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...
251
    {
252
        $zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
253
254
        $response['zona']       = $zona;
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...
255
        $response['error']      = false;
256
        $response['message']    = 'Success';
257
        $response['status']     = true;
258
259
        return response()->json($response);
260
    }
261
262
    /**
263
     * Show the form for editing the specified resource.
264
     *
265
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
266
     * @return \Illuminate\Http\Response
267
     */
268
    public function edit($id)
269
    {
270
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
271
        $zona           = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
272
        $siswas         = $this->siswa->getAttributes();
273
        $sekolahs       = $this->sekolah->getAttributes();
274
        $users          = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
275
        $users_special  = $this->user->all();
276
        $users_standar  = $this->user->findOrFail($user_id);
277
        $current_user   = Auth::User();
278
279
        if ($zona->siswa !== null) {
280
            array_set($zona->siswa, 'label', $zona->siswa->nomor_un.' - '.$zona->siswa->nama_siswa);
281
        }
282
283
        if ($zona->sekolah !== null) {
284
            array_set($zona->sekolah, 'label', $zona->sekolah->nama);
285
        }
286
287
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
288
289
        if ($zona->user !== null) {
290
            array_set($zona->user, 'label', $zona->user->name);
291
        }
292
293 View Code Duplication
        if ($role_check) {
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...
294
            $user_special = true;
295
296
            foreach($users_special as $user){
297
                array_set($user, 'label', $user->name);
298
            }
299
300
            $users = $users_special;
301
        } else {
302
            $user_special = false;
303
304
            array_set($users_standar, 'label', $users_standar->name);
305
306
            $users = $users_standar;
307
        }
308
309
        array_set($current_user, 'label', $current_user->name);
310
311
        $response['zona']           = $zona;
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...
312
        $response['siswas']         = $siswas;
313
        $response['sekolahs']       = $sekolahs;
314
        $response['users']          = $users;
315
        $response['user_special']   = $user_special;
316
        $response['current_user']   = $current_user;
317
        $response['error']          = false;
318
        $response['message']        = 'Success';
319
        $response['status']         = true;
320
321
        return response()->json($response);
322
    }
323
324
    /**
325
     * Update the specified resource in storage.
326
     *
327
     * @param  \Illuminate\Http\Request  $request
328
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
329
     * @return \Illuminate\Http\Response
330
     */
331
    public function update(Request $request, $id)
332
    {
333
        $zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
334
335
        $validator = Validator::make($request->all(), [
336
            // 'nomor_un'          => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,{$id},id,deleted_at,NULL",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
337
            // 'sekolah_id'        => "required|exists:{$this->sekolah->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
338
            // 'zona_siswa'     => "required|exists:{$this->city->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
339
            // 'zona_sekolah'   => "required|exists:{$this->village->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
340
            // 'lokasi_siswa'   => "required|exists:{$this->district->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
341
            // 'lokasi_sekolah' => "required|exists:{$this->village->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
342
            // 'nilai'             => 'required|numeric',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
343
            'user_id'           => "required|exists:{$this->user->getTable()},id",
344
        ]);
345
346 View Code Duplication
        if ($validator->fails()) {
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...
347
            $error      = true;
348
            $message    = $validator->errors()->first();
349
        } else {
350
            $nomor_un       = $zona->nomor_un; // $request->input('nomor_un');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
351
            $siswa          = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first();
352
            $zona_siswa     = substr($siswa->village_id, 0, 6);
353
            $zona_sekolah   = substr($siswa->sekolah->village_id, 0, 6);
354
            $lokasi_siswa   = $siswa->village_id;
355
            $lokasi_sekolah = $siswa->sekolah->village_id;
356
357
            $zona->nomor_un         = $nomor_un;
358
            $zona->sekolah_id       = $siswa->sekolah->id;
359
            $zona->zona_siswa       = $zona_siswa;
360
            $zona->zona_sekolah     = $zona_sekolah;
361
            $zona->lokasi_siswa     = $lokasi_siswa;
362
            $zona->lokasi_sekolah   = $lokasi_sekolah;
363
            $zona->nilai            = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah);
364
            $zona->user_id          = $request->input('user_id');
365
366
            $nilai = $this->nilai->updateOrCreate(
367
                [
368
                    'nomor_un'  => $zona->nomor_un,
369
                ],
370
                [
371
                    'nomor_un'  => $zona->nomor_un,
372
                    'zona'      => $zona->nilai,
373
                    'total'     => null,
374
                    'user_id'   => $zona->user_id,
375
                ]
376
            );
377
378
            DB::beginTransaction();
379
380
            if ($zona->save() && $nilai->save())
381
            {
382
                DB::commit();
383
384
                $error      = false;
385
                $message    = 'Success';
386
            } else {
387
                DB::rollBack();
388
389
                $error      = true;
390
                $message    = 'Failed';
391
            }
392
        }
393
394
        $response['zona']       = $zona;
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...
395
        $response['error']      = $error;
396
        $response['message']    = $message;
397
        $response['status']     = true;
398
399
        return response()->json($response);
400
    }
401
402
    /**
403
     * Remove the specified resource from storage.
404
     *
405
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
406
     * @return \Illuminate\Http\Response
407
     */
408 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...
409
    {
410
        $zona = $this->zona->findOrFail($id);
411
412
        if ($zona->delete()) {
413
            $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...
414
            $response['success']    = true;
415
            $response['status']     = true;
416
        } else {
417
            $response['message']    = 'Failed';
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...
418
            $response['success']    = false;
419
            $response['status']     = false;
420
        }
421
422
        return json_encode($response);
423
    }
424
}
425