Completed
Push — master ( 985212...c1d7cf )
by
unknown
05:48 queued 04:30
created

MasterPrestasiController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
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\MasterPrestasi;
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 MasterPrestasiController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    protected $master_prestasi;
31
    protected $user;
32
33
    public function __construct(MasterPrestasi $master_prestasi, User $user)
34
    {
35
        $this->master_prestasi = $master_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->master_prestasi->orderBy($sortCol, $sortDir);
50
        } else {
51
            $query = $this->master_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('nilai', 'like', $value)
58
                    ->orWhere('bobot', 'like', $value);
59
            });
60
        }
61
62
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
63
        $response = $query->paginate($perPage);
64
65
        foreach($response as $user){
66
            array_set($response->data, 'user', $user->user->name);
67
        }
68
69
        return response()->json($response)
70
            ->header('Access-Control-Allow-Origin', '*')
71
            ->header('Access-Control-Allow-Methods', 'GET');
72
    }
73
74
    /**
75
     * Show the form for creating a new resource.
76
     *
77
     * @return \Illuminate\Http\Response
78
     */
79
80 View Code Duplication
    public function create()
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...
81
    {
82
        $users = $this->user->all();
83
84
        foreach($users as $user){
85
            array_set($user, 'label', $user->name);
86
        }
87
88
        $response['user'] = $users;
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...
89
        $response['status'] = true;
90
91
        return response()->json($response);
92
    }
93
94
    /**
95
     * Display the specified resource.
96
     *
97
     * @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...
98
     * @return \Illuminate\Http\Response
99
     */
100
    public function store(Request $request)
101
    {
102
        $master_prestasi = $this->master_prestasi;
103
104
        $validator = Validator::make($request->all(), [
105
            'user_id' => 'required',
106
            'juara' => 'required|max:255',
107
            'tingkat' => 'required|max:255',
108
            'nilai' => 'required',
109
            'bobot' => 'required',
110
        ]);
111
112 View Code Duplication
        if($validator->fails()){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
113
            $check = $master_prestasi->where('juara',$request->juara)->whereNull('deleted_at')->count();
114
115
            if ($check > 0) {
116
                $response['message'] = 'Failed, juara ' . $request->juara . ' 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...
117
            } else {
118
                $master_prestasi->user_id = $request->input('user_id');
119
                $master_prestasi->juara = $request->input('juara');
120
                $master_prestasi->tingkat = $request->input('tingkat');
121
                $master_prestasi->nilai = $request->input('nilai');
122
                $master_prestasi->bobot = $request->input('bobot');
123
                $master_prestasi->save();
124
125
                $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...
126
            }
127
        } else {
128
                $master_prestasi->user_id = $request->input('user_id');
129
                $master_prestasi->juara = $request->input('juara');
130
                $master_prestasi->tingkat = $request->input('tingkat');
131
                $master_prestasi->nilai = $request->input('nilai');
132
                $master_prestasi->bobot = $request->input('bobot');
133
                $master_prestasi->save();
134
135
            $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...
136
        }
137
138
        $response['status'] = true;
139
140
        return response()->json($response);
141
    }
142
143
    /**
144
     * Store a newly created resource in storage.
145
     *
146
     * @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...
147
     * @return \Illuminate\Http\Response
148
     */
149 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...
150
    {
151
        $master_prestasi = $this->master_prestasi->findOrFail($id);
152
        
153
        $response['user'] = $master_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...
154
        $response['master_prestasi'] = $master_prestasi;
155
        $response['status'] = true;
156
157
        return response()->json($response);
158
    }
159
160
    /**
161
     * Show the form for editing the specified resource.
162
     *
163
     * @param  \App\Prestasi  $prestasi
164
     * @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...
165
     */
166
167 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...
168
    {
169
        $master_prestasi = $this->master_prestasi->findOrFail($id);
170
171
        array_set($master_prestasi->user, 'label', $master_prestasi->user->name);
172
173
        $response['master_prestasi'] = $master_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...
174
        $response['user'] = $master_prestasi->user;
175
        $response['status'] = true;
176
177
        return response()->json($response);
178
    }
179
180
    /**
181
     * Update the specified resource in storage.
182
     *
183
     * @param  \Illuminate\Http\Request  $request
184
     * @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...
185
     * @return \Illuminate\Http\Response
186
     */
187
    public function update(Request $request, $id)
188
    {
189
        $master_prestasi = $this->master_prestasi->findOrFail($id);
190
191
        if ($request->input('juara') == $request->input('juara'))
192
        {
193
            $validator = Validator::make($request->all(), [
194
                'user_id' => 'required',
195
                'juara' => 'required|max:255',
196
                'tingkat' => 'required|max:255',
197
                'nilai' => 'required',
198
                'bobot' => 'required',
199
                
200
            ]);
201
        } else {
202
            $validator = Validator::make($request->all(), [
203
                'user_id' => 'required',
204
                'juara' => 'required|max:255',
205
                'tingkat' => 'required|max:255',
206
                'nilai' => 'required',
207
                'bobot' => 'required',
208
            ]);
209
        }
210
211 View Code Duplication
        if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
212
            $check = $master_prestasi->where('juara',$request->juara)->whereNull('deleted_at')->count();
213
214
            if ($check > 0) {
215
                $response['message'] = 'Failed, juara ' . $request->juara . ' 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...
216
            } else {
217
                $master_prestasi->user_id = $request->input('user_id');
218
                $master_prestasi->juara = $request->input('juara');
219
                $master_prestasi->tingkat = $request->input('tingkat');
220
                $master_prestasi->nilai = $request->input('nilai');
221
                $master_prestasi->bobot = $request->input('bobot');
222
                $master_prestasi->save();
223
224
                $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...
225
            }
226
        } else {
227
                $master_prestasi->user_id = $request->input('user_id');
228
                $master_prestasi->juara = $request->input('juara');
229
                $master_prestasi->tingkat = $request->input('tingkat');
230
                $master_prestasi->nilai = $request->input('nilai');
231
                $master_prestasi->bobot = $request->input('bobot');
232
                $master_prestasi->save();
233
234
            $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
235
        }
236
237
        $response['status'] = true;
238
239
        return response()->json($response);
240
    }
241
242
    /**
243
     * Remove the specified resource from storage.
244
     *
245
     * @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...
246
     * @return \Illuminate\Http\Response
247
     */
248 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...
249
    {
250
        $master_prestasi = $this->master_prestasi->findOrFail($id);
251
252
        if ($master_prestasi->delete()) {
253
            $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...
254
        } else {
255
            $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...
256
        }
257
258
        return json_encode($response);
259
    }
260
}
261