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

SekolahController::store()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

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