Completed
Push — master ( 9b5b60...216c80 )
by
unknown
02:45 queued 52s
created

DataAkademikController::store()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 30

Duplication

Lines 40
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 30
nc 2
nop 1
dl 40
loc 40
rs 8.8571
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;
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...
100
        $response['error']          = false;
101
        $response['message']        = 'Success';
102
        $response['status']         = true;
103
104
        return response()->json($response);
105
    }
106
107
    /**
108
     * Display the specified resource.
109
     *
110
     * @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...
111
     * @return \Illuminate\Http\Response
112
     */
113 View Code Duplication
    public function store(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...
114
    {
115
        $data_akademik = $this->data_akademik;
116
117
        $validator = Validator::make($request->all(), [
118
            'nomor_un'      => "required|max:255|unique:{$this->data_akademik->getTable()},nomor_un,NULL,id,deleted_at,NULL",
119
            'nama_siswa'        => 'required',
120
            'nomor_kk'          => 'required',
121
            'bahasa_indonesia'  => 'required|numeric',
122
            'bahasa_inggris'    => 'required|numeric',
123
            'matematika'        => 'required|numeric',
124
            'ipa'               => 'required|numeric',
125
            'user_id'           => 'required',
126
        ]);
127
128
        if ($validator->fails()) {
129
            $error      = true;
130
            $message    = $validator->errors()->first();
131
132
            } else {
133
                $data_akademik->nomor_un          = $request->input('nomor_un');
134
                $data_akademik->nama_siswa        = $request->input('nama_siswa');
135
                $data_akademik->nomor_kk          = $request->input('nomor_kk');
136
                $data_akademik->user_id           = $request->input('user_id');
137
                $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
138
                $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
139
                $data_akademik->matematika        = $request->input('matematika');
140
                $data_akademik->ipa               = $request->input('ipa');
141
                $data_akademik->save();
142
143
                $error      = false;
144
                $message    = 'Success';
145
            }
146
147
            $response['error']      = $error;
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...
148
            $response['message']    = $message;
149
            $response['status']     = true;
150
151
            return response()->json($response);
152
    }
153
154
    /**
155
     * Store a newly created resource in storage.
156
     *
157
     * @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...
158
     * @return \Illuminate\Http\Response
159
     */
160
    public function show($id)
161
    {
162
        $data_akademik = $this->data_akademik->findOrFail($id);
163
164
        array_set($data_akademik, 'user', $data_akademik->user->name);
165
166
        $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...
167
        $response['status']             = true;
168
169
        return response()->json($response);
170
    }
171
172
    /**
173
     * Show the form for editing the specified resource.
174
     *
175
     * @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...
176
     * @return \Illuminate\Http\Response
177
     */
178
    public function edit($id)
179
    {
180
        $data_akademik = $this->data_akademik->findOrFail($id);
181
182
        array_set($data_akademik->user, 'label', $data_akademik->user->name);
183
184
        $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...
185
        $response['user']               = $data_akademik->user;
186
        //$response['status']             = true;
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...
187
        $response['error']              = false;
188
        $response['message']            = 'Success';
189
        $response['status']             = true;
190
191
        return response()->json($response);
192
    }
193
194
    /**
195
     * Update the specified resource in storage.
196
     *
197
     * @param  \Illuminate\Http\Request  $request
198
     * @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...
199
     * @return \Illuminate\Http\Response
200
     */
201 View Code Duplication
    public function update(Request $request, $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...
202
    {
203
        $data_akademik = $this->data_akademik->findOrFail($id);
204
        {
205
            $validator = Validator::make($request->all(), [
206
                'nomor_un'          => "required|max:255|unique:{$this->data_akademik->getTable()},nomor_un,{$id},id,deleted_at,NULL",
207
                'nama_siswa'        => 'required',
208
                'nomor_kk'          => 'required',
209
                'bahasa_indonesia'  => 'required|numeric',
210
                'bahasa_inggris'    => 'required|numeric',
211
                'matematika'        => 'required|numeric',
212
                'ipa'               => 'required|numeric',
213
                'user_id'           => 'required',
214
215
            ]);
216
        if ($validator->fails()) {
217
                $error      = true;
218
                $message    = $validator->errors()->first();
219
        } else {
220
                $data_akademik->nomor_un          = $request->input('nomor_un');
221
                $data_akademik->nomor_kk          = $request->input('nomor_kk');
222
                $data_akademik->nama_siswa        = $request->input('nama_siswa');
223
                $data_akademik->user_id           = $request->input('user_id');
224
                $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
225
                $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
226
                $data_akademik->matematika        = $request->input('matematika');
227
                $data_akademik->ipa               = $request->input('ipa');
228
                $data_akademik->save();
229
230
                $error      = false;
231
                $message    = 'Success';
232
            }
233
        }
234
235
        $response['error']      = $error;
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...
236
        $response['message']    = $message;
237
        $response['status']     = true;
238
        return response()->json($response);
239
    }
240
241
    /**
242
     * Remove the specified resource from storage.
243
     *
244
     * @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...
245
     * @return \Illuminate\Http\Response
246
     */
247
    public function destroy($id)
248
    {
249
        $data_akademik = $this->data_akademik->findOrFail($id);
250
251
        if ($data_akademik->delete()) {
252
            $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...
253
        } else {
254
            $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...
255
        }
256
257
        return json_encode($response);
258
    }
259
}
260