Completed
Push — master ( 9a5d67...747ab4 )
by
unknown
10s
created

MasterPrestasiController::update()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 57
Code Lines 38

Duplication

Lines 14
Ratio 24.56 %

Importance

Changes 0
Metric Value
cc 5
eloc 38
nc 7
nop 2
dl 14
loc 57
rs 8.7433
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\Prestasi\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Prestasi\Facades\PrestasiFacade;
9
10
/* Models */
11
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\MasterPrestasi;
12
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\JenisPrestasi;
13
use App\User;
14
15
/* Etc */
16
use Validator;
17
18
/**
19
 * The PrestasiController class.
20
 *
21
 * @package Bantenprov\Prestasi
22
 * @author  bantenprov <[email protected]>
23
 */
24
class MasterPrestasiController extends Controller
25
{
26
    /**
27
     * Create a new controller instance.
28
     *
29
     * @return void
30
     */
31
    protected $master_prestasi;
32
    protected $jenis_prestasi;
33
    protected $user;
34
35
    public function __construct(MasterPrestasi $master_prestasi, JenisPrestasi $jenis_prestasi, User $user)
36
    {
37
        $this->master_prestasi = $master_prestasi;
38
        $this->jenis_prestasi = $jenis_prestasi;
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->master_prestasi->orderBy($sortCol, $sortDir);
53
        } else {
54
            $query = $this->master_prestasi->orderBy('id', 'asc');
55
        }
56
57
        if ($request->exists('filter')) {
58
            $query->where(function($q) use($request) {
59
                $value = "%{$request->filter}%";
60
                $q->where('nilai', 'like', $value)
61
                    ->orWhere('bobot', 'like', $value);
62
            });
63
        }
64
65
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
66
        $response = $query->with('user')->with('jenis_prestasi')->paginate($perPage);
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
80 View Code Duplication
    public function create()
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...
81
    {
82
        $users = $this->user->all();
83
        $jenis_prestasis = $this->jenis_prestasi->all();
84
85
        foreach($users as $user){
86
            array_set($user, 'label', $user->name);
87
        }
88
89
        foreach($jenis_prestasis as $jenis_prestasi){
90
            array_set($jenis_prestasi, 'label', $jenis_prestasi->nama_jenis_prestasi);
91
        }
92
        
93
        $response['jenis_prestasi'] = $jenis_prestasis;
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...
94
        $response['user'] = $users;
95
        $response['status'] = true;
96
97
        return response()->json($response);
98
    }
99
100
    /**
101
     * Display the specified resource.
102
     *
103
     * @param  \App\Prestasi  $prestasi
0 ignored issues
show
Bug introduced by
There is no parameter named $prestasi. 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...
104
     * @return \Illuminate\Http\Response
105
     */
106
    public function store(Request $request)
107
    {
108
        $master_prestasi = $this->master_prestasi;
109
110
        $validator = Validator::make($request->all(), [
111
            /*'user_id' => 'required|unique:master_prestasis,user_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...
112
            'user_id' => 'required',
113
            'jenis_prestasi_id' => 'required',
114
            'juara' => 'required',
115
            'tingkat' => 'required',
116
            'nilai' => 'required',
117
            'bobot' => 'required',
118
        ]);
119
120
        if($validator->fails()){
121
            $check = $jenis_prestasi->where('label',$request->label)->whereNull('deleted_at')->count();
0 ignored issues
show
Bug introduced by
The variable $jenis_prestasi does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
122
123 View Code Duplication
            if ($check > 0) {
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
                $response['message'] = 'Failed ! Username, 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...
125
            } else {
126
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
127
                $master_prestasi->user_id = $request->input('user_id');
128
                $master_prestasi->juara = $request->input('juara');
129
                $master_prestasi->tingkat = $request->input('tingkat');
130
                $master_prestasi->nilai = $request->input('nilai');
131
                $master_prestasi->bobot = $request->input('bobot');
132
                $master_prestasi->save();
133
134
                $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...
135
            }
136
        } else {
137
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
138
                $master_prestasi->user_id = $request->input('user_id');
139
                $master_prestasi->juara = $request->input('juara');
140
                $master_prestasi->tingkat = $request->input('tingkat');
141
                $master_prestasi->nilai = $request->input('nilai');
142
                $master_prestasi->bobot = $request->input('bobot');
143
                $master_prestasi->save();
144
145
            $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...
146
        }
147
148
        $response['status'] = true;
149
150
        return response()->json($response);
151
    }
152
153
    /**
154
     * Store a newly created resource in storage.
155
     *
156
     * @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...
157
     * @return \Illuminate\Http\Response
158
     */
159 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...
160
    {
161
        $master_prestasi = $this->master_prestasi->findOrFail($id);
162
        
163
        $response['user'] = $master_prestasi->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...
164
        $response['jenis_prestasi'] = $master_prestasi->jenis_prestasi;
165
        $response['master_prestasi'] = $master_prestasi;
166
        $response['status'] = true;
167
168
        return response()->json($response);
169
    }
170
171
    /**
172
     * Show the form for editing the specified resource.
173
     *
174
     * @param  \App\Prestasi  $prestasi
175
     * @return \Illuminate\Http\Response
0 ignored issues
show
Bug introduced by
There is no parameter named $prestasi. 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...
176
     */
177
178 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...
179
    {
180
        $master_prestasi = $this->master_prestasi->findOrFail($id);
181
182
        array_set($master_prestasi->user, 'label', $master_prestasi->user->name);
183
        array_set($master_prestasi->jenis_prestasi, 'label', $master_prestasi->jenis_prestasi->nama_jenis_prestasi);
184
185
        $response['master_prestasi'] = $master_prestasi;
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...
186
        $response['jenis_prestasi'] = $master_prestasi->jenis_prestasi;
187
        $response['user'] = $master_prestasi->user;
188
        $response['status'] = true;
189
190
        return response()->json($response);
191
    }
192
193
    /**
194
     * Update the specified resource in storage.
195
     *
196
     * @param  \Illuminate\Http\Request  $request
197
     * @param  \App\Prestasi  $prestasi
0 ignored issues
show
Bug introduced by
There is no parameter named $prestasi. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
198
     * @return \Illuminate\Http\Response
199
     */
200
    public function update(Request $request, $id)
201
    {   
202
        $response = array();
203
        $message  = array();
204
        $master_prestasi = $this->master_prestasi->findOrFail($id);
205
206
            $validator = Validator::make($request->all(), [
207
                /*'user_id' => 'required|unique:sktms,user_id,'.$id,*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
208
                'user_id' => 'required',
209
                'jenis_prestasi_id' => 'required',
210
                'juara' => 'required',
211
                'tingkat' => 'required',
212
                'nilai' => 'required',
213
                'bobot' => 'required',
214
                
215
            ]);
216
217
        if ($validator->fails()) {
218
219
            foreach($validator->messages()->getMessages() as $key => $error){
220
                        foreach($error AS $error_get) {
221
                            array_push($message, $error_get);
222
                        }                
223
                    } 
224
225
            $check_user = $this->master_prestasi->where('id','!=', $id)->where('label', $request->label);
226
227 View Code Duplication
             if($check_user->count() > 0){
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
                  $response['message'] = implode("\n",$message);
229
230
            } else {
231
                $master_prestasi->user_id = $request->input('user_id');
232
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
233
                $master_prestasi->juara = $request->input('juara');
234
                $master_prestasi->tingkat = $request->input('tingkat');
235
                $master_prestasi->nilai = $request->input('nilai');
236
                $master_prestasi->bobot = $request->input('bobot');
237
                $master_prestasi->save();
238
239
                $response['message'] = 'success';
240
            }
241
        } else {
242
                $master_prestasi->user_id = $request->input('user_id');
243
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
244
                $master_prestasi->juara = $request->input('juara');
245
                $master_prestasi->tingkat = $request->input('tingkat');
246
                $master_prestasi->nilai = $request->input('nilai');
247
                $master_prestasi->bobot = $request->input('bobot');
248
                $master_prestasi->save();
249
250
            $response['message'] = 'success';
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\Prestasi  $prestasi
0 ignored issues
show
Bug introduced by
There is no parameter named $prestasi. 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
        $master_prestasi = $this->master_prestasi->findOrFail($id);
267
268
        if ($master_prestasi->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