Completed
Push — master ( 1b709d...2c8a1d )
by
unknown
10s
created

JenisPrestasiController::store()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 3
nop 1
dl 0
loc 34
rs 8.8571
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\JenisPrestasi;
12
use App\User;
13
14
/* Etc */
15
use Validator;
16
17
/**
18
 * The PrestasiController class.
19
 *
20
 * @package Bantenprov\Prestasi
21
 * @author  bantenprov <[email protected]>
22
 */
23
class JenisPrestasiController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    protected $jenis_prestasi;
31
    protected $user;
32
33
    public function __construct(JenisPrestasi $jenis_prestasi, User $user)
34
    {
35
        $this->jenis_prestasi = $jenis_prestasi;
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->jenis_prestasi->orderBy($sortCol, $sortDir);
50
        } else {
51
            $query = $this->jenis_prestasi->orderBy('id', 'asc');
52
        }
53
54
        if ($request->exists('filter')) {
55
            $query->where(function($q) use($request) {
56
                $value = "%{$request->filter}%";
57
                $q->where('user_id', 'like', $value)
58
                    ->orWhere('nama_jenis_prestasi', '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\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...
110
     * @return \Illuminate\Http\Response
111
     */
112
    public function store(Request $request)
113
    {
114
        $jenis_prestasi = $this->jenis_prestasi;
115
116
        $validator = Validator::make($request->all(), [
117
            /*'user_id' => 'required|unique:jenis_prestasis,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_jenis_prestasi' => 'required',
120
        ]);
121
122
        if($validator->fails()){
123
            $check = $jenis_prestasi->where('label',$request->label)->whereNull('deleted_at')->count();
124
125
            if ($check > 0) {
126
                $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...
127
            } else {
128
                $jenis_prestasi->user_id = $request->input('user_id');
129
                $jenis_prestasi->nama_jenis_prestasi = $request->input('nama_jenis_prestasi');
130
                $jenis_prestasi->save();
131
132
                $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...
133
            }
134
        } else {
135
                $jenis_prestasi->user_id = $request->input('user_id');
136
                $jenis_prestasi->nama_jenis_prestasi = $request->input('nama_jenis_prestasi');
137
                $jenis_prestasi->save();
138
139
            $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...
140
        }
141
142
        $response['status'] = true;
143
144
        return response()->json($response);
145
    }
146
147
    /**
148
     * Store a newly created resource in storage.
149
     *
150
     * @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...
151
     * @return \Illuminate\Http\Response
152
     */
153
    public function show($id)
154
    {
155
        $jenis_prestasi = $this->jenis_prestasi->findOrFail($id);
156
157
        $response['user'] = $jenis_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...
158
        $response['jenis_prestasi'] = $jenis_prestasi;
159
        $response['status'] = true;
160
161
        return response()->json($response);
162
    }
163
164
    /**
165
     * Show the form for editing the specified resource.
166
     *
167
     * @param  \App\Prestasi  $prestasi
168
     * @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...
169
     */
170
171 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...
172
    {
173
        $jenis_prestasi = $this->jenis_prestasi->findOrFail($id);
174
175
        array_set($jenis_prestasi->user, 'label', $jenis_prestasi->user->name);
176
177
        $response['jenis_prestasi'] = $jenis_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...
178
        $response['user'] = $jenis_prestasi->user;
179
        $response['status'] = true;
180
181
        return response()->json($response);
182
    }
183
184
    /**
185
     * Update the specified resource in storage.
186
     *
187
     * @param  \Illuminate\Http\Request  $request
188
     * @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...
189
     * @return \Illuminate\Http\Response
190
     */
191
    public function update(Request $request, $id)
192
    {
193
        $response = array();
194
        $message  = array();
195
        $jenis_prestasi = $this->jenis_prestasi->findOrFail($id);
196
197
            $validator = Validator::make($request->all(), [
198
                /*'user_id' => 'required|unique:jenis_prestasis,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...
199
                'user_id' => 'required',
200
                'nama_jenis_prestasi' => 'required',
201
202
            ]);
203
204
        if ($validator->fails()) {
205
206
            foreach($validator->messages()->getMessages() as $key => $error){
207
                        foreach($error AS $error_get) {
208
                            array_push($message, $error_get);
209
                        }
210
                    }
211
212
             $check_user = $this->jenis_prestasi->where('id','!=', $id)->where('label', $request->label);
213
214
             if($check_user->count() > 0){
215
                  $response['message'] = implode("\n",$message);
216
            } else {
217
                $jenis_prestasi->user_id = $request->input('user_id');
218
                $jenis_prestasi->nama_jenis_prestasi = $request->input('nama_jenis_prestasi');
219
                $jenis_prestasi->save();
220
221
                $response['message'] = 'success';
222
            }
223
        } else {
224
                $jenis_prestasi->user_id = $request->input('user_id');
225
                $jenis_prestasi->nama_jenis_prestasi = $request->input('nama_jenis_prestasi');
226
                $jenis_prestasi->save();
227
228
            $response['message'] = 'success';
229
        }
230
231
        $response['status'] = true;
232
233
        return response()->json($response);
234
    }
235
236
    /**
237
     * Remove the specified resource from storage.
238
     *
239
     * @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...
240
     * @return \Illuminate\Http\Response
241
     */
242 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...
243
    {
244
        $jenis_prestasi = $this->jenis_prestasi->findOrFail($id);
245
246
        if ($jenis_prestasi->delete()) {
247
            $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...
248
        } else {
249
            $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...
250
        }
251
252
        return json_encode($response);
253
    }
254
}
255