Passed
Push — master ( c3d3d3...41f3ed )
by
unknown
04:06 queued 01:50
created

AnggaranController::index()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 64
nop 1
dl 0
loc 36
rs 6.7272
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 Bantenprov\SectorEgovernment\Models\Bantenprov\SectorEgovernment\SectorEgovernment;
0 ignored issues
show
Bug introduced by
The type Bantenprov\SectorEgovern...nment\SectorEgovernment 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
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...
15
16
/* Etc */
17
use Validator;
18
19
/**
20
 * The AnggaranController class.
21
 *
22
 * @package Bantenprov\Anggaran
23
 * @author  bantenprov <[email protected]>
24
 */
25
class AnggaranController extends Controller
26
{  
27
    /**
28
     * Create a new controller instance.
29
     *
30
     * @return void
31
     */
32
    protected $group_egovernmentModel;
33
    protected $sector_egovernment;
34
    protected $anggaran;
35
    protected $user;
36
37
    public function __construct(Anggaran $anggaran, GroupEgovernment $group_egovernment,SectorEgovernment $sector_egovernment, User $user)
38
    {
39
        $this->anggaran      = $anggaran;
40
        $this->group_egovernmentModel    = $group_egovernment;
41
        $this->sector_egovernment    = $sector_egovernment;
42
        $this->user             = $user;
43
    }
44
45
    /**
46
     * Display a listing of the resource.
47
     *
48
     * @return \Illuminate\Http\Response
49
     */
50
    public function index(Request $request)
51
    {
52
        if (request()->has('sort')) {
53
            list($sortCol, $sortDir) = explode('|', request()->sort);
54
55
            $query = $this->anggaran->orderBy($sortCol, $sortDir);
56
        } else {
57
            $query = $this->anggaran->orderBy('id', 'asc');
58
        }
59
60
        if ($request->exists('filter')) {
61
            $query->where(function($q) use($request) {
62
                $value = "%{$request->filter}%";
63
                $q->where('label', 'like', $value)
64
                    ->orWhere('description', 'like', $value);
65
            });
66
        }
67
68
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
69
        $response = $query->paginate($perPage);
70
71
        foreach($response as $group_egovernment){
72
            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...
73
        }
74
75
        foreach($response as $sector_egovernment){
76
            array_set($response->data, 'sector_egovernment', $sector_egovernment->sector_egovernment->label);
77
        }
78
79
        foreach($response as $user){
80
            array_set($response->data, 'user', $user->user->name);
81
        }
82
83
        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...
84
            ->header('Access-Control-Allow-Origin', '*')
85
            ->header('Access-Control-Allow-Methods', 'GET');
86
    }
87
88
    /**
89
     * Show the form for creating a new resource.
90
     *
91
     * @return \Illuminate\Http\Response
92
     */
93
    public function create()
94
    {
95
        $group_egovernment = $this->group_egovernmentModel->all();
96
        $sector_egovernment = $this->sector_egovernment->all();
97
        $users = $this->user->all();
98
99
        foreach($users as $user){
100
            array_set($user, 'label', $user->name);
101
        }
102
103
        $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...
104
        $response['sector_egovernment'] = $sector_egovernment;
105
        $response['user'] = $users;
106
        $response['status'] = true;
107
108
        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...
109
    }
110
111
    /**
112
     * Display the specified resource.
113
     *
114
     * @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...
115
     * @return \Illuminate\Http\Response
116
     */
117
    public function store(Request $request)
118
    {
119
        $anggaran = $this->anggaran;
120
121
        $validator = Validator::make($request->all(), [
122
            'group_egovernment_id' => 'required',
123
            'sector_egovernment_id' => 'required',
124
            'user_id' => 'required',
125
            'label' => 'required|max:255|unique:anggarans,label',
126
            'description' => 'max:255',
127
        ]);
128
129
        if($validator->fails()){
130
            $check = $anggaran->where('label',$request->label)->whereNull('deleted_at')->count();
131
132
            if ($check > 0) {
133
                $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...
134
            } else {
135
                $anggaran->group_egovernment_id = $request->input('group_egovernment_id');
136
                $anggaran->sector_egovernment_id = $request->input('sector_egovernment_id');
137
                $anggaran->user_id = $request->input('user_id');
138
                $anggaran->label = $request->input('label');
139
                $anggaran->description = $request->input('description');
140
                $anggaran->save();
141
142
                $response['message'] = 'success';
143
            }
144
        } else {
145
            $anggaran->group_egovernment_id = $request->input('group_egovernment_id');
146
            $anggaran->sector_egovernment_id = $request->input('sector_egovernment_id');
147
            $anggaran->user_id = $request->input('user_id');
148
            $anggaran->label = $request->input('label');
149
            $anggaran->description = $request->input('description');
150
            $anggaran->save();
151
            $response['message'] = 'success';
152
        }
153
154
        $response['status'] = true;
155
156
        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...
157
    }
158
159
    /**
160
     * Store a newly created resource in storage.
161
     *
162
     * @param  \Illuminate\Http\Request  $request
163
     * @return \Illuminate\Http\Response
164
     */
165
    public function show($id)
166
    {
167
        $anggaran = $this->anggaran->findOrFail($id);
168
169
        $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...
170
        $response['group_egovernment'] = $anggaran->group_egovernment;
171
        $response['sector_egovernment'] = $anggaran->sector_egovernment;
172
        $response['user'] = $anggaran->user;
173
        $response['status'] = true;
174
175
        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...
176
    }
177
178
    /**
179
     * Show the form for editing the specified resource.
180
     *
181
     * @param  \App\Anggaran  $anggaran
182
     * @return \Illuminate\Http\Response
183
     */
184
    public function edit($id)
185
    {
186
        $anggaran = $this->anggaran->findOrFail($id);
187
188
        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

188
        array_set(/** @scrutinizer ignore-type */ $anggaran->user, 'label', $anggaran->user->name);
Loading history...
189
190
        $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...
191
        $response['group_egovernment'] = $anggaran->group_egovernment;
192
        $response['sector_egovernment'] = $anggaran->sector_egovernment;
193
        $response['user'] = $anggaran->user;
194
        $response['status'] = true;
195
196
        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...
197
    }
198
199
    /**
200
     * Update the specified resource in storage.
201
     *
202
     * @param  \Illuminate\Http\Request  $request
203
     * @param  \App\Anggaran  $anggaran
204
     * @return \Illuminate\Http\Response
205
     */
206
    public function update(Request $request, $id)
207
    {
208
        $anggaran = $this->anggaran->findOrFail($id);
209
210
        if ($request->input('old_label') == $request->input('label'))
211
        {
212
            $validator = Validator::make($request->all(), [
213
                'label' => 'required|max:255',
214
                'description' => 'max:255',
215
                'group_egovernment_id' => 'required',
216
                'sector_egovernment_id' => 'required',
217
                'user_id' => 'required',
218
            ]);
219
        } else {
220
            $validator = Validator::make($request->all(), [
221
                'label' => 'required|max:255|unique:anggarans,label',
222
                'description' => 'max:255',
223
                'group_egovernment_id' => 'required',
224
                'sector_egovernment_id' => 'required',
225
                'user_id' => 'required',
226
            ]);
227
        }
228
229
        if ($validator->fails()) {
230
            $check = $anggaran->where('label',$request->label)->whereNull('deleted_at')->count();
231
232
            if ($check > 0) {
233
                $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...
234
            } else {
235
                $anggaran->label = $request->input('label');
236
                $anggaran->description = $request->input('description');
237
                $anggaran->group_egovernment_id = $request->input('group_egovernment_id');
238
                $anggaran->sector_egovernment_id = $request->input('sector_egovernment_id');
239
                $anggaran->user_id = $request->input('user_id');
240
                $anggaran->save();
241
242
                $response['message'] = 'success';
243
            }
244
        } else {
245
            $anggaran->label = $request->input('label');
246
            $anggaran->description = $request->input('description');
247
            $anggaran->group_egovernment_id = $request->input('group_egovernment_id');
248
            $anggaran->sector_egovernment_id = $request->input('sector_egovernment_id');
249
            $anggaran->user_id = $request->input('user_id');
250
            $anggaran->save();
251
252
            $response['message'] = 'success';
253
        }
254
255
        $response['status'] = true;
256
257
        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...
258
    }
259
260
    /**
261
     * Remove the specified resource from storage.
262
     *
263
     * @param  \App\Anggaran  $anggaran
264
     * @return \Illuminate\Http\Response
265
     */
266
    public function destroy($id)
267
    {
268
        $anggaran = $this->anggaran->findOrFail($id);
269
270
        if ($anggaran->delete()) {
271
            $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...
272
        } else {
273
            $response['status'] = false;
274
        }
275
276
        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...
277
    }
278
}
279