Completed
Pull Request — master (#12)
by
unknown
01:21
created

ProdiSekolahController::create()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 37
Code Lines 25

Duplication

Lines 11
Ratio 29.73 %

Importance

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