Completed
Push — master ( 510875...366501 )
by
unknown
01:55 queued 36s
created

OrangTuaController::store()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 60
Code Lines 48

Duplication

Lines 31
Ratio 51.67 %

Importance

Changes 0
Metric Value
cc 3
eloc 48
nc 3
nop 1
dl 31
loc 60
rs 9.5555
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\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->paginate($perPage);
65
66
        foreach($response as $user){
67
            array_set($response->data, 'user', $user->user->name);
68
        }
69
70
        return response()->json($response)
71
            ->header('Access-Control-Allow-Origin', '*')
72
            ->header('Access-Control-Allow-Methods', 'GET');
73
    }
74
75
    /**
76
     * Show the form for creating a new resource.
77
     *
78
     * @return \Illuminate\Http\Response
79
     */
80
    public function create()
81
    {
82
        $users = $this->user->all();
83
84
        foreach($users as $user){
85
            array_set($user, 'label', $user->name);
86
        }
87
88
        $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...
89
        $response['loaded'] = true;
90
91
        return response()->json($response);
92
    }
93
94
    /**
95
     * Display the specified resource.
96
     *
97
     * @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...
98
     * @return \Illuminate\Http\Response
99
     */
100
    public function store(Request $request)
101
    {
102
        $orang_tua = $this->orang_tua;
103
104
        $validator = Validator::make($request->all(), [
105
            'user_id' => 'required',
106
            'nomor_un'   => 'required|max:255',
107
            'no_kk'   => 'required|max:255',
108
            'no_telp'   => 'required|max:255',
109
            'nama_ayah'   => 'required|max:255',
110
            'nama_ibu'   => 'required|max:255',
111
            'pendidikan_ayah'   => 'required|max:255',
112
            'kerja_ayah'   => 'required|max:255',
113
            'pendidikan_ibu'   => 'required|max:255',
114
            'kerja_ibu'   => 'required|max:255',
115
            'alamat_ortu'   => 'required|max:255',
116
        ]);
117
118
        if($validator->fails()){
119
            $check = $orang_tua->where('label',$request->label)->whereNull('deleted_at')->count();
120
121
            if ($check > 0) {
122
                $response['message'] = 'Failed, label ' . $request->label . ' 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...
123 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...
124
            $orang_tua->user_id = $request->input('user_id');
125
            $orang_tua->nomor_un = $request->input('nomor_un');
126
            $orang_tua->no_kk = $request->input('no_kk');
127
            $orang_tua->no_telp = $request->input('no_telp');
128
            $orang_tua->nama_ayah = $request->input('nama_ayah');
129
            $orang_tua->nama_ibu = $request->input('nama_ibu');
130
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
131
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
132
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
133
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
134
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
135
            $orang_tua->save();
136
137
            $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...
138
            }
139
140 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...
141
            $orang_tua->user_id = $request->input('user_id');
142
            $orang_tua->nomor_un = $request->input('nomor_un');
143
            $orang_tua->no_kk = $request->input('no_kk');
144
            $orang_tua->no_telp = $request->input('no_telp');
145
            $orang_tua->nama_ayah = $request->input('nama_ayah');
146
            $orang_tua->nama_ibu = $request->input('nama_ibu');
147
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
148
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
149
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
150
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
151
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
152
            $orang_tua->save();
153
            $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...
154
        }
155
156
        $response['loaded'] = true;
157
158
        return response()->json($response);
159
    }
160
161
    /**
162
     * Store a newly created resource in storage.
163
     *
164
     * @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...
165
     * @return \Illuminate\Http\Response
166
     */
167 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...
168
    {
169
        $orang_tua = $this->orang_tua->findOrFail($id);
170
        
171
        $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...
172
        $response['orang_tua'] = $orang_tua;
173
        $response['loaded'] = true;
174
175
        return response()->json($response);
176
    }
177
178
    /**
179
     * Show the form for editing the specified resource.
180
     *
181
     * @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...
182
     * @return \Illuminate\Http\Response
183
     */
184 View Code Duplication
    public function edit($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...
185
    {
186
        $orang_tua = $this->orang_tua->findOrFail($id);
187
188
        $response['orang_tua'] = $orang_tua;
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...
189
        $response['loaded'] = true;
190
191
        return response()->json($response);
192
    }
193
194
    /**
195
     * Update the specified resource in storage.
196
     *
197
     * @param  \Illuminate\Http\Request  $request
198
     * @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...
199
     * @return \Illuminate\Http\Response
200
     */
201
    public function update(Request $request, $id)
202
    {
203
        $orang_tua = $this->orang_tua->findOrFail($id);
204
205
        $validator = Validator::make($request->all(), [
206
            'nomor_un'   => 'required|max:255',
207
            'no_kk'   => 'required|max:255',
208
            'no_telp'   => 'required|max:255',
209
            'nama_ayah'   => 'required|max:255',
210
            'nama_ibu'   => 'required|max:255',
211
            'pendidikan_ayah'   => 'required|max:255',
212
            'kerja_ayah'   => 'required|max:255',
213
            'pendidikan_ibu'   => 'required|max:255',
214
            'kerja_ibu'   => 'required|max:255',
215
            'alamat_ortu'   => 'required|max:255',
216
        ]);
217
218
        if($validator->fails()){
219
            $response['error']  = 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...
220
            $response['message'] = $validator->errors()->first();
221
        } else {
222
            $orang_tua->user_id    = $request->user_id;
223
            $orang_tua->nomor_un = $request->nomor_un;
224
            $orang_tua->no_kk = $request->no_kk;
225
            $orang_tua->no_telp = $request->no_telp;
226
            $orang_tua->nama_ayah = $request->nama_ayah;
227
            $orang_tua->nama_ibu = $request->nama_ibu;
228
            $orang_tua->pendidikan_ayah = $request->pendidikan_ayah;
229
            $orang_tua->kerja_ayah = $request->kerja_ayah;
230
            $orang_tua->pendidikan_ibu = $request->pendidikan_ibu;
231
            $orang_tua->kerja_ibu = $request->kerja_ibu;
232
            $orang_tua->alamat_ortu = $request->alamat_ortu;
233
            $orang_tua->save();
234
235
            $response['error'] = 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...
236
            $response['message'] = 'Success';
237
        }
238
239
        $response['loaded'] = true;
240
241
        return response()->json($response);
242
    }
243
244
    /**
245
     * Remove the specified resource from storage.
246
     *
247
     * @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...
248
     * @return \Illuminate\Http\Response
249
     */
250 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...
251
    {
252
        $orang_tua = $this->orang_tua->findOrFail($id);
253
254
        if ($orang_tua->delete()) {
255
            $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...
256
        } else {
257
            $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...
258
        }
259
260
        return json_encode($response);
261
    }
262
}
263