Completed
Push — master ( 8f960c...3e2a1d )
by
unknown
03:01 queued 01:50
created

ZonaController::update()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 64
Code Lines 46

Duplication

Lines 26
Ratio 40.63 %

Importance

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