Completed
Push — master ( 62dae1...2e964c )
by
unknown
03:33
created

DataAkademikController::store()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 49
Code Lines 36

Duplication

Lines 30
Ratio 61.22 %

Importance

Changes 0
Metric Value
cc 3
eloc 36
nc 3
nop 1
dl 30
loc 49
rs 9.2258
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\DataAkademik\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\DataAkademik\Facades\DataAkademikFacade;
9
use App\User;
10
11
/* Models */
12
use Bantenprov\DataAkademik\Models\Bantenprov\DataAkademik\DataAkademik;
13
14
/* Etc */
15
use Validator;
16
17
/**
18
 * The DataAkademikController class.
19
 *
20
 * @package Bantenprov\DataAkademik
21
 * @author  bantenprov <[email protected]>
22
 */
23
class DataAkademikController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    protected $user;
31
    public function __construct(DataAkademik $data_akademik, User $user)
32
    {
33
        $this->data_akademik = $data_akademik;
34
        $this->user             = $user;
35
    }
36
37
    /**
38
     * Display a listing of the resource.
39
     *
40
     * @return \Illuminate\Http\Response
41
     */
42
    public function index(Request $request)
43
    {
44
        if ($request->has('sort')) {
45
            list($sortCol, $sortDir) = explode('|', $request->sort);
46
47
            $query = $this->data_akademik->with('user')->orderBy($sortCol, $sortDir);
48
        } else {
49
            $query = $this->data_akademik->with('user')->orderBy('id', 'asc');
50
        }
51
52
        if ($request->exists('filter')) {
53
            $query->where(function($q) use($request) {
54
                $value = "%{$request->filter}%";
55
                $q->where('nama_siswa', 'like', $value)
56
                    ->orWhere('nomor_un', 'like', $value);
57
            });
58
        }
59
60
61
        $perPage = $request->has('per_page') ? (int) $request->per_page : null;
62
        $response = $query->paginate($perPage);
63
64
        return response()->json($response)
65
            ->header('Access-Control-Allow-Origin', '*')
66
            ->header('Access-Control-Allow-Methods', 'GET');
67
    }
68
69
    /**
70
     * Show the form for creating a new resource.
71
     *
72
     * @return \Illuminate\Http\Response
73
     */
74
    public function create()
75
    {
76
        $response = [];
77
78
        $users_special = $this->user->all();
79
        $users_standar = $this->user->find(\Auth::User()->id);
80
        $current_user = \Auth::User();
81
82
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
83
84
        if($role_check){
85
            $response['user_special'] = true;
86
            foreach($users_special as $user){
87
                array_set($user, 'label', $user->name);
88
            }
89
            $response['user'] = $users_special;
90
        }else{
91
            $response['user_special'] = false;
92
            array_set($users_standar, 'label', $users_standar->name);
93
            $response['user'] = $users_standar;
94
        }
95
96
        array_set($current_user, 'label', $current_user->name);
97
98
        $response['current_user'] = $current_user;
99
        $response['status'] = true;
100
101
        return response()->json($response);
102
    }
103
104
    /**
105
     * Display the specified resource.
106
     *
107
     * @param  \App\DataAkademik  $data_akademik
0 ignored issues
show
Bug introduced by
There is no parameter named $data_akademik. 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...
108
     * @return \Illuminate\Http\Response
109
     */
110
    public function store(Request $request)
111
    {
112
        $data_akademik = $this->data_akademik;
113
114
        $validator = Validator::make($request->all(), [
115
            'nomor_un'          => 'required|unique:data_akademiks,nomor_un',
116
            'nama_siswa'        => 'required',
117
            'bahasa_indonesia'  => 'required|numeric',
118
            'bahasa_inggris'    => 'required|numeric',
119
            'matematika'        => 'required|numeric',
120
            'ipa'               => 'required|numeric',
121
            'user_id'           => 'required',
122
        ]);
123
124 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...
125
            $check = $data_akademik->where('nomor_un',$request->nomor_un)->whereNull('deleted_at')->count();
126
127
            if ($check > 0) {
128
                $response['message'] = 'Failed Nomor UN : ' . $request->nomor_un . ' 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
130
            } else {
131
                $data_akademik->nomor_un          = $request->input('nomor_un');
132
                $data_akademik->nama_siswa        = $request->input('nama_siswa');
133
                $data_akademik->user_id           = $request->input('user_id');
134
                $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
135
                $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
136
                $data_akademik->matematika        = $request->input('matematika');
137
                $data_akademik->ipa               = $request->input('ipa');
138
                $data_akademik->save();
139
140
                $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...
141
            }
142
        } else {
143
            $data_akademik->nomor_un          = $request->input('nomor_un');
144
            $data_akademik->nama_siswa        = $request->input('nama_siswa');
145
            $data_akademik->user_id           = $request->input('user_id');
146
            $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
147
            $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
148
            $data_akademik->matematika        = $request->input('matematika');
149
            $data_akademik->ipa               = $request->input('ipa');
150
            $data_akademik->save();
151
152
            $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...
153
        }
