Completed
Push — master ( b49e4b...955280 )
by
unknown
10s
created

MasterZonaController::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Zona\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Zona\Facades\ZonaFacade;
9
10
/* Models */
11
use Bantenprov\Zona\Models\Bantenprov\Zona\MasterZona;
12
use App\User;
13
14
/* Etc */
15
use Validator;
16
use Auth;
17
18
/**
19
 * The MasterZonaController class.
20
 *
21
 * @package Bantenprov\Zona
22
 * @author  bantenprov <[email protected]>
23
 */
24
class MasterZonaController extends Controller
25
{
26
    protected $master_zona;
27
    protected $user;
28
29
    /**
30
     * Create a new controller instance.
31
     *
32
     * @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...
33
     */
34
    public function __construct()
35
    {
36
        $this->master_zona  = new MasterZona;
37
        $this->user         = new User;
38
    }
39
40
    /**
41
     * Display a listing of the resource.
42
     *
43
     * @return \Illuminate\Http\Response
44
     */
45
    public function index(Request $request)
46
    {
47 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...
48
            list($sortCol, $sortDir) = explode('|', request()->sort);
49
50
            $query = $this->master_zona->orderBy($sortCol, $sortDir);
51
        } else {
52
            $query = $this->master_zona->orderBy('id', 'asc');
53
        }
54
55
        if ($request->exists('filter')) {
56
            $query->where(function($q) use($request) {
57
                $value = "%{$request->filter}%";
58
59
                $q->where('tingkat', 'like', $value)
60
                    ->orWhere('kode', 'like', $value)
61
                    ->orWhere('label', 'like', $value);
62
            });
63
        }
64
65
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
66
67
        $response   = $query->with(['user'])->paginate($perPage);
68
69
        return response()->json($response)
70
            ->header('Access-Control-Allow-Origin', '*')
71
            ->header('Access-Control-Allow-Methods', 'GET');
72
    }
73
74
    /**
75
     * Display a listing of the resource.
76
     *
77
     * @return \Illuminate\Http\Response
78
     */
79 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...
80
    {
81
        $master_zonas = $this->master_zona->with(['user'])->get();
82
83
        // foreach($master_zonas as $master_zona){
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
84
        //     array_set($master_zona, 'label', $master_zona->label);
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...
85
        // }
86
87
        $response['master_zonas']   = $master_zonas;
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...
88
        $response['error']          = false;
89
        $response['message']        = 'Success';
90
        $response['status']         = true;
91
92
        return response()->json($response);
93
    }
94
95
    /**
96
     * Show the form for creating a new resource.
97
     *
98
     * @return \Illuminate\Http\Response
99
     */
100
    public function create()
101
    {
102
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
103
        $master_zona    = $this->master_zona->getAttributes();
104
        $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...
105
        $users_special  = $this->user->all();
106
        $users_standar  = $this->user->findOrFail($user_id);
107
        $current_user   = Auth::User();
108
109
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
110
111 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...
112
            $user_special = true;
113
114
            foreach($users_special as $user){
115
                array_set($user, 'label', $user->name);
116
            }
117
118
            $users = $users_special;
119
        }else{
120
            $user_special = false;
121
122
            array_set($users_standar, 'label', $users_standar->name);
123
124
            $users = $users_standar;
125
        }
126
127
        array_set($current_user, 'label', $current_user->name);
128
129
        $response['master_zona']    = $master_zona;
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...
130
        $response['users']          = $users;
131
        $response['user_special']   = $user_special;
132
        $response['current_user']   = $current_user;
133
        $response['error']          = false;
134
        $response['message']        = 'Success';
135
        $response['status']         = true;
136
137
        return response()->json($response);
138
    }
139
140
    /**
141
     * Store a newly created resource in storage.
142
     *
143
     * @param  \Illuminate\Http\Request  $request
144
     * @return \Illuminate\Http\Response
145
     */
