Passed
Push — master ( 915018...9edb23 )
by
unknown
03:47
created

AnggaranController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Anggaran\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Http\Request;
8
use Bantenprov\BudgetAbsorption\Facades\AnggaranFacade;
0 ignored issues
show
Bug introduced by
The type Bantenprov\BudgetAbsorption\Facades\AnggaranFacade was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/* Models */
11
use Bantenprov\Anggaran\Models\Bantenprov\Anggaran\Anggaran;
12
use Bantenprov\GroupEgovernment\Models\Bantenprov\GroupEgovernment\GroupEgovernment;
0 ignored issues
show
Bug introduced by
The type Bantenprov\GroupEgovernm...rnment\GroupEgovernment was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use App\User;
0 ignored issues
show
Bug introduced by
The type App\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/* Etc */
16
use Validator;
17
18
/**
19
 * The AnggaranController class.
20
 *
21
 * @package Bantenprov\Anggaran
22
 * @author  bantenprov <[email protected]>
23
 */
24
class AnggaranController extends Controller
25
{  
26
    /**
27
     * Create a new controller instance.
28
     *
29
     * @return void
30
     */
31
    protected $group_egovernmentModel;
32
    protected $anggaran;
33
    protected $user;
34
35
    public function __construct(Anggaran $anggaran, GroupEgovernment $group_egovernment, User $user)
36
    {
37
        $this->anggaran      = $anggaran;
38
        $this->group_egovernmentModel    = $group_egovernment;
39
        $this->user             = $user;
40
    }
41
42
    /**
43
     * Display a listing of the resource.
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47
    public function index(Request $request)
48
    {
49
        if (request()->has('sort')) {
50
            list($sortCol, $sortDir) = explode('|', request()->sort);
51
52
            $query = $this->anggaran->orderBy($sortCol, $sortDir);
53
        } else {
54
            $query = $this->anggaran->orderBy('id', 'asc');
55
        }
56
57
        if ($request->exists('filter')) {
58
            $query->where(function($q) use($request) {
59
                $value = "%{$request->filter}%";
60
                $q->where('label', 'like', $value)
61
                    ->orWhere('description', 'like', $value);
62
            });
63
        }
64
65
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
66
        $response = $query->paginate($perPage);
67
68
        foreach($response as $group_egovernment){
69
            array_set($response->data, 'group_egovernment', $group_egovernment->group_egovernment->label);
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Illuminate\Pagination\LengthAwarePaginator.
Loading history...
70
        }
71
72
        foreach($response as $user){
73
            array_set($response->data, 'user', $user->user->name);
74
        }
75
76
        return response()->json($response)
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...-Allow-Methods', 'GET') returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
77
            ->header('Access-Control-Allow-Origin', '*')
78
            ->header('Access-Control-Allow-Methods', 'GET');
79
    }
80
81
    /**
82
     * Show the form for creating a new resource.
83
     *
84
     * @return \Illuminate\Http\Response
85
     */
86
    public function create()
87
    {
88
        $group_egovernment = $this->group_egovernmentModel->all();
89
        $users = $this->user->all();
90
91
        foreach($users as $user){
92
            array_set($user, 'label', $user->name);
93
        }
94
95
        $response['group_egovernment'] = $group_egovernment;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
96
        $response['user'] = $users;
97
        $response['status'] = true;
98
99
        return response()->json($response);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($response) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
100
    }
101
102
    /**
103
     * Display the specified resource.
104
     *
105
     * @param  \App\Anggaran  $anggaran
0 ignored issues
show
Bug introduced by
The type App\Anggaran was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
106
     * @return \Illuminate\Http\Response
107
     */
108
    public function store(Request $request)
109
    {
110
        $anggaran = $this->anggaran;
111
112
        $validator = Validator::make($request->all(), [
113
            'group_egovernment_id' => 'required',
114
            'user_id' => 'required',
115
            'label' => 'required|max:16|unique:anggarans,label',
116
            'description' => 'max:255',
117
        ]);
118
119
        if($validator->fails()){
120
            $check = $anggaran->where('label',$request->label)->whereNull('deleted_at')->count();
121
122
            if ($check > 0) {
123
                $response['message'] = 'Failed, label ' . $request->label . ' already exists';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
124
            } else {
125
                $anggaran->group_egovernment_id = $request->input('group_egovernment_id');
126
                $anggaran->user_id = $request->input('user_id');
127
                $anggaran->label = $request->input('label');
128
                $anggaran->description = $request->input('description');
129
                $anggaran->save();
130
131
                $response['message'] = 'success';
132
            }
133
        } else {
134
            $anggaran->group_egovernment_id = $request->input('group_egovernment_id');
135
            $anggaran->user_id = $request->input('user_id');
136
            $anggaran->label = $request->input('label');
137
            $anggaran->description = $request->input('description');
138
            $anggaran->save();
139
            $response['message'] = 'success';
140
        }
141
142
        $response['status'] = true;
143
144
        return response()->json($response);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($response) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
145
    }
146
147
    /**
148
     * Store a newly created resource in storage.
149
     *
150
     * @param  \Illuminate\Http\Request  $request
151
     * @return \Illuminate\Http\Response
152
     */
153
    public function show($id)
154
    {
155
        $anggaran = $this->anggaran->findOrFail($id);
156
157
        $response['anggaran'] = $anggaran;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
158
        $response['group_egovernment'] = $anggaran->group_egovernment;
159
        $response['user'] = $anggaran->user;
160
        $response['status'] = true;
161
162
        return response()->json($response);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($response) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
163
    }
164
165
    /**
166
     * Show the form for editing the specified resource.
167
     *
168
     * @param  \App\Anggaran  $anggaran
169
     * @return \Illuminate\Http\Response
170
     */
171
    public function edit($id)
172
    {
173
        $anggaran = $this->anggaran->findOrFail($id);
174
175
        array_set($anggaran->user, 'label', $anggaran->user->name);
0 ignored issues
show
Bug introduced by
$anggaran->user of type App\User is incompatible with the type array expected by parameter $array of array_set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

175
        array_set(/** @scrutinizer ignore-type */ $anggaran->user, 'label', $anggaran->user->name);
Loading history...
176
177
        $response['anggaran'] = $anggaran;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
178
        $response['group_egovernment'] = $anggaran->group_egovernment;
179
        $response['user'] = $anggaran->user;
180
        $response['status'] = true;
181
182
        return response()->json($response);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($response) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
183
    }
184
185
    /**
186
     * Update the specified resource in storage.
187
     *
188
     * @param  \Illuminate\Http\Request  $request
189
     * @param  \App\Anggaran  $anggaran
190
     * @return \Illuminate\Http\Response
191
     */
192
    public function update(Request $request, $id)
193
    {
194
        $anggaran = $this->anggaran->findOrFail($id);
195
196
        if ($request->input('old_label') == $request->input('label'))
197
        {
198
            $validator = Validator::make($request->all(), [
199
                'label' => 'required|max:16',
200
                'description' => 'max:255',
201
                'group_egovernment_id' => 'required',
202
                'user_id' => 'required',
203
            ]);
204
        } else {
205
            $validator = Validator::make($request->all(), [
206
                'label' => 'required|max:16|unique:anggarans,label',
207
                'description' => 'max:255',
208
                'group_egovernment_id' => 'required',
209
                'user_id' => 'required',
210
            ]);
211
        }
212
213
        if ($validator->fails()) {
214
            $check = $anggaran->where('label',$request->label)->whereNull('deleted_at')->count();
215
216
            if ($check > 0) {
217
                $response['message'] = 'Failed, label ' . $request->label . ' already exists';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
218
            } else {
219
                $anggaran->label = $request->input('label');
220
                $anggaran->description = $request->input('description');
221
                $anggaran->group_egovernment_id = $request->input('group_egovernment_id');
222
                $anggaran->user_id = $request->input('user_id');
223
                $anggaran->save();
224
225
                $response['message'] = 'success';
226
            }
227
        } else {
228
            $anggaran->label = $request->input('label');
229
            $anggaran->description = $request->input('description');
230
            $anggaran->group_egovernment_id = $request->input('group_egovernment_id');
231
            $anggaran->user_id = $request->input('user_id');
232
            $anggaran->save();
233
234
            $response['message'] = 'success';
235
        }
236
237
        $response['status'] = true;
238
239
        return response()->json($response);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($response) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
240
    }
241
242
    /**
243
     * Remove the specified resource from storage.
244
     *
245
     * @param  \App\Anggaran  $anggaran
246
     * @return \Illuminate\Http\Response
247
     */
248
    public function destroy($id)
249
    {
250
        $anggaran = $this->anggaran->findOrFail($id);
251
252
        if ($anggaran->delete()) {
253
            $response['status'] = true;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
254
        } else {
255
            $response['status'] = false;
256
        }
257
258
        return json_encode($response);
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode($response) returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
259
    }
260
}
261