Completed
Push — master ( e07f46...7ee80f )
by
unknown
01:29
created

SktmController::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
146
        }
147
148
        $response['status'] = true;
149
150
        return response()->json($response);
151
    }
152
153
    /**
154
     * Store a newly created resource in storage.
155
     *
156
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

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

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

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

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

Loading history...
157
     * @return \Illuminate\Http\Response
158
     */
159 View Code Duplication
    public function show($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
160
    {
161
        $sktm = $this->sktm->findOrFail($id);
162
        
163
        $response['user'] = $sktm->user;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
164
        $response['master_sktm'] = $sktm->master_sktm;
165
        $response['sktm'] = $sktm;
166
        $response['status'] = true;
167
168
        return response()->json($response);
169
    }
170
171
    /**
172
     * Show the form for editing the specified resource.
173
     *
174
     * @param  \App\Sktm  $sktm
175
     * @return \Illuminate\Http\Response
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. Was it maybe removed?

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

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

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

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

Loading history...
176
     */
177
178
    public function edit($id)
179
    {
180
        $sktm = $this->sktm->findOrFail($id);
181
182
        array_set($sktm->user, 'label', $sktm->user->name);
183
        array_set($sktm->master_sktm, 'label', $sktm->master_sktm->juara);
184
        
185
        $response['master_sktm'] = $sktm->master_sktm;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
186
        $response['sktm'] = $sktm;
187
        $response['user'] = $sktm->user;
188
        $response['status'] = true;
189
190
        return response()->json($response);
191
    }
192
193
    /**
194
     * Update the specified resource in storage.
195
     *
196
     * @param  \Illuminate\Http\Request  $request
197
     * @param  \App\Sktm  $sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. Was it maybe removed?

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

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

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

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

Loading history...
198
     * @return \Illuminate\Http\Response
199
     */
200
    public function update(Request $request, $id)
201
    {
202
        $sktm = $this->sktm->findOrFail($id);
203
204
        if ($request->input('master_sktm_id') == $request->input('master_sktm_id'))
205
        {
206
            $validator = Validator::make($request->all(), [
207
                'user_id' => 'required',
208
                'master_sktm_id' => 'required',
209
                'nomor_un' => 'required',
210
                'nama_lomba' => 'required|max:255',
211
                
212
            ]);
213
        } else {
214
            $validator = Validator::make($request->all(), [
215
                'user_id' => 'required',
216
                'master_sktm_id' => 'required',
217
                'nomor_un' => 'required',
218
                'nama_lomba' => 'required|max:255',
219
            ]);
220
        }
221
222 View Code Duplication
        if ($validator->fails()) {
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...
223
            $check = $sktm->where('master_sktm_id',$request->master_sktm_id)->whereNull('deleted_at')->count();
224
225
            if ($check > 0) {
226
                $response['message'] = 'Failed, master_sktm_id ' . $request->master_sktm_id . ' 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...
227
            } else {
228
                $sktm->user_id = $request->input('user_id');
229
                $sktm->master_sktm_id = $request->input('master_sktm_id');
230
                $sktm->nomor_un = $request->input('nomor_un');
231
                $sktm->nama_lomba = $request->input('nama_lomba');
232
                $sktm->save();
233
234
                $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...
235
            }
236
        } else {
237
                $sktm->user_id = $request->input('user_id');
238
                $sktm->master_sktm_id = $request->input('master_sktm_id');
239
                $sktm->nomor_un = $request->input('nomor_un');
240
                $sktm->nama_lomba = $request->input('nama_lomba');
241
                $sktm->save();
242
243
            $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...
244
        }
245
246
        $response['status'] = true;
247
248
        return response()->json($response);
249
    }
250
251
    /**
252
     * Remove the specified resource from storage.
253
     *
254
     * @param  \App\Sktm  $sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $sktm. 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...
255
     * @return \Illuminate\Http\Response
256
     */
257 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...
258
    {
259
        $sktm = $this->sktm->findOrFail($id);
260
261
        if ($sktm->delete()) {
262
            $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...
263
        } else {
264
            $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...
265
        }
266
267
        return json_encode($response);
268
    }
269
}
270