146 View Code Duplication
    public function store(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
147
    {
148
        $master_zona = $this->master_zona;
149
150
        $validator = Validator::make($request->all(), [
151
            'tingkat'   => 'required|numeric',
152
            'kode'      => "required|numeric|unique:{$this->master_zona->getTable()},kode,NULL,id,deleted_at,NULL",
153
            'label'     => 'required|max:255',
154
            'user_id'   => "required|exists:{$this->user->getTable()},id",
155
        ]);
156
157
        if ($validator->fails()) {
158
            $error      = true;
159
            $message    = $validator->errors()->first();
160
        } else {
161
            $master_zona->tingkat   = $request->input('tingkat');
162
            $master_zona->kode      = $request->input('kode');
163
            $master_zona->label     = $request->input('label');
164
            $master_zona->user_id   = $request->input('user_id');
165
            $master_zona->save();
166
167
            $error      = false;
168
            $message    = 'Success';
169
        }
170
171
        $response['master_zona']    = $master_zona;
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...
172
        $response['error']          = $error;
173
        $response['message']        = $message;
174
        $response['status']         = true;
175
176
        return response()->json($response);
177
    }
178
179
    /**
180
     * Display the specified resource.
181
     *
182
     * @param  \App\MasterZona  $master_zona
0 ignored issues
show
Bug introduced by
There is no parameter named $master_zona. 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...
183
     * @return \Illuminate\Http\Response
184
     */
185 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...
186
    {
187
        $master_zona    = $this->master_zona->with(['user'])->findOrFail($id);
188
189
        $response['master_zona']    = $master_zona;
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...
190
        $response['error']          = false;
191
        $response['message']        = 'Success';
192
        $response['status']         = true;
193
194
        return response()->json($response);
195
    }
196
197
    /**
198
     * Show the form for editing the specified resource.
199
     *
200
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
201
     * @return \Illuminate\Http\Response
202
     */
203
    public function edit($id)
204
    {
205
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
206
        $master_zona    = $this->master_zona->with(['user'])->findOrFail($id);
207
        $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...
208
        $users_special  = $this->user->all();
209
        $users_standar  = $this->user->findOrFail($user_id);
210
        $current_user   = Auth::User();
211
212
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
213
214
        if ($master_zona->user !== null) {
215
            array_set($master_zona->user, 'label', $master_zona->user->name);
216
        }
217
218 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...
219
            $user_special = true;
220
221
            foreach($users_special as $user){
222
                array_set($user, 'label', $user->name);
223
            }
224
225
            $users = $users_special;
226
        }else{
227
            $user_special = false;
228
229
            array_set($users_standar, 'label', $users_standar->name);
230
231
            $users = $users_standar;
232
        }
233
234
        array_set($current_user, 'label', $current_user->name);
235
236
        $response['master_zona']    = $master_zona;
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...
237
        $response['users']          = $users;
238
        $response['user_special']   = $user_special;
239
        $response['current_user']   = $current_user;
240
        $response['error']          = false;
241
        $response['message']        = 'Success';
242
        $response['status']         = true;
243
244
        return response()->json($response);
245
    }
246
247
    /**
248
     * Update the specified resource in storage.
249
     *
250
     * @param  \Illuminate\Http\Request  $request
251
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
252
     * @return \Illuminate\Http\Response
253
     */
254 View Code Duplication
    public function update(Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
255
    {
256
        $master_zona = $this->master_zona->with(['user'])->findOrFail($id);
257
258
        $validator = Validator::make($request->all(), [
259
            'tingkat'   => 'required|numeric',
260
            'kode'      => "required|numeric|unique:{$this->master_zona->getTable()},kode,{$id},id,deleted_at,NULL",
261
            'label'     => 'required|max:255',
262
            'user_id'   => "required|exists:{$this->user->getTable()},id",
263
        ]);
264
265
        if ($validator->fails()) {
266
            $error      = true;
267
            $message    = $validator->errors()->first();
268
        } else {
269
            $master_zona->tingkat   = $request->input('tingkat');
270
            $master_zona->kode      = $request->input('kode');
271
            $master_zona->label     = $request->input('label');
272
            $master_zona->user_id   = $request->input('user_id');
273
            $master_zona->save();
274
275
            $error      = false;
276
            $message    = 'Success';
277
        }
278
279
        $response['error']      = $error;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
280
        $response['message']    = $message;
281
        $response['status']     = true;
282
283
        return response()->json($response);
284
    }
285
286
    /**
287
     * Remove the specified resource from storage.
288
     *
289
     * @param  \App\MasterZona  $master-zona
0 ignored issues
show
Bug introduced by
There is no parameter named $master-zona. 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
        $master_zona = $this->master_zona->findOrFail($id);
295
296
        if ($master_zona->delete()) {
297
            $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...
298
            $response['success']    = true;
299
            $response['status']     = true;
300
        } else {
301
            $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...
302
            $response['success']    = false;
303
            $response['status']     = false;
304
        }
305
306
        return json_encode($response);
307
    }
308
}
309