Completed
Push — master ( 747ab4...57f583 )
by
unknown
06:12 queued 04:26
created

PrestasiController::create()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 8
nop 0
dl 0
loc 25
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\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
    public function __construct(Prestasi $prestasi, MasterPrestasi $master_prestasi, User $user, Siswa $siswa)
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('siswa_id', '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
        foreach($master_prestasis as $master_prestasi){
93
            array_set($master_prestasi, 'label', $master_prestasi->juara);
94
        }
95
96
        foreach($siswas as $siswa){
97
            array_set($siswa, 'label', $siswa->nama_siswa);
98
        }
99
        
100
        $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...
101
        $response['siswa'] = $siswas;
102
        $response['user'] = $users;
103
        $response['status'] = true;
104
105
        return response()->json($response);
106
    }
107
108
    /**
109
     * Display the specified resource.
110
     *
111
     * @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...
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function store(Request $request)
115
    {
116
        $prestasi = $this->prestasi;
117
118
        $validator = Validator::make($request->all(), [
119
            'user_id' => 'required|unique:prestasis,user_id',
120
            'master_prestasi_id' => 'required',
121
            'siswa_id' => 'required|unique:prestasis,siswa_id',
122
            'nama_lomba' => 'required',
123
        ]);
124
125
        if($validator->fails()){
126
            $check = $prestasi->where('user_id',$request->user_id)->orWhere('siswa_id',$request->siswa_id)->whereNull('deleted_at')->count();
127
128
            if ($check > 0) {
129
                $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...
130 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
                $prestasi->user_id = $request->input('user_id');
132
                $prestasi->master_prestasi_id = $request->input('master_prestasi_id');
133
                $prestasi->siswa_id = $request->input('siswa_id');
134
                $prestasi->nama_lomba = $request->input('nama_lomba');
135
                $prestasi->save();
136
137
                $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...
138
            }
139 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
                $prestasi->user_id = $request->input('user_id');
141
                $prestasi->master_prestasi_id = $request->input('master_prestasi_id');
142
                $prestasi->siswa_id = $request->input('siswa_id');
143
                $prestasi->nama_lomba = $request->input('nama_lomba');
144
                $prestasi->save();
145
146
            $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...
147
        }
148
149
        $response['status'] = true;
150
151
        return response()->json($response);
152
    }
153
154
    /**
155
     * Store a newly created resource in storage.
156
     *
157
     * @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...
158
     * @return \Illuminate\Http\Response
159
     */
160 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...
161
    {
162
        $prestasi = $this->prestasi->findOrFail($id);
163
        
164
        $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...
165
        $response['master_prestasi'] = $prestasi->master_prestasi;
166
        $response['siswa'] = $prestasi->siswa;
167
        $response['prestasi'] = $prestasi;
168
        $response['status'] = true;
169
170
        return response()->json($response);
171
    }
172
173
    /**
174
     * Show the form for editing the specified resource.
175
     *
176
     * @param  \App\Prestasi  $prestasi
177
     * @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...
178
     */
179
180
    public function edit($id)
181
    {
182
        $prestasi = $this->prestasi->findOrFail($id);
183
184
        array_set($prestasi->user, 'label', $prestasi->user->name);
185
        array_set($prestasi->master_prestasi, 'label', $prestasi->master_prestasi->juara);
186
        array_set($prestasi->siswa, 'label', $prestasi->siswa->nama_siswa);
187
        
188
        $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...
189
        $response['siswa'] = $prestasi->siswa;
190
        $response['prestasi'] = $prestasi;
191
        $response['user'] = $prestasi->user;
192
        $response['status'] = true;
193
194
        return response()->json($response);
195
    }
196
197
    /**
198
     * Update the specified resource in storage.
199
     *
200
     * @param  \Illuminate\Http\Request  $request
201
     * @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...
202
     * @return \Illuminate\Http\Response
203
     */
204
    public function update(Request $request, $id)
205
    {   
206
        $response = array();
207
        $message  = array();
208
209
        $prestasi = $this->prestasi->findOrFail($id);
210
211
            $validator = Validator::make($request->all(), [
212
                'user_id' => 'required|unique:prestasis,user_id,'.$id,
213
                'master_prestasi_id' => 'required',
214
                'siswa_id' => 'required|unique:prestasis,siswa_id,'.$id,
215
                'nama_lomba' => 'required',
216
                
217
            ]);
218
219
        if ($validator->fails()) {
220
221
            foreach($validator->messages()->getMessages() as $key => $error){
222
                        foreach($error AS $error_get) {
223
                            array_push($message, $error_get);
224
                        }                
225
                    } 
226
227
             $check_user = $this->prestasi->where('id','!=', $id)->where('user_id', $request->user_id);
228
             $check_siswa = $this->prestasi->where('id','!=', $id)->where('siswa_id', $request->siswa_id);
229
230
             if($check_user->count() > 0 || $check_siswa->count() > 0){
231
                  $response['message'] = implode("\n",$message);
232 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
                $prestasi->user_id = $request->input('user_id');
234
                $prestasi->master_prestasi_id = $request->input('master_prestasi_id');
235
                $prestasi->siswa_id = $request->input('siswa_id');
236
                $prestasi->nama_lomba = $request->input('nama_lomba');
237
                $prestasi->save();
238
239
                $response['message'] = 'success';
240
            }
241 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
                $prestasi->user_id = $request->input('user_id');
243
                $prestasi->master_prestasi_id = $request->input('master_prestasi_id');
244
                $prestasi->siswa_id = $request->input('siswa_id');
245
                $prestasi->nama_lomba = $request->input('nama_lomba');
246
                $prestasi->save();
247
248
            $response['message'] = 'success';
249
        }
250
251
        $response['status'] = true;
252
253
        return response()->json($response);
254
    }
255
256
    /**
257
     * Remove the specified resource from storage.
258
     *
259
     * @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...
260
     * @return \Illuminate\Http\Response
261
     */
262 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...
263
    {
264
        $prestasi = $this->prestasi->findOrFail($id);
265
266
        if ($prestasi->delete()) {
267
            $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...
268
        } else {
269
            $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...
270
        }
271
272
        return json_encode($response);
273
    }
274
}
275