Completed
Push — master ( a0de1f...cb0838 )
by
unknown
02:35 queued 01:07
created

MasterPrestasiController::juara_label()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
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 Illuminate\Support\Facades\DB;
9
use Bantenprov\Prestasi\Facades\PrestasiFacade;
10
11
/* Models */
12
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\MasterPrestasi;
13
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\JenisPrestasi;
14
use App\User;
15
16
/* Etc */
17
use Validator;
18
use Auth;
19
20
/**
21
 * The PrestasiController class.
22
 *
23
 * @package Bantenprov\Prestasi
24
 * @author  bantenprov <[email protected]>
25
 */
26
class MasterPrestasiController extends Controller
27
{
28
    /**
29
     * Create a new controller instance.
30
     *
31
     * @return void
32
     */
33
    protected $master_prestasi;
34
    protected $jenis_prestasi;
35
    protected $user;
36
37
    public function __construct()
38
    {
39
        $this->master_prestasi  = new MasterPrestasi;
40
        $this->jenis_prestasi   = new JenisPrestasi;
41
        $this->user             = new User;
42
    }
43
44
    /**
45
     * Display a listing of the resource.
46
     *
47
     * @return \Illuminate\Http\Response
48
     */
49 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...
50
    {
51
        if (request()->has('sort')) {
52
            list($sortCol, $sortDir) = explode('|', request()->sort);
53
54
            $query = $this->master_prestasi->orderBy($sortCol, $sortDir);
55
        } else {
56
            $query = $this->master_prestasi->orderBy('id', 'asc');
57
        }
58
59
        if ($request->exists('filter')) {
60
            $query->where(function($q) use($request) {
61
                $value = "%{$request->filter}%";
62
                $q->where('nilai', 'like', $value)
63
                    ->orWhere('id', 'like', $value);
64
            });
65
        }
66
67
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
68
        $response = $query->with('user')->with('jenis_prestasi')->paginate($perPage);
69
70
71
        return response()->json($response)
72
            ->header('Access-Control-Allow-Origin', '*')
73
            ->header('Access-Control-Allow-Methods', 'GET');
74
    }
75
76
    /**
77
     * Show the form for creating a new resource.
78
     *
79
     * @return \Illuminate\Http\Response
80
     */
81
82
    public function create()
83
    {
84
        $response = [];
85
86
        $jenis_prestasis = $this->jenis_prestasi->all();
87
        $users_special = $this->user->all();
88
        $users_standar = $this->user->find(\Auth::User()->id);
89
        $current_user = \Auth::User();
90
91
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
92
93 View Code Duplication
        if($role_check){
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...
94
            $response['user_special'] = true;
95
            foreach($users_special as $user){
96
                array_set($user, 'label', $user->name);
97
            }
98
            $response['user'] = $users_special;
99
        }else{
100
            $response['user_special'] = false;
101
            array_set($users_standar, 'label', $users_standar->name);
102
            $response['user'] = $users_standar;
103
        }
104
105
        array_set($current_user, 'label', $current_user->name);
106
107
        $response['current_user'] = $current_user;
108
109
        foreach($jenis_prestasis as $jenis_prestasi){
110
            array_set($jenis_prestasi, 'label', $jenis_prestasi->nama);
111
        }
112
113
        $response['jenis_prestasi'] = $jenis_prestasis;
114
        $response['status'] = true;
115
116
        return response()->json($response);
117
    }
118
119
    /**
120
     * Display the specified resource.
121
     *
122
     * @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...
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function store(Request $request)
126
    {
127
        $master_prestasi = $this->master_prestasi;
128
129
        $validator = Validator::make($request->all(), [
130
            /*'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...
131
            'user_id' => 'required',
132
            'jenis_prestasi_id' => 'required',
133
            'juara' => 'required',
134
            'kode' => 'required|unique:master_prestasis,kode',
135
            'tingkat' => 'required',
136
            'nilai' => 'required'
137
        ]);
138
139
        if($validator->fails()){
140
            $check = $master_prestasi->where('kode',$request->kode)->whereNull('deleted_at')->count();
141
142 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...
143
                $response['message'] = 'Failed ! Kode Prestasi, 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...
144
            } else {
145
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
146
                $master_prestasi->user_id = $request->input('user_id');
147
                $master_prestasi->juara = $request->input('juara');
148
                $master_prestasi->kode = $request->input('kode');
149
                $master_prestasi->tingkat = $request->input('tingkat');
150
                $master_prestasi->nilai = $request->input('nilai');
151
                $master_prestasi->save();
152
153
                $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
154
            }
155
        } else {
156
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
157
                $master_prestasi->user_id = $request->input('user_id');
158
                $master_prestasi->juara = $request->input('juara');
159
                $master_prestasi->kode = $request->input('kode');
160
                $master_prestasi->tingkat = $request->input('tingkat');
161
                $master_prestasi->nilai = $request->input('nilai');
162
                $master_prestasi->save();
163
164
            $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...
165
        }
166
167
        $response['status'] = true;
168
169
        return response()->json($response);
170
    }
171
172
    /**
173
     * Store a newly created resource in storage.
174
     *
175
     * @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...
176
     * @return \Illuminate\Http\Response
177
     */
178 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...
179
    {
180
        $master_prestasi = $this->master_prestasi->findOrFail($id);
181
182
        $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...
183
        $response['jenis_prestasi'] = $master_prestasi->jenis_prestasi;
184
        $response['master_prestasi'] = $master_prestasi;
185
        $response['status'] = true;
186
187
        return response()->json($response);
188
    }
189
190
    /**
191
     * Show the form for editing the specified resource.
192
     *
193
     * @param  \App\Prestasi  $prestasi
194
     * @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...
195
     */
196
197
    public function edit($id)
198
    {
199
        $master_prestasi = $this->master_prestasi->findOrFail($id);
200
201
        array_set($master_prestasi->user, 'label', $master_prestasi->user->name);
202
        array_set($master_prestasi->jenis_prestasi, 'label', $master_prestasi->jenis_prestasi->nama);
203
204
        $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...
205
        $response['jenis_prestasi'] = $master_prestasi->jenis_prestasi;
206
        $response['user'] = $master_prestasi->user;
207
        $response['status'] = true;
208
209
        return response()->json($response);
210
    }
211
212
    /**
213
     * Update the specified resource in storage.
214
     *
215
     * @param  \Illuminate\Http\Request  $request
216
     * @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...
217
     * @return \Illuminate\Http\Response
218
     */
219
    public function update(Request $request, $id)
220
    {
221
        $response = array();
222
        $message  = array();
223
        $master_prestasi = $this->master_prestasi->findOrFail($id);
224
225
            $validator = Validator::make($request->all(), [
226
                /*'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...
227
                'user_id' => 'required',
228
                'jenis_prestasi_id' => 'required',
229
                'juara' => 'required',
230
                'kode' => 'required|unique:master_prestasis,kode',
231
                'tingkat' => 'required',
232
                'nilai' => 'required'
233
234
            ]);
235
236
        if ($validator->fails()) {
237
238
            foreach($validator->messages()->getMessages() as $key => $error){
239
                        foreach($error AS $error_get) {
240
                            array_push($message, $error_get);
241
                        }
242
                    }
243
244
            $check_kode = $this->master_prestasi->where('id','!=', $id)->where('kode', $request->kode);
245
246 View Code Duplication
             if($check_kode->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...
247
                  $response['message'] = implode("\n",$message);
248
249
            } else {
250
                $master_prestasi->user_id = $request->input('user_id');
251
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
252
                $master_prestasi->juara = $request->input('juara');
253
                $master_prestasi->kode = $request->input('kode');
254
                $master_prestasi->tingkat = $request->input('tingkat');
255
                $master_prestasi->nilai = $request->input('nilai');
256
                $master_prestasi->save();
257
258
                $response['message'] = 'success';
259
            }
260
        } else {
261
                $master_prestasi->user_id = $request->input('user_id');
262
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
263
                $master_prestasi->juara = $request->input('juara');
264
                $master_prestasi->kode = $request->input('kode');
265
                $master_prestasi->tingkat = $request->input('tingkat');
266
                $master_prestasi->nilai = $request->input('nilai');
267
                $master_prestasi->save();
268
269
            $response['message'] = 'success';
270
        }
271
272
        $response['status'] = true;
273
274
        return response()->json($response);
275
    }
276
277
    /**
278
     * Remove the specified resource from storage.
279
     *
280
     * @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...
281
     * @return \Illuminate\Http\Response
282
     */
283 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...
284
    {
285
        $master_prestasi = $this->master_prestasi->findOrFail($id);
286
287
        if ($master_prestasi->delete()) {
288
            $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...
289
        } else {
290
            $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...
291
        }
292
293
        return json_encode($response);
294
    }
295
296 View Code Duplication
    public function juara(){
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...
297
        return array(
298
            array('id' => 1, 'label' => 'Juara 1'),
299
            array('id' => 2, 'label' => 'Juara 2'),
300
            array('id' => 3, 'label' => 'Juara 3'),
301
            array('id' => 4, 'label' => 'Juara Harapan 1'),
302
        );            
303
    }
304
305
    public function juara_label($id){
306
        $juaras  = array(
307
            array('id' => 1, 'label' => 'Juara 1'),
308
            array('id' => 2, 'label' => 'Juara 2'),
309
            array('id' => 3, 'label' => 'Juara 3'),
310
            array('id' => 4, 'label' => 'Juara Harapan 1'),
311
        );
312
        foreach($juaras as $key => $val){
313
            if($val['id'] == $id){
314
                return $val['label'];
315
            }else{
316
                return FALSE;
317
            }    
318
        }
319
    }
320
    
321 View Code Duplication
    public function tingkat(){
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...
322
        return array(
323
            array('id' => 1, 'label' => 'Tingkat Internasional'),
324
            array('id' => 2, 'label' => 'Tingkat Nasional'),
325
            array('id' => 3, 'label' => 'Tingkat Provinsi'),
326
            array('id' => 4, 'label' => 'Tingkat Kabupaten/Kota')
327
        );
328
    }
329
330
    public function tingkat_label($id){
331
        $tingkats   = array(
332
            array('id' => 1, 'label' => 'Tingkat Internasional'),
333
            array('id' => 2, 'label' => 'Tingkat Nasional'),
334
            array('id' => 3, 'label' => 'Tingkat Provinsi'),
335
            array('id' => 4, 'label' => 'Tingkat Kabupaten/Kota')
336
        );
337
338
        foreach($tingkats as $key => $val){
339
            if($val['id'] == $id){
340
                return $val['label'];
341
            }else{
342
                return FALSE;
343
            }    
344
        }
345
    }
346
    
347
}
348