Completed
Push — master ( 25e9b2...55947d )
by
unknown
10s
created

SktmController::update()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 60
Code Lines 36

Duplication

Lines 20
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 6
eloc 36
nc 7
nop 2
dl 20
loc 60
rs 8.6961
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 View Code Duplication
    public function index(Request $request)
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...
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->with('user')->with('master_sktm')->paginate($perPage);
67
68
        /*foreach($response as $master_sktm){
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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|unique:sktms,nomor_un',
120
            'master_sktm_id' => 'required|unique:sktms,master_sktm_id',
121
            'no_sktm' => 'required',
122
            'nilai' => 'required',
123
        ]);
124
125
        if($validator->fails()){
126
            $check = $sktm->where('user_id', $request->user_id)->orWhere('nomor_un', $request->nomor_un)->whereNull('deleted_at')->count();
127
128
            if ($check > 0) {
129
                $response['message'] = 'Failed ! Username, Nomor UN, 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 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...
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 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
                $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
        $response = array();
206
        $message  = array();
207
        $sktm = $this->sktm->findOrFail($id);
208
209
        /*if ($request->input('master_sktm_id') == $request->input('master_sktm_id'))
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
210
        {*/
211
            $validator = Validator::make($request->all(), [
212
                'user_id' => 'required|unique:sktms,user_id,'.$id,
213
                'nomor_un' => 'required|unique:sktms,nomor_un,'.$id,
214
                'master_sktm_id' => 'required',
215
                'no_sktm' => 'required',
216
                'nilai' => 'required',
217
                
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->sktm->where('id','!=', $id)->where('user_id', $request->user_id);
229
             $check_nomor_un = $this->sktm->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
230
231
             if($check_user->count() > 0 || $check_nomor_un->count() > 0){
232
                  $response['message'] = implode("\n",$message);
233
234
            /*$check = $sktm->where('user_id',$request->user_id)->whereNull('deleted_at')->count();
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
235
236
            if ($check > 0) {
237
                $response['message'] = 'Failed, Master SKTM ' . $request->user_id . ' already exists';*/
238 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...
239
                $sktm->user_id = $request->input('user_id');
240
                $sktm->nomor_un = $request->input('nomor_un');
241
                $sktm->master_sktm_id = $request->input('master_sktm_id');
242
                $sktm->no_sktm = $request->input('no_sktm');
243
                $sktm->nilai = $request->input('nilai');
244
                $sktm->save();
245
246
                $response['message'] = 'success';
247
            }
248 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...
249
                $sktm->user_id = $request->input('user_id');
250
                $sktm->nomor_un = $request->input('nomor_un');
251
                $sktm->master_sktm_id = $request->input('master_sktm_id');
252
                $sktm->no_sktm = $request->input('no_sktm');
253
                $sktm->nilai = $request->input('nilai');
254
                $sktm->save();
255
256
            $response['message'] = 'success';
257
        }
258
259
        $response['status'] = true;
260
261
        return response()->json($response);
262
    }
263
264
    /**
265
     * Remove the specified resource from storage.
266
     *
267
     * @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...
268
     * @return \Illuminate\Http\Response
269
     */
270 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...
271
    {
272
        $sktm = $this->sktm->findOrFail($id);
273
274
        if ($sktm->delete()) {
275
            $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...
276
        } else {
277
            $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...
278
        }
279
280
        return json_encode($response);
281
    }
282
}
283