JenisPrestasiController::index()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 16

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 8
nop 1
dl 25
loc 25
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Prestasi\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\DB;
9
use Bantenprov\Prestasi\Facades\PrestasiFacade;
10
11
/* Models */
12
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\JenisPrestasi;
13
use App\User;
14
15
/* Etc */
16
use Validator;
17
use Auth;
18
19
/**
20
 * The PrestasiController class.
21
 *
22
 * @package Bantenprov\Prestasi
23
 * @author  bantenprov <[email protected]>
24
 */
25
class JenisPrestasiController extends Controller
26
{
27
    /**
28
     * Create a new controller instance.
29
     *
30
     * @return void
31
     */
32
    protected $jenis_prestasi;
33
    protected $user;
34
35
    public function __construct(JenisPrestasi $jenis_prestasi, User $user)
36
    {
37
        $this->jenis_prestasi = $jenis_prestasi;
38
        $this->user = $user;
39
    }
40
41
    /**
42
     * Display a listing of the resource.
43
     *
44
     * @return \Illuminate\Http\Response
45
     */
46 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...
47
    {
48
        if (request()->has('sort')) {
49
            list($sortCol, $sortDir) = explode('|', request()->sort);
50
51
            $query = $this->jenis_prestasi->orderBy($sortCol, $sortDir);
52
        } else {
53
            $query = $this->jenis_prestasi->orderBy('id', 'asc');
54
        }
55
56
        if ($request->exists('filter')) {
57
            $query->where(function($q) use($request) {
58
                $value = "%{$request->filter}%";
59
                $q->where('user_id', 'like', $value)
60
                    ->orWhere('nama', 'like', $value);
61
            });
62
        }
63
64
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
65
        $response = $query->with('user')->paginate($perPage);
66
67
        return response()->json($response)
68
            ->header('Access-Control-Allow-Origin', '*')
69
            ->header('Access-Control-Allow-Methods', 'GET');
70
    }
71
72
    /**
73
     * Show the form for creating a new resource.
74
     *
75
     * @return \Illuminate\Http\Response
76
     */
77
78
    public function create()
79
    {
80
        $response = [];
81
82
        $users_special = $this->user->all();
83
        $users_standar = $this->user->find(\Auth::User()->id);
84
        $current_user = \Auth::User();
85
86
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
87
88 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...
89
            $response['user_special'] = true;
90
            foreach($users_special as $user){
91
                array_set($user, 'label', $user->name);
92
            }
93
            $response['user'] = $users_special;
94
        }else{
95
            $response['user_special'] = false;
96
            array_set($users_standar, 'label', $users_standar->name);
97
            $response['user'] = $users_standar;
98
        }
99
100
        array_set($current_user, 'label', $current_user->name);
101
102
        $response['current_user'] = $current_user;
103
        $response['status'] = true;
104
105
        return response()->json($response);
106
    }
107
108
    /**
109
     * Display the specified resource.
110
     *
111
     * @param  \App\Prestasi  $prestasi
0 ignored issues
show
Bug introduced by
There is no parameter named $prestasi. Was it maybe removed?

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

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

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

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

Loading history...
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function store(Request $request)
115
    {
116
        $jenis_prestasi = $this->jenis_prestasi;
117
118
        $validator = Validator::make($request->all(), [
119
            /*'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...
120
            'user_id' => 'required',
121
            'nama' => 'required',
122
        ]);
123
124
        if($validator->fails()){
125
            $check = $jenis_prestasi->where('label',$request->label)->whereNull('deleted_at')->count();
126
127
            if ($check > 0) {
128
                $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...
129 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

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

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

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

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

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

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

Loading history...
219
                $jenis_prestasi->user_id = $request->input('user_id');
220
                $jenis_prestasi->nama = $request->input('nama');
221
                $jenis_prestasi->save();
222
223
                $response['message'] = 'success';
224
            }
225 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
226
                $jenis_prestasi->user_id = $request->input('user_id');
227
                $jenis_prestasi->nama = $request->input('nama');
228
                $jenis_prestasi->save();
229
230
            $response['message'] = 'success';
231
        }
232
233
        $response['status'] = true;
234
235
        return response()->json($response);
236
    }
237
238
    /**
239
     * Remove the specified resource from storage.
240
     *
241
     * @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...
242
     * @return \Illuminate\Http\Response
243
     */
244 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...
245
    {
246
        $jenis_prestasi = $this->jenis_prestasi->findOrFail($id);
247
248
        if ($jenis_prestasi->delete()) {
249
            $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...
250
        } else {
251
            $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...
252
        }
253
254
        return json_encode($response);
255
    }
256
}
257