Completed
Push — master ( 286447...e07f46 )
by
unknown
11s
created

SktmController::update()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 61
Code Lines 46

Duplication

Lines 29
Ratio 47.54 %

Importance

Changes 0
Metric Value
cc 4
eloc 46
nc 6
nop 2
dl 29
loc 61
rs 8.9392
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\Sktm\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Sktm\Facades\SktmFacade;
9
10
/* Models */
11
use Bantenprov\Sktm\Models\Bantenprov\Sktm\Sktm;
12
use App\User;
13
14
/* Etc */
15
use Validator;
16
17
/**
18
 * The SktmController class.
19
 *
20
 * @package Bantenprov\Sktm
21
 * @author  bantenprov <[email protected]>
22
 */
23
class SktmController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    protected $user;
31
    protected $sktm;
32
33
    public function __construct(Sktm $sktm, User $user)
34
    {
35
        $this->sktm = $sktm;
36
        $this->user = $user;
37
    }
38
39
    /**
40
     * Display a listing of the resource.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function index(Request $request)
45
    {
46
        if (request()->has('sort')) {
47
            list($sortCol, $sortDir) = explode('|', request()->sort);
48
49
            $query = $this->sktm->orderBy($sortCol, $sortDir);
50
        } else {
51
            $query = $this->sktm->orderBy('id', 'asc');
52
        }
53
54
        if ($request->exists('filter')) {
55
            $query->where(function($q) use($request) {
56
                $value = "%{$request->filter}%";
57
                $q->where('nomor_un', 'like', $value)
58
                    ->orWhere('kode_sktm', 'like', $value);
59
            });
60
        }
61
62
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
63
        $response = $query->paginate($perPage);
64
65
        foreach($response as $user){
66
            array_set($response->data, 'user', $user->user->name);
67
        }
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
        $sktm = $this->sktm;
82
        $users = $this->user->all();
83
84
        foreach($users as $user){
85
            array_set($user, 'label', $user->name);
86
        }
87
88
        $response['sktm'] = $sktm;
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['user'] = $users;
90
        $response['status'] = true;
91
92
        return response()->json($sktm);
93
    }
94
95
    /**
96
     * Display the specified resource.
97
     *
98
     * @param  \App\Sktm  $sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. 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...
99
     * @return \Illuminate\Http\Response
100
     */
101
    public function store(Request $request)
102
    {
103
        $sktm = $this->sktm;
104
105
        $validator = Validator::make($request->all(), [
106
            'user_id' => 'required|unique:sktms,user_id',
107
            'nomor_un' => 'required',
108
            'kode_sktm' => 'required',
109
            'nama_suket' => 'required',
110
            'instansi_suket' => 'required',
111
            'no_suket' => 'required',
112
            'nilai_sktm' => 'required',
113
        ]);
114
115 View Code Duplication
        if($validator->fails()){
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...
116
            $check = $sktm->where('user_id',$request->user_id)->whereNull('deleted_at')->count();
117
118
            if ($check > 0) {
119
                $response['message'] = 'Failed, Username ' . $request->user_id . ' 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...
120
            } else {
121
                $sktm->user_id = $request->input('user_id');
122
                $sktm->nomor_un = $request->input('nomor_un');
123
                $sktm->kode_sktm = $request->input('kode_sktm');
124
                $sktm->nama_suket = $request->input('nama_suket');
125
                $sktm->instansi_suket = $request->input('instansi_suket');
126
                $sktm->no_suket = $request->input('no_suket');
127
                $sktm->nilai_sktm = $request->input('nilai_sktm');
128
                $sktm->save();
129
130
                $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...
131
            }
132
        } else {
133
                $sktm->user_id = $request->input('user_id');
134
                $sktm->nomor_un = $request->input('nomor_un');
135
                $sktm->kode_sktm = $request->input('kode_sktm');
136
                $sktm->nama_suket = $request->input('nama_suket');
137
                $sktm->instansi_suket = $request->input('instansi_suket');
138
                $sktm->no_suket = $request->input('no_suket');
139
                $sktm->nilai_sktm = $request->input('nilai_sktm');
140
            $sktm->save();
141
142
            $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...
143
        }
144
145
        $response['status'] = true;
146
147
        return response()->json($response);
148
    }
