Completed
Push — master ( 9fc5d7...b12892 )
by
unknown
9s
created

OrangTuaController   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 270
Duplicated Lines 19.63 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 53
loc 270
rs 10
c 0
b 0
f 0
wmc 22
lcom 2
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B index() 0 25 4
A create() 0 13 2
A store() 31 60 3
A show() 10 10 1
A edit() 0 12 1
C update() 0 73 8
A destroy() 12 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 App\User;
13
14
/* Etc */
15
use Validator;
16
17
/**
18
 * The OrangTuaController class.
19
 *
20
 * @package Bantenprov\OrangTua
21
 * @author  bantenprov <[email protected]>
22
 */
23
class OrangTuaController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
31
     protected $orang_tua;
32
     protected $user;
33
34
    public function __construct(OrangTua $orang_tua, User $user)
35
    {
36
        $this->orang_tua = $orang_tua;
37
        $this->user = $user;
38
    }
39
40
    /**
41
     * Display a listing of the resource.
42
     *
43
     * @return \Illuminate\Http\Response
44
     */
45
    public function index(Request $request)
46
    {
47
        if ($request->has('sort')) {
48
            list($sortCol, $sortDir) = explode('|', $request->sort);
49
50
            $query = $this->orang_tua->orderBy($sortCol, $sortDir);
51
        } else {
52
            $query = $this->orang_tua->orderBy('id', 'asc');
53
        }
54
55
        if ($request->exists('filter')) {
56
            $query->where(function($q) use($request) {
57
               $value = "%{$request->filter}%";
58
                $q->where('nomor_un', 'like', $value)
59
                  ->orWhere('no_kk', 'like', $value);
60
            });
61
        }
62
63
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
64
        $response = $query->with('user')->paginate($perPage);
65
66
        return response()->json($response)
67
            ->header('Access-Control-Allow-Origin', '*')
68
            ->header('Access-Control-Allow-Methods', 'GET');
69
    }
70
71
    /**
72
     * Show the form for creating a new resource.
73
     *
74
     * @return \Illuminate\Http\Response
75
     */
76
    public function create()
77
    {
78
        $users = $this->user->all();
79
80
        foreach($users as $user){
81
            array_set($user, 'label', $user->name);
82
        }
83
84
        $response['user'] = $users;
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...
85
        $response['loaded'] = true;
86
87
        return response()->json($response);
88
    }
89
90
    /**
91
     * Display the specified resource.
92
     *
93
     * @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...
94
     * @return \Illuminate\Http\Response
95
     */
96
    public function store(Request $request)
97
    {
98
        $orang_tua = $this->orang_tua;
99
100
        $validator = Validator::make($request->all(), [
101
            'user_id' => 'required|unique:orangtuas,user_id',
102
            'nomor_un'   => 'required|unique:orangtuas,nomor_un',
103
            'no_kk'   => 'required|unique:orangtuas,no_kk',
104
            'no_telp'   => 'required|unique:orangtuas,no_telp',
105
            'nama_ayah'   => 'required',
106
            'nama_ibu'   => 'required',
107
            'pendidikan_ayah'   => 'required',
108
            'kerja_ayah'   => 'required',
109
            'pendidikan_ibu'   => 'required',
110
            'kerja_ibu'   => 'required',
111
            'alamat_ortu'   => 'required',
112
        ]);
113
114
        if($validator->fails()){
115
            $check = $orang_tua->where('user_id',$request->user_id)->orWhere('nomor_un',$request->nomor_un)->orWhere('no_kk',$request->no_kk)->orWhere('no_telp',$request->no_telp)->whereNull('deleted_at')->count();
116
117
            if ($check > 0) {
118
                $response['message'] = 'Failed ! Username, Nomor UN, Nomor KK, 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...
119 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...
120
            $orang_tua->user_id = $request->input('user_id');
121
            $orang_tua->nomor_un = $request->input('nomor_un');
122
            $orang_tua->no_kk = $request->input('no_kk');
123
            $orang_tua->no_telp = $request->input('no_telp');
124
            $orang_tua->nama_ayah = $request->input('nama_ayah');
125
            $orang_tua->nama_ibu = $request->input('nama_ibu');
126
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
127
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
128
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
129
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
130
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
131
            $orang_tua->save();
132
133
            $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...
134
            }
135
136 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...
137
            $orang_tua->user_id = $request->input('user_id');
138
            $orang_tua->nomor_un = $request->input('nomor_un');
139
            $orang_tua->no_kk = $request->input('no_kk');
140
            $orang_tua->no_telp = $request->input('no_telp');
141
            $orang_tua->nama_ayah = $request->input('nama_ayah');
142
            $orang_tua->nama_ibu = $request->input('nama_ibu');
143
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
144
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
145
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
146
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
147
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
148
            $orang_tua->save();
149
            $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...
150
        }
151
152
        $response['loaded'] = true;
153
154
        return response()->json($response);
155
    }