154
155
        $response['status'] = true;
156
157
        return response()->json($response);
158
    }
159
160
    /**
161
     * Store a newly created resource in storage.
162
     *
163
     * @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...
164
     * @return \Illuminate\Http\Response
165
     */
166
    public function show($id)
167
    {
168
        $data_akademik = $this->data_akademik->findOrFail($id);
169
170
        array_set($data_akademik, 'user', $data_akademik->user->name);
171
172
        $response['data_akademik']   = $data_akademik;
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...
173
        $response['status']             = true;
174
175
        return response()->json($response);
176
    }
177
178
    /**
179
     * Show the form for editing the specified resource.
180
     *
181
     * @param  \App\DataAkademik  $data_akademik
0 ignored issues
show
Bug introduced by
There is no parameter named $data_akademik. 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...
182
     * @return \Illuminate\Http\Response
183
     */
184
    public function edit($id)
185
    {
186
        $data_akademik = $this->data_akademik->findOrFail($id);
187
188
        array_set($data_akademik->user, 'label', $data_akademik->user->name);
189
190
        $response['data_akademik']   = $data_akademik;
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...
191
        $response['user']               = $data_akademik->user;
192
        $response['status']             = true;
193
194
        return response()->json($response);
195
    }
196
197
    /**
198
     * Update the specified resource in storage.
199
     *
200
     * @param  \Illuminate\Http\Request  $request
201
     * @param  \App\DataAkademik  $data_akademik
0 ignored issues
show
Bug introduced by
There is no parameter named $data_akademik. 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...
202
     * @return \Illuminate\Http\Response
203
     */
204
    public function update(Request $request, $id)
205
    {
206
        $data_akademik = $this->data_akademik->findOrFail($id);
207
208
        if ($request->input('old_nomor_un') == $request->input('nomor_un'))
209
        {
210
            $validator = Validator::make($request->all(), [
211
                'nomor_un'          => 'required',
212
                'nama_siswa'        => 'required',
213
                'bahasa_indonesia'  => 'required|numeric',
214
                'bahasa_inggris'    => 'required|numeric',
215
                'matematika'        => 'required|numeric',
216
                'ipa'               => 'required|numeric',
217
                'user_id'           => 'required',
218
219
            ]);
220
        } else {
221
            $validator = Validator::make($request->all(), [
222
                'nomor_un'          => 'required|unique:data_akademiks,nomor_un',
223
                'nama_siswa'        => 'required',
224
                'bahasa_indonesia'  => 'required|numeric',
225
                'bahasa_inggris'    => 'required|numeric',
226
                'matematika'        => 'required|numeric',
227
                'ipa'               => 'required|numeric',
228
                'user_id'           => 'required',
229
            ]);
230
        }
231
232 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...
233
            $check = $data_akademik->where('nomor_un',$request->nomor_un)->whereNull('deleted_at')->count();
234
235
            if ($check > 0) {
236
                $response['message'] = 'Failed Nomor UN : ' . $request->nomor_un . ' 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...
237
            } else {
238
                $data_akademik->nomor_un          = $request->input('nomor_un');
239
                $data_akademik->nama_siswa        = $request->input('nama_siswa');
240
                $data_akademik->user_id           = $request->input('user_id');
241
                $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
242
                $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
243
                $data_akademik->matematika        = $request->input('matematika');
244
                $data_akademik->ipa               = $request->input('ipa');
245
                $data_akademik->save();
246
247
                $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...
248
            }
249
        } else {
250
            $data_akademik->nomor_un          = $request->input('nomor_un');
251
            $data_akademik->nama_siswa        = $request->input('nama_siswa');
252
            $data_akademik->user_id           = $request->input('user_id');
253
            $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
254
            $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
255
            $data_akademik->matematika        = $request->input('matematika');
256
            $data_akademik->ipa               = $request->input('ipa');
257
            $data_akademik->save();
258
259
            $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...
260
        }
261
262
        $response['status'] = true;
263
264
        return response()->json($response);
265
    }
266
267
    /**
268
     * Remove the specified resource from storage.
269
     *
270
     * @param  \App\DataAkademik  $data_akademik
0 ignored issues
show
Bug introduced by
There is no parameter named $data_akademik. 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...
271
     * @return \Illuminate\Http\Response
272
     */
273
    public function destroy($id)
274
    {
275
        $data_akademik = $this->data_akademik->findOrFail($id);
276
277
        if ($data_akademik->delete()) {
278
            $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...
279
        } else {
280
            $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...
281
        }
282
283
        return json_encode($response);
284
    }
285
}
286