Completed
Push — master ( bd6010...92183f )
by
unknown
10s
created

ProdiSekolahController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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
10
/* Models */
11
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\ProdiSekolah;
12
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
13
use Bantenprov\ProgramKeahlian\Models\Bantenprov\ProgramKeahlian\ProgramKeahlian;
14
use App\User;
15
16
/* Etc */
17
use Validator;
18
use Auth;
19
20
/**
21
 * The ProdiSekolahController class.
22
 *
23
 * @package Bantenprov\Sekolah
24
 * @author  bantenprov <[email protected]>
25
 */
26
class ProdiSekolahController extends Controller
27
{
28
    protected $prodi_sekolah;
29
    protected $sekolah;
30
    protected $program_keahlian;
31
    protected $user;
32
33
    /**
34
     * Create a new controller instance.
35
     *
36
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
37
     */
38
    public function __construct()
39
    {
40
        $this->prodi_sekolah    = new ProdiSekolah;
41
        $this->sekolah          = new Sekolah;
42
        $this->program_keahlian = new ProgramKeahlian;
43
        $this->user             = new User;
44
    }
45
46
    /**
47
     * Display a listing of the resource.
48
     *
49
     * @return \Illuminate\Http\Response
50
     */
51
    public function index(Request $request)
52
    {
53 View Code Duplication
        if (request()->has('sort')) {
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...
54
            list($sortCol, $sortDir) = explode('|', request()->sort);
55
56
            $query = $this->prodi_sekolah->orderBy($sortCol, $sortDir);
57
        } else {
58
            $query = $this->prodi_sekolah->orderBy('id', 'asc');
59
        }
60
61 View Code Duplication
        if ($request->exists('filter')) {
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...
62
            $query->where(function($q) use($request) {
63
                $value = "%{$request->filter}%";
64
65
                $q->where('keterangan', 'like', $value)
66
                    ->orWhere('kuota_siswa', 'like', $value);
67
            });
68
        }
69
70
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
71
72
        $response = $query->with('sekolah', 'program_keahlian', 'user')->paginate($perPage);
73
74
        return response()->json($response)
75
            ->header('Access-Control-Allow-Origin', '*')
76
            ->header('Access-Control-Allow-Methods', 'GET');
77
    }
78
79
    /**
80
     * Display a listing of the resource.
81
     *
82
     * @return \Illuminate\Http\Response
83
     */
84 View Code Duplication
    public function get()
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...
85
    {
86
        $prodi_sekolahs = $this->prodi_sekolah->with('sekolah', 'program_keahlian', 'user')->get();
87
88
        foreach($prodi_sekolahs as $prodi_sekolah){
89
            array_set($prodi_sekolah, 'label', $prodi_sekolah->nama);
90
        }
91
92
        $response['prodi_sekolahs']   = $prodi_sekolahs;
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...
93
        $response['error']      = false;
94
        $response['message']    = 'Success';
95
        $response['status']     = true;
96
97
        return response()->json($response);
98
    }
99
100
    /**
101
     * Show the form for creating a new resource.
102
     *
103
     * @return \Illuminate\Http\Response
104
     */
105
    public function create()
106
    {
107
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
108
        $prodi_sekolah      = $this->prodi_sekolah->getAttributes();
109
        $sekolahs           = $this->sekolah->getAttributes();
110
        $program_keahlians  = $this->program_keahlian->all();
111
        $users              = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
        $users_special      = $this->user->all();
113
        $users_standar      = $this->user->findOrFail($user_id);
114
        $current_user       = Auth::User();
115
116
        foreach($sekolahs as $sekolah){
117
            array_set($sekolah, 'label', $sekolah->nama);
118
        }
119
120
        foreach($program_keahlians as $program_keahlian){
121
            array_set($program_keahlian, 'label', $program_keahlian->label);
122
        }
123
124
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
125
126 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...
127
            $user_special = true;
128
129
            foreach($users_special as $user){
130
                array_set($user, 'label', $user->name);
131
            }
132
133
            $users = $users_special;
134
        }else{
135
            $user_special = false;
136
137
            array_set($users_standar, 'label', $users_standar->name);
138
139
            $users = $users_standar;
140
        }
141
142
        array_set($current_user, 'label', $current_user->name);
143
144
        $response['prodi_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...
145
        $response['sekolahs']           = $sekolahs;
146
        $response['program_keahlians']  = $program_keahlians;
147
        $response['users']              = $users;
148
        $response['user_special']       = $user_special;
149
        $response['current_user']       = $current_user;
150
        $response['error']              = false;
151
        $response['message']            = 'Success';
152
        $response['status']             = true;
153
154
        return response()->json($response);
155
    }
156
157
    /**
158
     * Store a newly created resource in storage.
159
     *
160
     * @param  \Illuminate\Http\Request  $request
161
     * @return \Illuminate\Http\Response
162
     */
163
    public function store(Request $request)
164
    {
165
        $prodi_sekolah = $this->prodi_sekolah;
166
167
        $validator = Validator::make($request->all(), [
168
            'sekolah_id'            => "required|exists:{$this->sekolah->getTable()},id",
169
            'program_keahlian_id'   => "required|exists:{$this->program_keahlian->getTable()},id|unique:{$this->prodi_sekolah->getTable()},program_keahlian_id,NULL,id,sekolah_id,{$request->input('sekolah_id')},deleted_at,NULL",
170
            'kuota_siswa'           => 'required|numeric|min:0|max:100000',
171
            'keterangan'            => 'required|max:255',
172
            'user_id'               => "required|exists:{$this->user->getTable()},id",
173
        ]);
174
175
        if ($validator->fails()) {
176
            $error      = true;
177
            $message    = $validator->errors()->first();
178 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
179
            $prodi_sekolah->sekolah_id          = $request->input('sekolah_id');
180
            $prodi_sekolah->program_keahlian_id = $request->input('program_keahlian_id');
181
            $prodi_sekolah->kuota_siswa         = $request->input('kuota_siswa');
182
            $prodi_sekolah->keterangan          = $request->input('keterangan');
183
            $prodi_sekolah->user_id             = $request->input('user_id');
184
            $prodi_sekolah->save();
185
186
            $error      = false;
187
            $message    = 'Success';
188
        }
189
190
        $response['prodi_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...
191
        $response['error']          = $error;
192
        $response['message']        = $message;
193
        $response['status']         = true;
194
195
        return response()->json($response);
196
    }
197
198
    /**
199
     * Display the specified resource.
200
     *
201
     * @param  \App\ProdiSekolah  $prodi_sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $prodi_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...
202
     * @return \Illuminate\Http\Response
203
     */
204 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...
205
    {
206
        $prodi_sekolah = $this->prodi_sekolah->with(['sekolah', 'program_keahlian', 'user'])->findOrFail($id);
207
208
        $response['prodi_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...
209
        $response['error']          = false;
210
        $response['message']        = 'Success';
211
        $response['status']         = true;
212
213
        return response()->json($response);
214
    }
215
216
    /**
217
     * Show the form for editing the specified resource.
218
     *
219
     * @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...
220
     * @return \Illuminate\Http\Response
221
     */
222 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...
223
    {
224
        $prodi_sekolah = $this->prodi_sekolah->with(['sekolah', 'program_keahlian', 'user'])->findOrFail($id);
225
226
        $response['prodi_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...
227
        $response['error']          = false;
228
        $response['message']        = 'Success';
229
        $response['status']         = true;
230
231
        return response()->json($response);
232
    }
233
234
    /**
235
     * Update the specified resource in storage.
236
     *
237
     * @param  \Illuminate\Http\Request  $request
238
     * @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...
239
     * @return \Illuminate\Http\Response
240
     */
241
    public function update(Request $request, $id)
242
    {
243
        $response = array();
244
        $message  = array();
245
246
        $prodi_sekolah = $this->prodi_sekolah->findOrFail($id);
247
248
            $validator = Validator::make($request->all(), [
249
                'sekolah_id'             => 'required',
250
                'user_id'                => 'required|unique:prodi_sekolahs,user_id,'.$id,
251
                'keterangan'             => 'required',
252
                'kuota_siswa'            => 'required',
253
                'program_keahlian_id'    => 'required',
254
            ]);
255
256
            if($validator->fails()){
257
258
                foreach($validator->messages()->getMessages() as $key => $error){
259
                    foreach($error AS $error_get) {
260
                        array_push($message, $error_get);
261
                    }
262
                }
263
264
                $check_user   = $this->prodi_sekolah->where('id','!=', $id)->where('user_id', $request->user_id);
265
266
            if($check_user->count() > 0){
267
                    $response['message'] = implode("\n",$message);
268
269 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
270
                $prodi_sekolah->sekolah_id          = $request->input('sekolah_id');
271
                $prodi_sekolah->user_id             = $request->input('user_id');
272
                $prodi_sekolah->program_keahlian_id = $request->input('program_keahlian_id');
273
                $prodi_sekolah->keterangan          = $request->input('keterangan');
274
                $prodi_sekolah->kuota_siswa         = $request->input('kuota_siswa');
275
                $prodi_sekolah->save();
276
277
                $response['message'] = 'success';
278
            }
279
280 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
281
                $prodi_sekolah->sekolah_id          = $request->input('sekolah_id');
282
                $prodi_sekolah->user_id             = $request->input('user_id');
283
                $prodi_sekolah->program_keahlian_id = $request->input('program_keahlian_id');
284
                $prodi_sekolah->keterangan          = $request->input('keterangan');
285
                $prodi_sekolah->kuota_siswa         = $request->input('kuota_siswa');
286
                $prodi_sekolah->save();
287
288
            $response['message'] = 'success';
289
        }
290
291
        $response['status'] = true;
292
293
        return response()->json($response);
294
    }
295
296
    /**
297
     * Remove the specified resource from storage.
298
     *
299
     * @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...
300
     * @return \Illuminate\Http\Response
301
     */
302 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...
303
    {
304
        $prodi_sekolah = $this->prodi_sekolah->findOrFail($id);
305
306
        if ($prodi_sekolah->delete()) {
307
            $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...
308
            $response['success']    = true;
309
            $response['status']     = true;
310
        } else {
311
            $response['message']    = 'Failed';
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...
312
            $response['success']    = false;
313
            $response['status']     = false;
314
        }
315
316
        return json_encode($response);
317
    }
318
}
319