Completed
Push — master ( c4fdcb...4f630f )
by
unknown
8s
created

MasterSktmController::create()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 19

Duplication

Lines 11
Ratio 37.93 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 3
nop 0
dl 11
loc 29
rs 8.8571
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\MasterSktm;
12
use App\User;
13
14
/* Etc */
15
use Validator;
16
17
/**
18
 * The SktmController class.
19
 *
20
 * @package Bantenprov\Sktm
21
 * @author  bantenprov <[email protected]>
22
 */
23
class MasterSktmController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    protected $master_sktm;
31
    protected $user;
32
33
    public function __construct(MasterSktm $master_sktm, User $user)
34
    {
35
        $this->master_sktm = $master_sktm;
36
        $this->user = $user;
37
    }
38
39
    /**
40
     * Display a listing of the resource.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44 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...
45
    {
46
        if (request()->has('sort')) {
47
            list($sortCol, $sortDir) = explode('|', request()->sort);
48
49
            $query = $this->master_sktm->orderBy($sortCol, $sortDir);
50
        } else {
51
            $query = $this->master_sktm->orderBy('id', 'asc');
52
        }
53
54
        if ($request->exists('filter')) {
55
            $query->where(function($q) use($request) {
56
                $value = "%{$request->filter}%";
57
                $q->where('nama', 'like', $value)
58
                    ->orWhere('nilai', 'like', $value);
59
            });
60
        }
61
62
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
63
        $response = $query->with('user')->paginate($perPage);
64
65
        return response()->json($response)
66
            ->header('Access-Control-Allow-Origin', '*')
67
            ->header('Access-Control-Allow-Methods', 'GET');
68
    }
69
70
    /**
71
     * Show the form for creating a new resource.
72
     *
73
     * @return \Illuminate\Http\Response
74
     */
75
76
    public function create()
77
    {
78
        $response = [];
79
80
        $users_special = $this->user->all();
81
        $users_standar = $this->user->find(\Auth::User()->id);
82
        $current_user = \Auth::User();
83
84
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
85
86 View Code Duplication
        if($role_check){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
87
            $response['user_special'] = true;
88
            foreach($users_special as $user){
89
                array_set($user, 'label', $user->name);
90
            }
91
            $response['user'] = $users_special;
92
        }else{
93
            $response['user_special'] = false;
94
            array_set($users_standar, 'label', $users_standar->name);
95
            $response['user'] = $users_standar;
96
        }
97
98
        array_set($current_user, 'label', $current_user->name);
99
100
        $response['current_user'] = $current_user;
101
        $response['status'] = true;
102
103
        return response()->json($response);
104
    }
105
106
    /**
107
     * Display the specified resource.
108
     *
109
     * @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...
110
     * @return \Illuminate\Http\Response
111
     */
112
    public function store(Request $request)
113
    {
114
        $master_sktm = $this->master_sktm;
115
116
        $validator = Validator::make($request->all(), [
117
            /*'user_id' => 'required|unique:master_sktms,user_id',*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
            'user_id' => 'required',
119
            'nama' => 'required',
120
            'nilai' => 'required',
121
            'instansi' => 'required',
122
        ]);
123
124
        /*if($validator->fails()){
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
125
            $check = $master_sktm->where('user_id',$request->user_id)->whereNull('deleted_at')->count();
126
127
            if ($check > 0) {
128
                $response['message'] = 'Failed ! Username, already exists';*/
129
          if($validator->fails()){
130
            $check = $master_sktm->where('label',$request->label)->whereNull('deleted_at')->count();
131
132
            if ($check > 0) {
133
                $response['message'] = 'Failed ! Username, 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...
134
135
            } else {
136
                $master_sktm->user_id = $request->input('user_id');
137
                $master_sktm->nama = $request->input('nama');
138
                $master_sktm->nilai = $request->input('nilai');
139
                $master_sktm->instansi = $request->input('instansi');
140
                $master_sktm->save();
141
142
                $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...
143
            }
144
        } else {
145
                $master_sktm->user_id = $request->input('user_id');
146
                $master_sktm->nama = $request->input('nama');
147
                $master_sktm->nilai = $request->input('nilai');
148
                $master_sktm->instansi = $request->input('instansi');
149
                $master_sktm->save();
150
151
            $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
152
        }
153
154
        $response['status'] = true;
155
156
        return response()->json($response);
157
    }
158
159
    /**
160
     * Store a newly created resource in storage.
161
     *
162
     * @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...
163
     * @return \Illuminate\Http\Response
164
     */
165
    public function show($id)
166
    {
167
        $master_sktm = $this->master_sktm->findOrFail($id);
168
169
        $response['user'] = $master_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...
170
        $response['master_sktm'] = $master_sktm;
171
        $response['status'] = true;
172
173
        return response()->json($response);
174
    }
175
176
    /**
177
     * Show the form for editing the specified resource.
178
     *
179
     * @param  \App\Sktm  $sktm
180
     * @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...
181
     */
182
183
    public function edit($id)
184
    {
185
        $master_sktm = $this->master_sktm->findOrFail($id);
186
187
        array_set($master_sktm->user, 'label', $master_sktm->user->name);
188
189
        $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...
190
        $response['user'] = $master_sktm->user;
191
        $response['status'] = true;
192
193
        return response()->json($response);
194
    }
195
196
    /**
197
     * Update the specified resource in storage.
198
     *
199
     * @param  \Illuminate\Http\Request  $request
200
     * @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...
201
     * @return \Illuminate\Http\Response
202
     */
203
    public function update(Request $request, $id)
204
    {
205
        $response = array();
206
        $message  = array();
207
        $master_sktm = $this->master_sktm->findOrFail($id);
208
209
            $validator = Validator::make($request->all(), [
210
                /*'user_id' => 'required|unique:master_sktms,user_id,'.$id,*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
211
                'user_id' => 'required',
212
                'nama' => 'required',
213
                'nilai' => 'required',
214
                'instansi' => 'required',
215
216
            ]);
217
218
        if ($validator->fails()) {
219
220
            foreach($validator->messages()->getMessages() as $key => $error){
221
                        foreach($error AS $error_get) {
222
                            array_push($message, $error_get);
223
                        }
224
                    }
225
226
             /*$check_user = $this->master_sktm->where('id','!=', $id)->where('user_id', $request->user_id);*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
227
             $check_user = $this->master_sktm->where('id','!=', $id)->where('label', $request->label);
228
229
             if($check_user->count() > 0){
230
                  $response['message'] = implode("\n",$message);
231
232
            } else {
233
                $master_sktm->user_id = $request->input('user_id');
234
                $master_sktm->nama = $request->input('nama');
235
                $master_sktm->nilai = $request->input('nilai');
236
                $master_sktm->instansi = $request->input('instansi');
237
                $master_sktm->save();
238
239
                $response['message'] = 'success';
240
            }
241
        } else {
242
                $master_sktm->user_id = $request->input('user_id');
243
                $master_sktm->nama = $request->input('nama');
244
                $master_sktm->nilai = $request->input('nilai');
245
                $master_sktm->instansi = $request->input('instansi');
246
                $master_sktm->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\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...
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
        $master_sktm = $this->master_sktm->findOrFail($id);
265
266
        if ($master_sktm->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