156
157
    /**
158
     * Store a newly created resource in storage.
159
     *
160
     * @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...
161
     * @return \Illuminate\Http\Response
162
     */
163 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...
164
    {
165
        $orang_tua = $this->orang_tua->findOrFail($id);
166
        
167
        $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...
168
        $response['orang_tua'] = $orang_tua;
169
        $response['loaded'] = true;
170
171
        return response()->json($response);
172
    }
173
174
    /**
175
     * Show the form for editing the specified resource.
176
     *
177
     * @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...
178
     * @return \Illuminate\Http\Response
179
     */
180
    public function edit($id)
181
    {
182
        $orang_tua = $this->orang_tua->findOrFail($id);
183
184
        array_set($orang_tua->user, 'label', $orang_tua->user->name);
185
        
186
        $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...
187
        $response['orang_tua'] = $orang_tua;
188
        $response['loaded'] = true;
189
190
        return response()->json($response);
191
    }
192
193
    /**
194
     * Update the specified resource in storage.
195
     *
196
     * @param  \Illuminate\Http\Request  $request
197
     * @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...
198
     * @return \Illuminate\Http\Response
199
     */
200
    public function update(Request $request, $id)
201
    {   
202
        $response = array();
203
        $message  = array();
204
        $orang_tua = $this->orang_tua->findOrFail($id);
205
206
        $validator = Validator::make($request->all(), [
207
            'user_id' => 'required|unique:orangtuas,user_id,'.$id,
208
            'nomor_un'   => 'required |unique:orangtuas,nomor_un,'.$id,
209
            'no_kk'   => 'required|unique:orangtuas,no_kk,'.$id,
210
            'no_telp'   => 'required|unique:orangtuas,no_telp,'.$id,
211
            'nama_ayah'   => 'required',
212
            'nama_ibu'   => 'required',
213
            'pendidikan_ayah'   => 'required',
214
            'kerja_ayah'   => 'required',
215
            'pendidikan_ibu'   => 'required',
216
            'kerja_ibu'   => 'required',
217
            'alamat_ortu'   => 'required',
218
        ]);
219
220
        if ($validator->fails()) {
221
222
            foreach($validator->messages()->getMessages() as $key => $error){
223
                        foreach($error AS $error_get) {
224
                            array_push($message, $error_get);
225
                        }                
226
                    } 
227
228
             $check_user     = $this->orang_tua->where('id','!=', $id)->where('user_id', $request->user_id);
229
             $check_nomor_un = $this->orang_tua->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
230
             $check_no_kk = $this->orang_tua->where('id','!=', $id)->where('no_kk', $request->no_kk);
231
             $check_no_telp = $this->orang_tua->where('id','!=', $id)->where('no_telp', $request->no_telp);
232
233
             if($check_user->count() > 0 || $check_nomor_un->count() > 0 || $check_no_kk->count() > 0 || $check_no_telp->count() > 0){
234
                  $response['message'] = implode("\n",$message);
235
        } else {
236
            $orang_tua->user_id    = $request->input('user_id');
237
            $orang_tua->nomor_un = $request->input('nomor_un');
238
            $orang_tua->no_kk = $request->input('no_kk');
239
            $orang_tua->no_telp = $request->input('no_telp');
240
            $orang_tua->nama_ayah = $request->input('nama_ayah');
241
            $orang_tua->nama_ibu = $request->input('nama_ibu');
242
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
243
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
244
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
245
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
246
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
247
            $orang_tua->save();
248
249
            $response['message'] = 'success';
250
251
          }
252
        } else {
253
            $orang_tua->user_id    = $request->input('user_id');
254
            $orang_tua->nomor_un = $request->input('nomor_un');
255
            $orang_tua->no_kk = $request->input('no_kk');
256
            $orang_tua->no_telp = $request->input('no_telp');
257
            $orang_tua->nama_ayah = $request->input('nama_ayah');
258
            $orang_tua->nama_ibu = $request->input('nama_ibu');
259
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
260
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
261
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
262
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
263
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
264
            $orang_tua->save();
265
266
            $response['message'] = 'success';
267
        }
268
269
        $response['loaded'] = true;
270
271
        return response()->json($response);
272
    }
273
274
    /**
275
     * Remove the specified resource from storage.
276
     *
277
     * @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...
278
     * @return \Illuminate\Http\Response
279
     */
280 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...
281
    {
282
        $orang_tua = $this->orang_tua->findOrFail($id);
283
284
        if ($orang_tua->delete()) {
285
            $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...
286
        } else {
287
            $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...
288
        }
289
290
        return json_encode($response);
291
    }
292
}
293