Completed
Push — master ( 1408c1...55118e )
by
unknown
12s queued 10s
created

MasterZonaController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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