Completed
Push — master ( 2c8a1d...109ddf )
by
unknown
8s
created

PrestasiController::create()   C

Complexity

Conditions 11
Paths 102

Size

Total Lines 45
Code Lines 43

Duplication

Lines 23
Ratio 51.11 %

Importance

Changes 0
Metric Value
cc 11
eloc 43
nc 102
nop 0
dl 23
loc 45
rs 5.2323
c 0
b 0
f 0

How to fix   Complexity   

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\Prestasi;
12
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\MasterPrestasi;
13
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
14
use App\User;
15
16
/* Etc */
17
use Validator;
18
19
/**
20
 * The PrestasiController class.
21
 *
22
 * @package Bantenprov\Prestasi
23
 * @author  bantenprov <[email protected]>
24
 */
25
class PrestasiController extends Controller
26
{
27
    /**
28
     * Create a new controller instance.
29
     *
30
     * @return void
31
     */
32
    protected $prestasi;
33
    protected $master_prestasi;
34
    protected $siswa;
35
    protected $user;
36
37 View Code Duplication
    public function __construct(Prestasi $prestasi, MasterPrestasi $master_prestasi, User $user, Siswa $siswa)
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...
38
    {
39
        $this->prestasi = $prestasi;
40
        $this->master_prestasi = $master_prestasi;
41
        $this->siswa = $siswa;
42
        $this->user = $user;
43
    }
44
45
    /**
46
     * Display a listing of the resource.
47
     *
48
     * @return \Illuminate\Http\Response
49
     */
50 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...
51
    {
52
        if (request()->has('sort')) {
53
            list($sortCol, $sortDir) = explode('|', request()->sort);
54
55
            $query = $this->prestasi->orderBy($sortCol, $sortDir);
56
        } else {
57
            $query = $this->prestasi->orderBy('id', 'asc');
58
        }
59
60
        if ($request->exists('filter')) {
61
            $query->where(function($q) use($request) {
62
                $value = "%{$request->filter}%";
63
                $q->where('nomor_un', 'like', $value)
64
                    ->orWhere('nama_lomba', 'like', $value);
65
            });
66
        }
67
68
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
69
        $response = $query->with('user')->with('master_prestasi')->with('siswa')->paginate($perPage);
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
        $users = $this->user->all();
85
        $master_prestasis = $this->master_prestasi->all();
86
        $siswas = $this->siswa->all();
87
88
        foreach($users as $user){
89
            array_set($user, 'label', $user->name);
90
        }
91
92 View Code Duplication
        foreach($master_prestasis as $master_prestasi){
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...
93
            if($master_prestasi->juara == 1){
94
                $juara = "Juara 1";
95
            }elseif($master_prestasi->juara == 2){
96
                $juara = "Juara 2";
97
            }elseif($master_prestasi->juara == 3){
98
                $juara = "Juara 3";
99
            }else{
100
                $juara = "Juara Harapan 1";
101
            }
102
103
            if($master_prestasi->tingkat == 1){
104
                $tingkat = "Tingkat Internasional";
105
            }elseif($master_prestasi->tingkat == 2){
106
                $tingkat = "Tingkat Nasional";
107
            }elseif($master_prestasi->tingkat == 3){
108
                $tingkat = "Tingkat Provinsi";
109
            }else{
110
                $tingkat = "Tingkat Kabupaten/Kota";
111
            }
112
113
            array_set($master_prestasi, 'label', "( ".$juara." ".$tingkat." ) - ".$master_prestasi->jenis_prestasi->nama_jenis_prestasi);
114
        }
115
116
        foreach($siswas as $siswa){
117
            array_set($siswa, 'label', $siswa->nama_siswa);
118
        }
119
120
        $response['master_prestasi'] = $master_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...
121
        $response['siswa'] = $siswas;
122
        $response['user'] = $users;
123
        $response['status'] = true;
124
125
        return response()->json($response);
126
    }
127
128
    /**
129
     * Display the specified resource.
130
     *
131
     * @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...
132
     * @return \Illuminate\Http\Response
133
     */
134 View Code Duplication
    public function store(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...
135
    {
136
        $prestasi = $this->prestasi;
137
138
        $validator = Validator::make($request->all(), [
139
            'user_id' => 'required|unique:prestasis,user_id',
140
            'master_prestasi_id' => 'required',
141
            'nomor_un' => 'required|unique:prestasis,nomor_un',
142
            'nama_lomba' => 'required',
143
        ]);
144
145
        if($validator->fails()){
146
            $check = $prestasi->where('user_id',$request->user_id)->orWhere('nomor_un',$request->nomor_un)->whereNull('deleted_at')->count();
147
148
            if ($check > 0) {
149
                $response['message'] = 'Failed ! Username, Nama Siswa, 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...
150
            } else {
151
                $prestasi->user_id = $request->input('user_id');
152
                $prestasi->master_prestasi_id = $request->input('master_prestasi_id');
153
                $prestasi->nomor_un = $request->input('nomor_un');
154
                $prestasi->nama_lomba = $request->input('nama_lomba');
155
                $prestasi->save();
156
157
                $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...
158
            }
159
        } else {
160
                $prestasi->user_id = $request->input('user_id');
161
                $prestasi->master_prestasi_id = $request->input('master_prestasi_id');
162
                $prestasi->nomor_un = $request->input('nomor_un');
163
                $prestasi->nama_lomba = $request->input('nama_lomba');
164
                $prestasi->save();
165
166
            $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...
167
        }
168
169
        $response['status'] = true;
170
171
        return response()->json($response);
172
    }
173
174
    /**
175
     * Store a newly created resource in storage.
176
     *
177
     * @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...
178
     * @return \Illuminate\Http\Response
179
     */
180 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...
181
    {
182
        $prestasi = $this->prestasi->findOrFail($id);
183
184
        $response['user'] = $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...
185
        $response['master_prestasi'] = $prestasi->master_prestasi;
186
        $response['siswa'] = $prestasi->siswa;
187
        $response['prestasi'] = $prestasi;
188
        $response['status'] = true;
189
190
        return response()->json($response);
191
    }
192
193
    /**
194
     * Show the form for editing the specified resource.
195
     *
196
     * @param  \App\Prestasi  $prestasi
197
     * @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...
198
     */
199
200 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...
201
    {
202
        $prestasi = $this->prestasi->findOrFail($id);
203
204
        array_set($prestasi->user, 'label', $prestasi->user->name);
205
        array_set($prestasi->master_prestasi, 'label', $prestasi->master_prestasi->juara);
206
        array_set($prestasi->siswa, 'label', $prestasi->siswa->nama_siswa);
207
208
        $response['master_prestasi'] = $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...
209
        $response['siswa'] = $prestasi->siswa;
210
        $response['prestasi'] = $prestasi;
211
        $response['user'] = $prestasi->user;
212
        $response['status'] = true;
213
214
        return response()->json($response);
215
    }
216
217
    /**
218
     * Update the specified resource in storage.
219
     *
220
     * @param  \Illuminate\Http\Request  $request
221
     * @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...
222
     * @return \Illuminate\Http\Response
223
     */
224 View Code Duplication
    public function update(Request $request, $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...
225
    {
226
        $response = array();
227
        $message  = array();
228
229
        $prestasi = $this->prestasi->findOrFail($id);
230
231
            $validator = Validator::make($request->all(), [
232
                'user_id' => 'required|unique:prestasis,user_id,'.$id,
233
                'master_prestasi_id' => 'required',
234
                'nomor_un' => 'required|unique:prestasis,nomor_un,'.$id,
235
                'nama_lomba' => 'required',
236
237
            ]);
238
239
        if ($validator->fails()) {
240
241
            foreach($validator->messages()->getMessages() as $key => $error){
242
                        foreach($error AS $error_get) {
243
                            array_push($message, $error_get);
244
                        }
245
                    }
246
247
             $check_user = $this->prestasi->where('id','!=', $id)->where('user_id', $request->user_id);
248
             $check_siswa = $this->prestasi->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
249
250
             if($check_user->count() > 0 || $check_siswa->count() > 0){
251
                  $response['message'] = implode("\n",$message);
252
            } else {
253
                $prestasi->user_id = $request->input('user_id');
254
                $prestasi->master_prestasi_id = $request->input('master_prestasi_id');
255
                $prestasi->nomor_un = $request->input('nomor_un');
256
                $prestasi->nama_lomba = $request->input('nama_lomba');
257
                $prestasi->save();
258
259
                $response['message'] = 'success';
260
            }
261
        } else {
262
                $prestasi->user_id = $request->input('user_id');
263
                $prestasi->master_prestasi_id = $request->input('master_prestasi_id');
264
                $prestasi->nomor_un = $request->input('nomor_un');
265
                $prestasi->nama_lomba = $request->input('nama_lomba');
266
                $prestasi->save();
267
268
            $response['message'] = 'success';
269
        }
270
271
        $response['status'] = true;
272
273
        return response()->json($response);
274
    }
275
276
    /**
277
     * Remove the specified resource from storage.
278
     *
279
     * @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...
280
     * @return \Illuminate\Http\Response
281
     */
282 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...
283
    {
284
        $prestasi = $this->prestasi->findOrFail($id);
285
286
        if ($prestasi->delete()) {
287
            $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...
288
        } else {
289
            $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...
290
        }
291
292
        return json_encode($response);
293
    }
294
}
295