MasterSktmController::destroy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 16
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 Illuminate\Support\Facades\DB;
9
use Bantenprov\Sktm\Facades\SktmFacade;
10
11
/* Models */
12
use Bantenprov\Sktm\Models\Bantenprov\Sktm\MasterSktm;
13
use App\User;
14
15
/* Etc */
16
use Validator;
17
use Auth;
18
19
/**
20
 * The MasterSktmController class.
21
 *
22
 * @package Bantenprov\Sktm
23
 * @author  bantenprov <[email protected]>
24
 */
25
class MasterSktmController extends Controller
26
{
27
    protected $master_sktm;
28
    protected $user;
29
30
    /**
31
     * Create a new controller instance.
32
     *
33
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
34
     */
35
    public function __construct()
36
    {
37
        $this->master_sktm  = new MasterSktm;
38
        $this->user         = new User;
39
    }
40
41
    /**
42
     * Display a listing of the resource.
43
     *
44
     * @return \Illuminate\Http\Response
45
     */
46
    public function index(Request $request)
47
    {
48
        if (request()->has('sort')) {
49
            list($sortCol, $sortDir) = explode('|', request()->sort);
50
51
            $query = $this->master_sktm->orderBy($sortCol, $sortDir);
52
        } else {
53
            $query = $this->master_sktm->orderBy('id', 'asc');
54
        }
55
56
        if ($request->exists('filter')) {
57
            $query->where(function($q) use($request) {
58
                $value = "%{$request->filter}%";
59
60
                $q->where('nama', 'like', $value)
61
                    ->orWhere('instansi', 'like', $value);
62
            });
63
        }
64
65
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
66
67
        $response   = $query->with(['user'])->paginate($perPage);
68
69
        return response()->json($response)
70
            ->header('Access-Control-Allow-Origin', '*')
71
            ->header('Access-Control-Allow-Methods', 'GET');
72
    }
73
74
    /**
75
     * Display a listing of the resource.
76
     *
77
     * @return \Illuminate\Http\Response
78
     */
79 View Code Duplication
    public function get()
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...
80
    {
81
        $master_sktms = $this->master_sktm->with(['user'])->get();
82
83
        $response['master_sktms']   = $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...
84
        $response['error']          = false;
85
        $response['message']        = 'Success';
86
        $response['status']         = true;
87
88
        return response()->json($response);
89
    }
90
91
    /**
92
     * Show the form for creating a new resource.
93
     *
94
     * @return \Illuminate\Http\Response
95
     */
96
    public function create()
97
    {
98
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
99
        $master_sktm    = $this->master_sktm->getAttributes();
100
        $users          = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
        $users_special  = $this->user->all();
102
        $users_standar  = $this->user->findOrFail($user_id);
103
        $current_user   = Auth::User();
104
105
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
106
107
        if ($role_check) {
108
            $user_special = true;
109
110
            foreach ($users_special as $user) {
111
                array_set($user, 'label', $user->name);
112
            }
113
114
            $users = $users_special;
115
        } else {
116
            $user_special = false;
117
118
            array_set($users_standar, 'label', $users_standar->name);
119
120
            $users = $users_standar;
121
        }
122
123
        array_set($current_user, 'label', $current_user->name);
124
125
        $response['master_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...
126
        $response['users']          = $users;
127
        $response['user_special']   = $user_special;
128
        $response['current_user']   = $current_user;
129
        $response['error']          = false;
130
        $response['message']        = 'Success';
131
        $response['status']         = true;
132
133
        return response()->json($response);
134
    }
135
136
    /**
137
     * Store a newly created resource in storage.
138
     *
139
     * @param  \Illuminate\Http\Request  $request
140
     * @return \Illuminate\Http\Response
141
     */
142 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...
143
    {
144
        $master_sktm = $this->master_sktm;
145
146
        $validator = Validator::make($request->all(), [
147
            'nama'      => "required|max:255|unique:{$this->master_sktm->getTable()},nama,NULL,id,instansi,{$request->input('instansi')},deleted_at,NULL",
148
            'instansi'  => 'required|max:255',
149
            'nilai'     => 'required|numeric|max:100',
150
            'user_id'   => "required|exists:{$this->user->getTable()},id",
151
        ]);
152
153
        if ($validator->fails()) {
154
            $error      = true;
155
            $message    = $validator->errors()->first();
156
        } else {
157
            $master_sktm->nama      = $request->input('nama');
158
            $master_sktm->instansi  = $request->input('instansi');
159
            $master_sktm->nilai     = $request->input('nilai');
160
            $master_sktm->user_id   = $request->input('user_id');
161
            $master_sktm->save();
162
163
            $error      = false;
164
            $message    = 'Success';
165
        }
166
167
        $response['master_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...
168
        $response['error']          = $error;
169
        $response['message']        = $message;
170
        $response['status']         = true;
171
172
        return response()->json($response);
173
    }
174
175
    /**
176
     * Display the specified resource.
177
     *
178
     * @param  \App\MasterSktm  $master_sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $master_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...
179
     * @return \Illuminate\Http\Response
180
     */
181 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...
182
    {
183
        $master_sktm = $this->master_sktm->with(['user'])->findOrFail($id);
184
185
        $response['master_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['error']          = false;
187
        $response['message']        = 'Success';
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\MasterSktm  $master_sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $master_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...
197
     * @return \Illuminate\Http\Response
198
     */
199 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...
200
    {
201
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
202
        $master_sktm    = $this->master_sktm->with(['user'])->findOrFail($id);
203
        $users          = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
204
        $users_special  = $this->user->all();
205
        $users_standar  = $this->user->findOrFail($user_id);
206
        $current_user   = Auth::User();
207
208
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
209
210
        if ($master_sktm->user !== null) {
211
            array_set($master_sktm->user, 'label', $master_sktm->user->name);
212
        }
213
214
        if ($role_check) {
215
            $user_special = true;
216
217
            foreach ($users_special as $user) {
218
                array_set($user, 'label', $user->name);
219
            }
220
221
            $users = $users_special;
222
        } else {
223
            $user_special = false;
224
225
            array_set($users_standar, 'label', $users_standar->name);
226
227
            $users = $users_standar;
228
        }
229
230
        array_set($current_user, 'label', $current_user->name);
231
232
        $response['master_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...
233
        $response['users']          = $users;
234
        $response['user_special']   = $user_special;
235
        $response['current_user']   = $current_user;
236
        $response['error']          = false;
237
        $response['message']        = 'Success';
238
        $response['status']         = true;
239
240
        return response()->json($response);
241
    }
242
243
    /**
244
     * Update the specified resource in storage.
245
     *
246
     * @param  \Illuminate\Http\Request  $request
247
     * @param  \App\MasterSktm  $master_sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $master_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...
248
     * @return \Illuminate\Http\Response
249
     */
250 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...
251
    {
252
        $master_sktm = $this->master_sktm->with(['user'])->findOrFail($id);
253
254
        $validator = Validator::make($request->all(), [
255
            'nama'      => "required|max:255|unique:{$this->master_sktm->getTable()},nama,{$id},id,instansi,{$request->input('instansi')},deleted_at,NULL",
256
            'instansi'  => 'required|max:255',
257
            'nilai'     => 'required|numeric|max:100',
258
            'user_id'   => "required|exists:{$this->user->getTable()},id",
259
        ]);
260
261
        if ($validator->fails()) {
262
            $error      = true;
263
            $message    = $validator->errors()->first();
264
        } else {
265
            $master_sktm->nama      = $request->input('nama');
266
            $master_sktm->instansi  = $request->input('instansi');
267
            $master_sktm->nilai     = $request->input('nilai');
268
            $master_sktm->user_id   = $request->input('user_id');
269
            $master_sktm->save();
270
271
            $error      = false;
272
            $message    = 'Success';
273
        }
274
275
        $response['error']      = $error;
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...
276
        $response['message']    = $message;
277
        $response['status']     = true;
278
279
        return response()->json($response);
280
    }
281
282
    /**
283
     * Remove the specified resource from storage.
284
     *
285
     * @param  \App\MasterSktm  $master_sktm
0 ignored issues
show
Bug introduced by
There is no parameter named $master_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...
286
     * @return \Illuminate\Http\Response
287
     */
288
    public function destroy($id)
289
    {
290
        $master_sktm = $this->master_sktm->findOrFail($id);
291
292
        if ($master_sktm->delete()) {
293
            $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...
294
            $response['success']    = true;
295
            $response['status']     = true;
296
        } else {
297
            $response['message']    = 'Failed';
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...
298
            $response['success']    = false;
299
            $response['status']     = false;
300
        }
301
302
        return json_encode($response);
303
    }
304
}
305