Test Failed
Push — master ( e780be...09f3c0 )
by
unknown
02:15
created

EpormasCounterController::show()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 27
Code Lines 25

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
dl 27
loc 27
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 25
nc 3
nop 2
1
<?php
2
3
namespace Bantenprov\DashboardEpormas\Http\Controllers;
4
5
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...
6
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request 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\Support\Facades\DB;
8
use Illuminate\Support\Facades\Input;
9
use Bantenprov\DashboardEpormas\Models\EpormasCounter;
10
use Bantenprov\DashboardEpormas\Models\EpormasCategory;
11
use Bantenprov\DashboardEpormas\Models\EpormasCity;
12
use Validator, Image, Session, File, Response, Redirect, Exception;
13
14
class EpormasCounterController extends Controller
15
{
16
17
  /**
18
   * Display a listing of the resource.
19
   *
20
   * @return Response
21
   */
22 View Code Duplication
  public function index()
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...
23
  {
24
      try {
25
          $error = false;
26
          $statusCode = 200;
27
          $title = 'Success';
28
          $type = 'success';
29
          $message = 'Success';
30
          $result = EpormasCounter::whereNull('deleted_at')
31
                          ->with('getCity')
32
                          ->with('getCategory')
33
                          ->get();
34
      } catch (Exception $e) {
35
          $error = true;
36
          $statusCode = 404;
37
          $title = 'Error';
38
          $type = 'error';
39
          $message = 'Error';
40
          $result = 'Not Found';
41
      } finally {
42
          return Response::json(array(
43
            'error' => $error,
44
            'status' => $statusCode,
45
            'title' => $title,
46
            'type' => $type,
47
            'message' => $message,
48
            'result' => $result
49
          ));
50
      }
51
  }
52
53
  /**
54
   * Show the form for creating a new resource.
55
   *
56
   * @return Response
57
   */
58
  public function create()
59
  {
60
      try {
61
          $error = false;
62
          $statusCode = 200;
63
          $title = 'Success';
64
          $type = 'success';
65
          $message = 'Success';
66
          $city = EpormasCity::all();
67
          $category = EpormasCategory::all();
68
      } catch (Exception $e) {
69
          $error = true;
70
          $statusCode = 404;
71
          $title = 'Error';
72
          $type = 'error';
73
          $message = 'Error';
74
          $city = 'Not Found';
75
          $category = 'Not Found';
76
      } finally {
77
          return Response::json(array(
78
            'error' => $error,
79
            'status' => $statusCode,
80
            'title' => $title,
81
            'type' => $type,
82
            'message' => $message,
83
            'city' => $city,
84
            'category' => $category
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $category does not seem to be defined for all execution paths leading up to this point.
Loading history...
85
          ));
86
      }
87
  }
88
89
  /**
90
   * Store a newly created resource in storage.
91
   *
92
   * @return Response
93
   */
94
  public function store(Request $request, $version='')
95
  {
96
        $path = \Request::path();
97
        $explode = explode('/', $path);
98
99
        $from = 'form';
100
        if(in_array('api',$explode)){
101
          $from = 'api';
102
        }
103
104
        $via = $from;
105
        if($version != '' && $version != 'store'){
106
          $via .= '-'.$version;
107
        }
108
109
      	$rules = [
110
            'count' => 'required|numeric',
111
            'city_id' => 'required|numeric',
112
            'category_id' => 'required|numeric',
113
            'tanggal' => 'required|date',
114
        ];
115
116
        $validator = Validator::make($request->all(), $rules);
117
        if ($validator->fails()) {
118
          return response()->json([
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

118
          return /** @scrutinizer ignore-call */ response()->json([
Loading history...
119
              'title' => 'Error',
120
              'type'  => 'error',
121
              'message' => $validator->errors()->all()
122
          ]);
123
        }
124
125
        $format = date('Y-m-d', strtotime(str_replace(' ','-',$request->tanggal)));
0 ignored issues
show
Bug introduced by
It seems like strtotime(str_replace(' ...-', $request->tanggal)) can also be of type false; however, parameter $timestamp of date() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

125
        $format = date('Y-m-d', /** @scrutinizer ignore-type */ strtotime(str_replace(' ','-',$request->tanggal)));
Loading history...
126
        $resultcek = EpormasCounter::whereNull('deleted_at')
127
                             ->where('tanggal','like','%'.$format.'%')
0 ignored issues
show
Bug introduced by
Are you sure $format of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

127
                             ->where('tanggal','like','%'./** @scrutinizer ignore-type */ $format.'%')
Loading history...
128
                             ->where('category_id',$request->category_id)
129
                             ->where('city_id',$request->city_id)
130
                             ->groupBy('tahun','bulan','category_id','city_id')
131
                             ->orderBy('bulan')
132
                             ->count();
133
        if($resultcek > 0){
134
          return response()->json([
135
              'title' => 'Error',
136
              'type'  => 'error',
137
              'message' => 'Data has already been taken.'
138
          ]);
139
        }
140
141
        $date = explode("-",$format);
0 ignored issues
show
Bug introduced by
It seems like $format can also be of type false; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

141
        $date = explode("-",/** @scrutinizer ignore-type */ $format);
Loading history...
142
        $data = EpormasCounter::whereNull('deleted_at')
143
                       ->where('tahun', $date[0])
144
                       ->where('bulan', $date[1])
145
                       ->where('category_id',$request->category_id)
146
                       ->where('city_id',$request->city_id)
147
                       ->groupBy('tahun','bulan','category_id','city_id')
148
                       ->orderBy('bulan')
149
                       ->count();
150
        if($data > 0){
151
          return response()->json([
152
              'title' => 'Error',
153
              'type'  => 'error',
154
              'message' => 'Data has already been taken.'
155
          ]);
156
        }
157
158
        try {
159
            $error = false;
160
            $statusCode = 200;
161
            $title = 'Success';
162
            $type = 'success';
163
            $message = 'Data created successfully';
164
            $result = EpormasCounter::create([
165
                'count' => $request->count,
166
                'category_id' => $request->category_id,
167
                'city_id' => $request->city_id,
168
                'tahun' => $date[0],
169
                'bulan' => $date[1],
170
                'tanggal' => $format,
171
                'user_id' => $request->user_id,
172
                'via' => $via
173
            ]);
174
        } catch (Exception $e) {
175
            $error = true;
176
            $statusCode = 404;
177
            $title = 'Error';
178
            $type = 'error';
179
            $message = 'Error';
180
            $result = 'Not Found';
181
        } finally {
182
            return Response::json(array(
183
              'error' => $error,
184
              'status' => $statusCode,
185
              'title' => $title,
186
              'type' => $type,
187
              'message' => $message,
188
              'result' => $result
189
            ));
190
        }
191
  }
192
193
  /**
194
   * Display the specified resource.
195
   *
196
   * @param  int  $id
197
   * @return Response
198
   */
199 View Code Duplication
  public function show($version='', $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
        try {
202
            $error = false;
203
            $statusCode = 200;
204
            $title = 'Success';
205
            $type = 'success';
206
            $message = 'Success';
207
            $result = EpormasCounter::whereNull('deleted_at')
208
                            ->with('getCity')
209
                            ->with('getCategory')
210
                            ->find($id);
211
        } catch (Exception $e) {
212
            $error = true;
213
            $statusCode = 404;
214
            $title = 'Error';
215
            $type = 'error';
216
            $message = 'Error';
217
            $result = 'Not Found';
218
        } finally {
219
            return Response::json(array(
220
              'error' => $error,
221
              'status' => $statusCode,
222
              'title' => $title,
223
              'type' => $type,
224
              'message' => $message,
225
              'result' => $result
226
            ));
227
        }
228
  }
229
230
  /**
231
   * Show the form for editing the specified resource.
232
   *
233
   * @param  int  $id
234
   * @return Response
235
   */
236
  public function edit($id)
237
  {
238
        try {
239
            $error = false;
240
            $statusCode = 200;
241
            $title = 'Success';
242
            $type = 'success';
243
            $message = 'Success';
244
            $city = EpormasCity::all();
245
            $category = EpormasCategory::all();
246
            $result = EpormasCounter::whereNull('deleted_at')
247
                            ->with('getCity')
248
                            ->with('getCategory')
249
                            ->find($id);
250
            $format = date('Y-m-d', strtotime($result->tanggal));
0 ignored issues
show
Bug introduced by
It seems like strtotime($result->tanggal) can also be of type false; however, parameter $timestamp of date() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

250
            $format = date('Y-m-d', /** @scrutinizer ignore-type */ strtotime($result->tanggal));
Loading history...
251
        } catch (Exception $e) {
252
            $error = true;
253
            $statusCode = 404;
254
            $title = 'Error';
255
            $type = 'error';
256
            $message = 'Error';
257
            $result = 'Not Found';
258
            $city = 'Not Found';
259
            $format = 'Not Found';
260
            $category = 'Not Found';
261
        } finally {
262
            return Response::json(array(
263
              'error' => $error,
264
              'status' => $statusCode,
265
              'title' => $title,
266
              'type' => $type,
267
              'message' => $message,
268
              'result' => $result,
269
              'city' => $city,
270
              'tanggal' => $format,
271
              'category' => $category,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $category does not seem to be defined for all execution paths leading up to this point.
Loading history...
272
            ));
273
        }
274
  }
275
276
  /**
277
   * Update the specified resource in storage.
278
   *
279
   * @param  int  $id
280
   * @return Response
281
   */
282
  public function update(Request $request, $version='', $id)
283
  {
284
        $result = EpormasCounter::whereNull('deleted_at')
285
                        ->with('getCity')
286
                        ->with('getCategory')
287
                        ->find($id);
288
289
        $rules = [
290
            'count' => 'required|numeric',
291
            'city_id' => 'required|numeric',
292
            'category_id' => 'required|numeric',
293
            'tanggal' => 'required|date',
294
        ];
295
296
        $validator = Validator::make($request->all(), $rules);
297
        if ($validator->fails()) {
298
          return response()->json([
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

298
          return /** @scrutinizer ignore-call */ response()->json([
Loading history...
299
              'title' => 'Error',
300
              'type'  => 'error',
301
              'message' => $validator->errors()->all()
302
          ]);
303
        }
304
305
        $format = date('Y-m-d', strtotime(str_replace(' ','-',$request->tanggal)));
0 ignored issues
show
Bug introduced by
It seems like strtotime(str_replace(' ...-', $request->tanggal)) can also be of type false; however, parameter $timestamp of date() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

305
        $format = date('Y-m-d', /** @scrutinizer ignore-type */ strtotime(str_replace(' ','-',$request->tanggal)));
Loading history...
306
        if($result->city_id != $request->city_id || $result->category_id != $request->category_id){
307
            $resultcek = EpormasCounter::whereNull('deleted_at')
308
                                 ->where('tanggal','like','%'.$format.'%')
0 ignored issues
show
Bug introduced by
Are you sure $format of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

308
                                 ->where('tanggal','like','%'./** @scrutinizer ignore-type */ $format.'%')
Loading history...
309
            		                 ->where('category_id',$request->category_id)
310
            		                 ->where('city_id',$request->city_id)
311
                                 ->groupBy('tahun','bulan','category_id','city_id')
312
                                 ->orderBy('bulan')
313
                                 ->count();
314
            if($resultcek > 0){
315
              return response()->json([
316
                  'title' => 'Error',
317
                  'type'  => 'error',
318
                  'message' => 'Data has already been taken.'
319
              ]);
320
            }
321
        }
322
323
        $date = explode("-",$format);
0 ignored issues
show
Bug introduced by
It seems like $format can also be of type false; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

323
        $date = explode("-",/** @scrutinizer ignore-type */ $format);
Loading history...
324
        $dates = date('Y-m-d', strtotime($result->tanggal));
325
        if($dates != $format){
326
            $resultcek = EpormasCounter::whereNull('deleted_at')
327
            		                 ->where('category_id',$request->category_id)
328
            		                 ->where('city_id',$request->city_id)
329
                                 ->where('tanggal','like','%'.$format.'%')
330
                                 ->groupBy('tahun','bulan','category_id','city_id')
331
                                 ->orderBy('bulan')
332
                                 ->count();
333
            if($resultcek > 0){
334
              return response()->json([
335
                  'title' => 'Error',
336
                  'type'  => 'error',
337
                  'message' => 'Data has already been taken.'
338
              ]);
339
            }
340
341
            $data = EpormasCounter::whereNull('deleted_at')
342
                           ->where('tahun', $date[0])
343
                           ->where('bulan', $date[1])
344
            		           ->where('category_id',$request->category_id)
345
            		           ->where('city_id',$request->city_id)
346
                           ->groupBy('tahun','bulan','category_id','city_id')
347
                           ->orderBy('bulan')
348
                           ->count();
349
            if($data > 0){
350
              return response()->json([
351
                  'title' => 'Error',
352
                  'type'  => 'error',
353
                  'message' => 'Data has already been taken.'
354
              ]);
355
            }
356
        }
357
358
        $path = \Request::path();
359
        $explode = explode('/', $path);
360
361
        $from = $result->via;
362
        $user_id = $result->user_id;
363
        if(in_array('api',$explode)){
364
          $from = 'api';
365
          $user_id = $request->user_id;
366
        }
367
368
        $via = $from;
369
        if($version != '' && $version != 'update'){
370
          $via .= '-'.$version;
371
        }
372
373
        try {
374
            $error = false;
375
            $statusCode = 200;
376
            $title = 'Success';
377
            $type = 'success';
378
            $message = 'Data updated successfully';
379
            $result->update([
380
                'count' => $request->count,
381
                'category_id' => $request->category_id,
382
                'city_id' => $request->city_id,
383
                'tahun' => $date[0],
384
                'bulan' => $date[1],
385
                'tanggal' => $format,
386
                'user_id' => $user_id,
387
                'via' => $via
388
            ]);
389
        } catch (Exception $e) {
390
            $error = true;
391
            $statusCode = 404;
392
            $title = 'Error';
393
            $type = 'error';
394
            $message = 'Error';
395
            $result = 'Not Found';
396
        } finally {
397
            return Response::json(array(
398
              'error' => $error,
399
              'status' => $statusCode,
400
              'title' => $title,
401
              'type' => $type,
402
              'message' => $message,
403
              'result' => $result
404
            ));
405
        }
406
  }
407
408
  /**
409
   * Remove the specified resource from storage.
410
   *
411
   * @param  int  $id
412
   * @return Response
413
   */
414
  public function destroy($id)
415
  {
416
      try {
417
          EpormasCounter::find($id)->delete();
418
          $error = false;
419
          $statusCode = 200;
420
          $title = 'Success';
421
          $type = 'success';
422
          $message = 'Data deleted successfully';
423
      } catch (Exception $e) {
424
          $error = true;
425
          $statusCode = 404;
426
          $title = 'Error';
427
          $type = 'error';
428
          $message = 'Error';
429
      } finally {
430
          return Response::json(array(
431
            'error' => $error,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $error does not seem to be defined for all execution paths leading up to this point.
Loading history...
432
            'status' => $statusCode,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $statusCode does not seem to be defined for all execution paths leading up to this point.
Loading history...
433
            'title' => $title,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
434
            'type' => $type,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.
Loading history...
435
            'message' => $message
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.
Loading history...
436
          ));
437
      }
438
  }
439
440
}
441
442
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
443