149
150
    /**
151
     * Store a newly created resource in storage.
152
     *
153
     * @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...
154
     * @return \Illuminate\Http\Response
155
     */
156
    public function show($id)
157
    {
158
        $sktm = $this->sktm->findOrFail($id);
159
160
        $response['sktm'] = $sktm;
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...
161
        $response['user'] = $sktm->user;
162
        $response['status'] = true;
163
164
        return response()->json($response);
165
    }
166
167
    /**
168
     * Show the form for editing the specified resource.
169
     *
170
     * @param  \App\Sktm  $sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. 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...
171
     * @return \Illuminate\Http\Response
172
     */
173
    public function edit($id)
174
    {
175
        $sktm = $this->sktm->findOrFail($id);
176
177
        array_set($sktm->user, 'label', $sktm->user->name);
178
179
        $response['sktm'] = $sktm;
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...
180
        $response['user'] = $sktm->user;
181
        $response['status'] = true;
182
183
        return response()->json($response);
184
    }
185
186
    /**
187
     * Update the specified resource in storage.
188
     *
189
     * @param  \Illuminate\Http\Request  $request
190
     * @param  \App\Sktm  $sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. 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...
191
     * @return \Illuminate\Http\Response
192
     */
193
    public function update(Request $request, $id)
194
    {
195
        $sktm = $this->sktm->findOrFail($id);
196
197
        if ($request->input('user_id') == $request->input('user_id'))
198
        {
199
            $validator = Validator::make($request->all(), [
200
                'user_id' => 'required|unique:sktms,user_id',
201
                'nomor_un' => 'required',
202
                'kode_sktm' => 'required',
203
                'nama_suket' => 'required',
204
                'instansi_suket' => 'required',
205
                'no_suket' => 'required',
206
                'nilai_sktm' => 'required',
207
            ]);
208
        } else {
209
            $validator = Validator::make($request->all(), [
210
                'user_id' => 'required|unique:sktms,user_id',
211
                'nomor_un' => 'required',
212
                'kode_sktm' => 'required',
213
                'nama_suket' => 'required',
214
                'instansi_suket' => 'required',
215
                'no_suket' => 'required',
216
                'nilai_sktm' => 'required',
217
            ]);
218
        }
219
220 View Code Duplication
        if ($validator->fails()) {
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...
221
            $check = $sktm->where('user_id',$request->user_id)->whereNull('deleted_at')->count();
222
223
            if ($check > 0) {
224
                $response['message'] = 'Failed, Username ' . $request->user_id . ' 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...
225
            } else {
226
                $sktm->user_id = $request->input('user_id');
227
                $sktm->nomor_un = $request->input('nomor_un');
228
                $sktm->kode_sktm = $request->input('kode_sktm');
229
                $sktm->nama_suket = $request->input('nama_suket');
230
                $sktm->instansi_suket = $request->input('instansi_suket');
231
                $sktm->no_suket = $request->input('no_suket');
232
                $sktm->nilai_sktm = $request->input('nilai_sktm');
233
                $sktm->save();
234
235
                $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...
236
            }
237
        } else {
238
                $sktm->user_id = $request->input('user_id');
239
                $sktm->nomor_un = $request->input('nomor_un');
240
                $sktm->kode_sktm = $request->input('kode_sktm');
241
                $sktm->nama_suket = $request->input('nama_suket');
242
                $sktm->instansi_suket = $request->input('instansi_suket');
243
                $sktm->no_suket = $request->input('no_suket');
244
                $sktm->nilai_sktm = $request->input('nilai_sktm');
245
            $sktm->save();
246
247
            $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...
248
        }
249
250
        $response['status'] = true;
251
252
        return response()->json($response);
253
    }
254
255
    /**
256
     * Remove the specified resource from storage.
257
     *
258
     * @param  \App\Sktm  $sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. 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...
259
     * @return \Illuminate\Http\Response
260
     */
261
    public function destroy($id)
262
    {
263
        $sktm = $this->sktm->findOrFail($id);
264
265
        if ($sktm->delete()) {
266
            $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...
267
        } else {
268
            $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...
269
        }
270
271
        return json_encode($response);
272
    }
273
}
274