Completed
Push — master ( e369c3...96817d )
by
unknown
04:53 queued 03:06
created

OrangTuaController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 290
Duplicated Lines 21.03 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 61
loc 290
rs 10
c 0
b 0
f 0
wmc 23
lcom 2
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B index() 0 25 4
B create() 0 34 4
A store() 30 58 3
A show() 0 11 1
A edit() 0 14 1
C update() 31 69 7
A destroy() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bantenprov\OrangTua\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\OrangTua\Facades\OrangTuaFacade;
9
10
/* Models */
11
use Bantenprov\OrangTua\Models\Bantenprov\OrangTua\OrangTua;
12
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
13
use App\User;
14
15
/* Etc */
16
use Validator;
17
18
/**
19
 * The OrangTuaController class.
20
 *
21
 * @package Bantenprov\OrangTua
22
 * @author  bantenprov <[email protected]>
23
 */
24
class OrangTuaController extends Controller
25
{
26
    /**
27
     * Create a new controller instance.
28
     *
29
     * @return void
30
     */
31
32
     protected $orang_tua;
33
     protected $siswa;
34
     protected $user;
35
36
    public function __construct(OrangTua $orang_tua, User $user, Siswa $siswa)
37
    {
38
        $this->orang_tua = $orang_tua;
39
        $this->siswa = $siswa;
40
        $this->user = $user;
41
    }
42
43
    /**
44
     * Display a listing of the resource.
45
     *
46
     * @return \Illuminate\Http\Response
47
     */
48
    public function index(Request $request)
49
    {
50
        if ($request->has('sort')) {
51
            list($sortCol, $sortDir) = explode('|', $request->sort);
52
53
            $query = $this->orang_tua->orderBy($sortCol, $sortDir);
54
        } else {
55
            $query = $this->orang_tua->orderBy('id', 'asc');
56
        }
57
58
        if ($request->exists('filter')) {
59
            $query->where(function($q) use($request) {
60
               $value = "%{$request->filter}%";
61
                $q->where('id', 'like', $value)
62
                  ->orWhere('nama_ayah', 'like', $value);
63
            });
64
        }
65
66
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
67
        $response = $query->with('user')->with('siswa')->paginate($perPage);
68
69
        return response()->json($response)
70
            ->header('Access-Control-Allow-Origin', '*')
71
            ->header('Access-Control-Allow-Methods', 'GET');
72
    }
73
74
    /**
75
     * Show the form for creating a new resource.
76
     *
77
     * @return \Illuminate\Http\Response
78
     */
79
    public function create()
80
    {
81
        $response = [];
82
        $siswas = $this->siswa->all();
83
        $users_special = $this->user->all();
84
        $users_standar = $this->user->find(\Auth::User()->id);
85
        $current_user = \Auth::User();
86
87
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
88
89
        if($role_check){
90
            $response['user_special'] = true;
91
            foreach($users_special as $user){
92
                array_set($user, 'label', $user->name);
93
            }
94
            $response['user'] = $users_special;
95
        }else{
96
            $response['user_special'] = false;
97
            array_set($users_standar, 'label', $users_standar->name);
98
            $response['user'] = $users_standar;
99
        }
100
101
        array_set($current_user, 'label', $current_user->name);
102
103
        foreach($siswas as $siswa){
104
            array_set($siswa, 'label', $siswa->nama_siswa);
105
        }
106
107
        $response['current_user'] = $current_user;
108
        $response['siswa'] = $siswas;
109
        $response['status'] = true;
110
111
        return response()->json($response);
112
    }
113
114
    /**
115
     * Display the specified resource.
116
     *
117
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
118
     * @return \Illuminate\Http\Response
119
     */
120
    public function store(Request $request)
121
    {
122
        $orang_tua = $this->orang_tua;
123
124
        $validator = Validator::make($request->all(), [
125
            'user_id' => 'required|unique:orangtuas,user_id',
126
            'nomor_un' => 'required|unique:orangtuas,nomor_un',
127
            'no_telp'   => 'required|unique:orangtuas,no_telp',
128
            'nama_ayah'   => 'required',
129
            'nama_ibu'   => 'required',
130
            'pendidikan_ayah'   => 'required',
131
            'kerja_ayah'   => 'required',
132
            'pendidikan_ibu'   => 'required',
133
            'kerja_ibu'   => 'required',
134
            'alamat_ortu'   => 'required',
135
        ]);
136
137
        if($validator->fails()){
138
            $check = $orang_tua->where('user_id',$request->user_id)->orWhere('nomor_un',$request->nomor_un)->orWhere('no_telp',$request->no_telp)->whereNull('deleted_at')->count();
139
140
            if ($check > 0) {
141
                $response['message'] = 'Failed ! Username, Nama Siswa, Nomor Telp 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...
142 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...
143
            $orang_tua->user_id = $request->input('user_id');
144
            $orang_tua->nomor_un = $request->input('nomor_un');
145
            $orang_tua->no_telp = $request->input('no_telp');
146
            $orang_tua->nama_ayah = $request->input('nama_ayah');
147
            $orang_tua->nama_ibu = $request->input('nama_ibu');
148
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
149
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
150
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
151
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
152
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
153
            $orang_tua->save();
154
155
            $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...
156
            }
157
158 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...
159
            $orang_tua->user_id = $request->input('user_id');
160
            $orang_tua->nomor_un = $request->input('nomor_un');
161
            $orang_tua->no_telp = $request->input('no_telp');
162
            $orang_tua->nama_ayah = $request->input('nama_ayah');
163
            $orang_tua->nama_ibu = $request->input('nama_ibu');
164
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
165
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
166
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
167
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
168
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
169
            $orang_tua->save();
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
            }
173
174
        $response['status'] = true;
175
176
        return response()->json($response);
177
    }
178
179
    /**
180
     * Store a newly created resource in storage.
181
     *
182
     * @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...
183
     * @return \Illuminate\Http\Response
184
     */
185
    public function show($id)
186
    {
187
        $orang_tua = $this->orang_tua->findOrFail($id);
188
189
        $response['user'] = $orang_tua->user;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
190
        $response['siswa'] = $orang_tua->siswa;
191
        $response['orang_tua'] = $orang_tua;
192
        $response['status'] = true;
193
194
        return response()->json($response);
195
    }
196
197
    /**
198
     * Show the form for editing the specified resource.
199
     *
200
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
201
     * @return \Illuminate\Http\Response
202
     */
203
    public function edit($id)
204
    {
205
        $orang_tua = $this->orang_tua->findOrFail($id);
206
207
        array_set($orang_tua->user, 'label', $orang_tua->user->name);
208
        array_set($orang_tua->siswa, 'label', $orang_tua->siswa->nama_siswa);
209
210
        $response['user'] = $orang_tua->user;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
211
        $response['siswa'] = $orang_tua->siswa;
212
        $response['orang_tua'] = $orang_tua;
213
        $response['status'] = true;
214
215
        return response()->json($response);
216
    }
217
218
    /**
219
     * Update the specified resource in storage.
220
     *
221
     * @param  \Illuminate\Http\Request  $request
222
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. Was it maybe removed?

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

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

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

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

Loading history...
223
     * @return \Illuminate\Http\Response
224
     */
225
    public function update(Request $request, $id)
226
    {
227
        $response = array();
228
        $message  = array();
229
        $orang_tua = $this->orang_tua->findOrFail($id);
230
231
        $validator = Validator::make($request->all(), [
232
            'user_id' => 'required|unique:orangtuas,user_id,'.$id,
233
            'nomor_un' => 'required|unique:orangtuas,nomor_un,'.$id,
234
            'no_telp'   => 'required|unique:orangtuas,no_telp,'.$id,
235
            'nama_ayah'   => 'required',
236
            'nama_ibu'   => 'required',
237
            'pendidikan_ayah'   => 'required',
238
            'kerja_ayah'   => 'required',
239
            'pendidikan_ibu'   => 'required',
240
            'kerja_ibu'   => 'required',
241
            'alamat_ortu'   => 'required',
242
        ]);
243
244
        if ($validator->fails()) {
245
246
            foreach($validator->messages()->getMessages() as $key => $error){
247
                        foreach($error AS $error_get) {
248
                            array_push($message, $error_get);
249
                        }
250
                    }
251
252
             $check_user     = $this->orang_tua->where('id','!=', $id)->where('user_id', $request->user_id);
253
             $check_siswa = $this->orang_tua->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
254
             $check_no_telp = $this->orang_tua->where('id','!=', $id)->where('no_telp', $request->no_telp);
255
256
             if($check_user->count() > 0 || $check_siswa->count() > 0 || $check_no_telp->count() > 0){
257
                  $response['message'] = implode("\n",$message);
258 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...
259
            $orang_tua->user_id    = $request->input('user_id');
260
            $orang_tua->nomor_un = $request->input('nomor_un');
261
            $orang_tua->no_telp = $request->input('no_telp');
262
            $orang_tua->nama_ayah = $request->input('nama_ayah');
263
            $orang_tua->nama_ibu = $request->input('nama_ibu');
264
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
265
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
266
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
267
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
268
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
269
            $orang_tua->save();
270
271
            $response['message'] = 'success';
272
273
          }
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
            $orang_tua->user_id    = $request->input('user_id');
276
            $orang_tua->nomor_un = $request->input('nomor_un');
277
            $orang_tua->no_telp = $request->input('no_telp');
278
            $orang_tua->nama_ayah = $request->input('nama_ayah');
279
            $orang_tua->nama_ibu = $request->input('nama_ibu');
280
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
281
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
282
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
283
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
284
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
285
            $orang_tua->save();
286
287
            $response['message'] = 'success';
288
        }
289
290
        $response['status'] = true;
291
292
        return response()->json($response);
293
    }
294
295
    /**
296
     * Remove the specified resource from storage.
297
     *
298
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
299
     * @return \Illuminate\Http\Response
300
     */
301
    public function destroy($id)
302
    {
303
        $orang_tua = $this->orang_tua->findOrFail($id);
304
305
        if ($orang_tua->delete()) {
306
            $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...
307
        } else {
308
            $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...
309
        }
310
311
        return json_encode($response);
312
    }
313
}
314