Completed
Push — master ( 2ea9b4...b39495 )
by
unknown
11s
created

ProdiSekolahController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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