Completed
Push — master ( 1b709d...2c8a1d )
by
unknown
10s
created

MasterPrestasiController::create()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 36
Code Lines 23

Duplication

Lines 11
Ratio 30.56 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 6
nop 0
dl 11
loc 36
rs 8.5806
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 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('id', '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
    public function create()
81
    {
82
        $response = [];
83
84
        $jenis_prestasis = $this->jenis_prestasi->all();
85
        $users_special = $this->user->all();
86
        $users_standar = $this->user->find(\Auth::User()->id);
87
        $current_user = \Auth::User();
88
89
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
90
91 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...
92
            $response['user_special'] = true;
93
            foreach($users_special as $user){
94
                array_set($user, 'label', $user->name);
95
            }
96
            $response['user'] = $users_special;
97
        }else{
98
            $response['user_special'] = false;
99
            array_set($users_standar, 'label', $users_standar->name);
100
            $response['user'] = $users_standar;
101
        }
102
103
        array_set($current_user, 'label', $current_user->name);
104
105
        $response['current_user'] = $current_user;
106
107
        foreach($jenis_prestasis as $jenis_prestasi){
108
            array_set($jenis_prestasi, 'label', $jenis_prestasi->nama_jenis_prestasi);
109
        }
110
111
        $response['jenis_prestasi'] = $jenis_prestasis;
112
        $response['status'] = true;
113
114
        return response()->json($response);
115
    }
116
117
    /**
118
     * Display the specified resource.
119
     *
120
     * @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...
121
     * @return \Illuminate\Http\Response
122
     */
123
    public function store(Request $request)
124
    {
125
        $master_prestasi = $this->master_prestasi;
126
127
        $validator = Validator::make($request->all(), [
128
            /*'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...
129
            'user_id' => 'required',
130
            'jenis_prestasi_id' => 'required',
131
            'juara' => 'required',
132
            'kode_prestasi' => 'required|unique:master_prestasis,kode_prestasi',
133
            'tingkat' => 'required',
134
            'nilai' => 'required'
135
        ]);
136
137
        if($validator->fails()){
138
            $check = $master_prestasi->where('kode_prestasi',$request->kode_prestasi)->whereNull('deleted_at')->count();
139
140 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...
141
                $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...
142
            } else {
143
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
144
                $master_prestasi->user_id = $request->input('user_id');
145
                $master_prestasi->juara = $request->input('juara');
146
                $master_prestasi->kode_prestasi = $request->input('kode_prestasi');
147
                $master_prestasi->tingkat = $request->input('tingkat');
148
                $master_prestasi->nilai = $request->input('nilai');
149
                $master_prestasi->save();
150
151
                $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...
152
            }
153
        } else {
154
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
155
                $master_prestasi->user_id = $request->input('user_id');
156
                $master_prestasi->juara = $request->input('juara');
157
                $master_prestasi->kode_prestasi = $request->input('kode_prestasi');
158
                $master_prestasi->tingkat = $request->input('tingkat');
159
                $master_prestasi->nilai = $request->input('nilai');
160
                $master_prestasi->save();
161
162
            $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...
163
        }
164
165
        $response['status'] = true;
166
167
        return response()->json($response);
168
    }
169
170
    /**
171
     * Store a newly created resource in storage.
172
     *
173
     * @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...
174
     * @return \Illuminate\Http\Response
175
     */
176 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...
177
    {
178
        $master_prestasi = $this->master_prestasi->findOrFail($id);
179
180
        $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...
181
        $response['jenis_prestasi'] = $master_prestasi->jenis_prestasi;
182
        $response['master_prestasi'] = $master_prestasi;
183
        $response['status'] = true;
184
185
        return response()->json($response);
186
    }
187
188
    /**
189
     * Show the form for editing the specified resource.
190
     *
191
     * @param  \App\Prestasi  $prestasi
192
     * @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...
193
     */
194
195
    public function edit($id)
196
    {
197
        $master_prestasi = $this->master_prestasi->findOrFail($id);
198
199
        array_set($master_prestasi->user, 'label', $master_prestasi->user->name);
200
        array_set($master_prestasi->jenis_prestasi, 'label', $master_prestasi->jenis_prestasi->nama_jenis_prestasi);
201
202
        $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...
203
        $response['jenis_prestasi'] = $master_prestasi->jenis_prestasi;
204
        $response['user'] = $master_prestasi->user;
205
        $response['status'] = true;
206
207
        return response()->json($response);
208
    }
209
210
    /**
211
     * Update the specified resource in storage.
212
     *
213
     * @param  \Illuminate\Http\Request  $request
214
     * @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...
215
     * @return \Illuminate\Http\Response
216
     */
217
    public function update(Request $request, $id)
218
    {
219
        $response = array();
220
        $message  = array();
221
        $master_prestasi = $this->master_prestasi->findOrFail($id);
222
223
            $validator = Validator::make($request->all(), [
224
                /*'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...
225
                'user_id' => 'required',
226
                'jenis_prestasi_id' => 'required',
227
                'juara' => 'required',
228
                'kode_prestasi' => 'required|unique:master_prestasis,kode_prestasi',
229
                'tingkat' => 'required',
230
                'nilai' => 'required'
231
232
            ]);
233
234
        if ($validator->fails()) {
235
236
            foreach($validator->messages()->getMessages() as $key => $error){
237
                        foreach($error AS $error_get) {
238
                            array_push($message, $error_get);
239
                        }
240
                    }
241
242
            $check_kode_prestasi = $this->master_prestasi->where('id','!=', $id)->where('kode_prestasi', $request->kode_prestasi);
243
244 View Code Duplication
             if($check_kode_prestasi->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...
245
                  $response['message'] = implode("\n",$message);
246
247
            } else {
248
                $master_prestasi->user_id = $request->input('user_id');
249
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
250
                $master_prestasi->juara = $request->input('juara');
251
                $master_prestasi->kode_prestasi = $request->input('kode_prestasi');
252
                $master_prestasi->tingkat = $request->input('tingkat');
253
                $master_prestasi->nilai = $request->input('nilai');
254
                $master_prestasi->save();
255
256
                $response['message'] = 'success';
257
            }
258
        } else {
259
                $master_prestasi->user_id = $request->input('user_id');
260
                $master_prestasi->jenis_prestasi_id = $request->input('jenis_prestasi_id');
261
                $master_prestasi->juara = $request->input('juara');
262
                $master_prestasi->kode_prestasi = $request->input('kode_prestasi');
263
                $master_prestasi->tingkat = $request->input('tingkat');
264
                $master_prestasi->nilai = $request->input('nilai');
265
                $master_prestasi->save();
266
267
            $response['message'] = 'success';
268
        }
269
270
        $response['status'] = true;
271
272
        return response()->json($response);
273
    }
274
275
    /**
276
     * Remove the specified resource from storage.
277
     *
278
     * @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...
279
     * @return \Illuminate\Http\Response
280
     */
281 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...
282
    {
283
        $master_prestasi = $this->master_prestasi->findOrFail($id);
284
285
        if ($master_prestasi->delete()) {
286
            $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...
287
        } else {
288
            $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...
289
        }
290
291
        return json_encode($response);
292
    }
293
}
294