Completed
Push — master ( a64ec4...774d2e )
by
unknown
10s
created

ZonaController::update()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 68
Code Lines 50

Duplication

Lines 28
Ratio 41.18 %

Importance

Changes 0
Metric Value
cc 8
eloc 50
nc 7
nop 2
dl 28
loc 68
rs 6.5974
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($siswas as $siswa){
111
            array_set($siswa, 'label', $siswa->nama_siswa);
112
        }
113
114
        $response['siswa'] = $siswas;
115
        $response['current_user'] = $current_user;
116
        $response['master_zona'] = $master_zonas;
117
        $response['sekolah'] = $sekolahs;
118
        $response['status'] = true;
119
120
        return response()->json($response);
121
    }
122
123
    /**
124
     * Display the specified resource.
125
     *
126
     * @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...
127
     * @return \Illuminate\Http\Response
128
     */
129
    public function store(Request $request)
130
    {
131
        $zona = $this->zona;
132
133
        $validator = Validator::make($request->all(), [
134
            'user_id' => 'required|unique:zonas,user_id',
135
            'master_zona_id' => 'required|unique:zonas,master_zona_id',
136
            'nomor_un' => 'required|unique:zonas,nomor_un',
137
            'sekolah_id' => 'required',
138
            'zona_siswa' => 'required',
139
            'zona_sekolah' => 'required',
140
            'lokasi_siswa' => 'required',
141
            'lokasi_sekolah' => 'required',
142
            'nilai_zona' => 'required',
143
        ]);
144
145
        if($validator->fails()){
146
            $check = $zona->where('user_id',$request->user_id)->orWhere('nomor_un',$request->nomor_un)->orWhere('master_zona_id',$request->master_zona_id)->whereNull('deleted_at')->count();
147
148
            if ($check > 0) {
149
                $response['message'] = 'Failed ! Username, Master Zona, 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...
150 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...
151
                $zona->user_id = $request->input('user_id');
152
                $zona->master_zona_id = $request->input('master_zona_id');
153
                $zona->nomor_un = $request->input('nomor_un');
154
                $zona->sekolah_id = $request->input('sekolah_id');
155
                $zona->zona_siswa = $request->input('zona_siswa');
156
                $zona->zona_sekolah = $request->input('zona_sekolah');
157
                $zona->lokasi_siswa = $request->input('lokasi_siswa');
158
                $zona->lokasi_sekolah = $request->input('lokasi_sekolah');
159
                $zona->nilai_zona = $request->input('nilai_zona');
160
                $zona->save();
161
162
                $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...
163
            }
164 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...
165
            $zona->user_id = $request->input('user_id');
166
                $zona->master_zona_id = $request->input('master_zona_id');
167
                $zona->nomor_un = $request->input('nomor_un');
168
                $zona->sekolah_id = $request->input('sekolah_id');
169
                $zona->zona_siswa = $request->input('zona_siswa');
170
                $zona->zona_sekolah = $request->input('zona_sekolah');
171
                $zona->lokasi_siswa = $request->input('lokasi_siswa');
172
                $zona->lokasi_sekolah = $request->input('lokasi_sekolah');
173
                $zona->nilai_zona = $request->input('nilai_zona');
174
                $zona->save();
175
176
            $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...
177
        }
178
179
        $response['status'] = true;
180
181
        return response()->json($response);
182
    }
183
184
    /**
185
     * Store a newly created resource in storage.
186
     *
187
     * @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...
188
     * @return \Illuminate\Http\Response
189
     */
190
    public function show($id)
191
    {
192
        $zona = $this->zona->findOrFail($id);
193
194
        $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...
195
        $response['master_zona'] = $zona->master_zona;
196
        $response['sekolah'] = $zona->sekolah;
197
        $response['siswa'] = $zona->siswa;
198
        $response['user'] = $zona->user;
199
        $response['status'] = true;
200
201
        return response()->json($response);
202
    }
203
204
    /**
205
     * Show the form for editing the specified resource.
206
     *
207
     * @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...
208
     * @return \Illuminate\Http\Response
209
     */
210
    public function edit($id)
