Completed
Push — master ( 9126b1...25e9b2 )
by
unknown
12s
created

SktmController::update()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 54
Code Lines 38

Duplication

Lines 25
Ratio 46.3 %

Importance

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