211
    {
212
        $zona = $this->zona->findOrFail($id);
213
214
        array_set($zona->user, 'label', $zona->user->name);
215
        array_set($zona->siswa, 'label', $zona->siswa->nama_siswa);
216
217
        $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...
218
        $response['master_zona'] = $zona->master_zona;
219
        $response['sekolah'] = $zona->sekolah;
220
        $response['siswa'] = $zona->siswa;
221
        $response['user'] = $zona->user;
222
        $response['status'] = true;
223
224
        return response()->json($response);
225
    }
226
227
    /**
228
     * Update the specified resource in storage.
229
     *
230
     * @param  \Illuminate\Http\Request  $request
231
     * @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...
232
     * @return \Illuminate\Http\Response
233
     */
234
    public function update(Request $request, $id)
235
    {
236
        $response = array();
237
        $message  = array();
238
239
        $zona = $this->zona->findOrFail($id);
240
241
        $validator = Validator::make($request->all(), [
242
            'user_id' => 'required|unique:zonas,user_id,'.$id,
243
            'master_zona_id' => 'required|unique:zonas,master_zona_id,'.$id,
244
            'nomor_un' => 'required|unique:zonas,nomor_un,'.$id,
245
            'sekolah_id' => 'required',
246
            'zona_siswa' => 'required',
247
            'zona_sekolah' => 'required',
248
            'lokasi_siswa' => 'required',
249
            'lokasi_sekolah' => 'required',
250
            'nilai_zona' => 'required',
251
        ]);
252
253
        if ($validator->fails()) {
254
255
            foreach($validator->messages()->getMessages() as $key => $error){
256
                        foreach($error AS $error_get) {
257
                            array_push($message, $error_get);
258
                        }                
259
                    } 
260
261
             $check_user  = $this->zona->where('id','!=', $id)->where('user_id', $request->user_id);
262
             $check_siswa = $this->zona->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
263
             $check_master_zona = $this->zona->where('id','!=', $id)->where('master_zona_id', $request->master_zona_id);
264
             $check_sekolah = $this->zona->where('id','!=', $id)->where('sekolah_id', $request->sekolah_id);
265
266
             if($check_user->count() > 0 || $check_siswa->count() > 0 || $check_master_zona->count() > 0 || $check_sekolah->count() > 0){
267
                  $response['message'] = implode("\n",$message);
268 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...
269
            $zona->user_id = $request->input('user_id');
270
                $zona->master_zona_id = $request->input('master_zona_id');
271
                $zona->nomor_un = $request->input('nomor_un');
272
                $zona->sekolah_id = $request->input('sekolah_id');
273
                $zona->zona_siswa = $request->input('zona_siswa');
274
                $zona->zona_sekolah = $request->input('zona_sekolah');
275
                $zona->lokasi_siswa = $request->input('lokasi_siswa');
276
                $zona->lokasi_sekolah = $request->input('lokasi_sekolah');
277
                $zona->nilai_zona = $request->input('nilai_zona');
278
                $zona->save();
279
280
            $response['message'] = 'success';
281
        }
282
283 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...
284
            $zona->user_id = $request->input('user_id');
285
                $zona->master_zona_id = $request->input('master_zona_id');
286
                $zona->nomor_un = $request->input('nomor_un');
287
                $zona->sekolah_id = $request->input('sekolah_id');
288
                $zona->zona_siswa = $request->input('zona_siswa');
289
                $zona->zona_sekolah = $request->input('zona_sekolah');
290
                $zona->lokasi_siswa = $request->input('lokasi_siswa');
291
                $zona->lokasi_sekolah = $request->input('lokasi_sekolah');
292
                $zona->nilai_zona = $request->input('nilai_zona');
293
                $zona->save();
294
295
                $response['message'] = 'success';
296
            }
297
298
                $response['status'] = true;
299
300
        return response()->json($response);
301
    }
302
303
    /**
304
     * Remove the specified resource from storage.
305
     *
306
     * @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...
307
     * @return \Illuminate\Http\Response
308
     */
309 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...
310
    {
311
        $zona = $this->zona->findOrFail($id);
312
313
        if ($zona->delete()) {
314
            $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...
315
        } else {
316
            $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...
317
        }
318
319
        return json_encode($response);
320
    }
321
}
322