Test Failed
Push — master ( 09f3c0...452104 )
by
unknown
02:39
created

EpormasCounterController::store()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 102
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 5.2676
c 0
b 0
f 0
cc 8
eloc 82
nc 24
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
          return Response::json(array(
35
            'error' => $error,
36
            'status' => $statusCode,
37
            'title' => $title,
38
            'type' => $type,
39
            'message' => $message,
40
            'result' => $result
41
          ));
42
      } catch (Exception $e) {
43
          $error = true;
44
          $statusCode = 404;
45
          $title = 'Error';
46
          $type = 'error';
47
          $message = 'Error';
48
          $result = 'Not Found';
49
          return Response::json(array(
50
            'error' => $error,
51
            'status' => $statusCode,
52
            'title' => $title,
53
            'type' => $type,
54
            'message' => $message,
55
            'result' => $result
56
          ));
57
      }
58
  }
59
60
  /**
61
   * Show the form for creating a new resource.
62
   *
63
   * @return Response
64
   */
65
  public function create()
66
  {
67
      try {
68
          $error = false;
69
          $statusCode = 200;
70
          $title = 'Success';
71
          $type = 'success';
72
          $message = 'Success';
73
          $city = EpormasCity::all();
74
          $category = EpormasCategory::all();
75
          return Response::json(array(
76
            'error' => $error,
77
            'status' => $statusCode,
78
            'title' => $title,
79
            'type' => $type,
80
            'message' => $message,
81
            'city' => $city,
82
            'category' => $category
83
          ));
84
      } catch (Exception $e) {
85
          $error = true;
86
          $statusCode = 404;
87
          $title = 'Error';
88
          $type = 'error';
89
          $message = 'Error';
90
          $city = 'Not Found';
91
          $category = 'Not Found';
92
          return Response::json(array(
93
            'error' => $error,
94
            'status' => $statusCode,
95
            'title' => $title,
96
            'type' => $type,
97
            'message' => $message,
98
            'city' => $city,
99
            'category' => $category
100
          ));
101
      }
102
  }
103
104
  /**
105
   * Store a newly created resource in storage.
106
   *
107
   * @return Response
108
   */
109
  public function store(Request $request, $version='')
110
  {
111
        $path = \Request::path();
112
        $explode = explode('/', $path);
113
114
        $from = 'form';
115
        if(in_array('api',$explode)){
116
          $from = 'api';
117
        }
118
119
        $via = $from;
120
        if($version != '' && $version != 'store'){
121
          $via .= '-'.$version;
122
        }
123
124
      	$rules = [
125
            'count' => 'required|numeric',
126
            'city_id' => 'required|numeric',
127
            'category_id' => 'required|numeric',
128
            'tanggal' => 'required|date',
129
        ];
130
131
        $validator = Validator::make($request->all(), $rules);
132
        if ($validator->fails()) {
133
          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

133
          return /** @scrutinizer ignore-call */ response()->json([
Loading history...
134
              'title' => 'Error',
135
              'type'  => 'error',
136
              'message' => $validator->errors()->all()
137
          ]);
138
        }
139
140
        $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

140
        $format = date('Y-m-d', /** @scrutinizer ignore-type */ strtotime(str_replace(' ','-',$request->tanggal)));
Loading history...
141
        $resultcek = EpormasCounter::whereNull('deleted_at')
142
                             ->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

142
                             ->where('tanggal','like','%'./** @scrutinizer ignore-type */ $format.'%')
Loading history...
143
                             ->where('category_id',$request->category_id)
144
                             ->where('city_id',$request->city_id)
145
                             ->groupBy('tahun','bulan','category_id','city_id')
146
                             ->orderBy('bulan')
147
                             ->count();
148
        if($resultcek > 0){
149
          return response()->json([
150
              'title' => 'Error',
151
              'type'  => 'error',
152
              'message' => 'Data has already been taken.'
153
          ]);
154
        }
155
156
        $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

156
        $date = explode("-",/** @scrutinizer ignore-type */ $format);
Loading history...
157
        $data = EpormasCounter::whereNull('deleted_at')
158
                       ->where('tahun', $date[0])
159
                       ->where('bulan', $date[1])
160
                       ->where('category_id',$request->category_id)
161
                       ->where('city_id',$request->city_id)
162
                       ->groupBy('tahun','bulan','category_id','city_id')
163
                       ->orderBy('bulan')
164
                       ->count();
165
        if($data > 0){
166
          return response()->json([
167
              'title' => 'Error',
168
              'type'  => 'error',
169
              'message' => 'Data has already been taken.'
170
          ]);
171
        }
172
173
        try {
174
            $error = false;
175
            $statusCode = 200;
176
            $title = 'Success';
177
            $type = 'success';
178
            $message = 'Data created successfully';
179
            $result = EpormasCounter::create([
180
                'count' => $request->count,
181
                'category_id' => $request->category_id,
182
                'city_id' => $request->city_id,
183
                'tahun' => $date[0],
184
                'bulan' => $date[1],
185
                'tanggal' => $format,
186
                'user_id' => $request->user_id,
187
                'via' => $via
188
            ]);
189
            return Response::json(array(
190
              'error' => $error,
191
              'status' => $statusCode,
192
              'title' => $title,
193
              'type' => $type,
194
              'message' => $message,
195
              'result' => $result
196
            ));
197
        } catch (Exception $e) {
198
            $error = true;
199
            $statusCode = 404;
200
            $title = 'Error';
201
            $type = 'error';
202
            $message = 'Error';
203
            $result = 'Not Found';
204
            return Response::json(array(
205
              'error' => $error,
206
              'status' => $statusCode,
207
              'title' => $title,
208
              'type' => $type,
209
              'message' => $message,
210
              'result' => $result
211
            ));
212
        }
213
  }
214
215
  /**
216
   * Display the specified resource.
217
   *
218
   * @param  int  $id
219
   * @return Response
220
   */
221 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...
222
  {
223
        try {
224
            $error = false;
225
            $statusCode = 200;
226
            $title = 'Success';
227
            $type = 'success';
228
            $message = 'Success';
229
            $result = EpormasCounter::whereNull('deleted_at')
230
                            ->with('getCity')
231
                            ->with('getCategory')
232
                            ->find($id);
233
            return Response::json(array(
234
              'error' => $error,
235
              'status' => $statusCode,
236
              'title' => $title,
237
              'type' => $type,
238
              'message' => $message,
239
              'result' => $result
240
            ));
241
        } catch (Exception $e) {
242
            $error = true;
243
            $statusCode = 404;
244
            $title = 'Error';
245
            $type = 'error';
246
            $message = 'Error';
247
            $result = 'Not Found';
248
            return Response::json(array(
249
              'error' => $error,
250
              'status' => $statusCode,
251
              'title' => $title,
252
              'type' => $type,
253
              'message' => $message,
254
              'result' => $result
255
            ));
256
        }
257
  }
258
259
  /**
260
   * Show the form for editing the specified resource.
261
   *
262
   * @param  int  $id
263
   * @return Response
264
   */
265
  public function edit($id)
266
  {
267
        try {
268
            $error = false;
269
            $statusCode = 200;
270
            $title = 'Success';
271
            $type = 'success';
272
            $message = 'Success';
273
            $city = EpormasCity::all();
274
            $category = EpormasCategory::all();
275
            $result = EpormasCounter::whereNull('deleted_at')
276
                            ->with('getCity')
277
                            ->with('getCategory')
278
                            ->find($id);
279
            $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

279
            $format = date('Y-m-d', /** @scrutinizer ignore-type */ strtotime($result->tanggal));
Loading history...
280
            return Response::json(array(
281
              'error' => $error,
282
              'status' => $statusCode,
283
              'title' => $title,
284
              'type' => $type,
285
              'message' => $message,
286
              'result' => $result,
287
              'city' => $city,
288
              'tanggal' => $format,
289
              'category' => $category
290
            ));
291
        } catch (Exception $e) {
292
            $error = true;
293
            $statusCode = 404;
294
            $title = 'Error';
295
            $type = 'error';
296
            $message = 'Error';
297
            $result = 'Not Found';
298
            $city = 'Not Found';
299
            $format = 'Not Found';
300
            $category = 'Not Found';
301
            return Response::json(array(
302
              'error' => $error,
303
              'status' => $statusCode,
304
              'title' => $title,
305
              'type' => $type,
306
              'message' => $message,
307
              'result' => $result,
308
              'city' => $city,
309
              'tanggal' => $format,
310
              'category' => $category
311
            ));
312
        }
313
  }
314
315
  /**
316
   * Update the specified resource in storage.
317
   *
318
   * @param  int  $id
319
   * @return Response
320
   */
321
  public function update(Request $request, $version='', $id)
322
  {
323
        $result = EpormasCounter::whereNull('deleted_at')
324
                        ->with('getCity')
325
                        ->with('getCategory')
326
                        ->find($id);
327
328
        $rules = [
329
            'count' => 'required|numeric',
330
            'city_id' => 'required|numeric',
331
            'category_id' => 'required|numeric',
332
            'tanggal' => 'required|date',
333
        ];
334
335
        $validator = Validator::make($request->all(), $rules);
336
        if ($validator->fails()) {
337
          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

337
          return /** @scrutinizer ignore-call */ response()->json([
Loading history...
338
              'title' => 'Error',
339
              'type'  => 'error',
340
              'message' => $validator->errors()->all()
341
          ]);
342
        }
343
344
        $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

344
        $format = date('Y-m-d', /** @scrutinizer ignore-type */ strtotime(str_replace(' ','-',$request->tanggal)));
Loading history...
345
        if($result->city_id != $request->city_id || $result->category_id != $request->category_id){
346
            $resultcek = EpormasCounter::whereNull('deleted_at')
347
                                 ->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

347
                                 ->where('tanggal','like','%'./** @scrutinizer ignore-type */ $format.'%')
Loading history...
348
            		                 ->where('category_id',$request->category_id)
349
            		                 ->where('city_id',$request->city_id)
350
                                 ->groupBy('tahun','bulan','category_id','city_id')
351
                                 ->orderBy('bulan')
352
                                 ->count();
353
            if($resultcek > 0){
354
              return response()->json([
355
                  'title' => 'Error',
356
                  'type'  => 'error',
357
                  'message' => 'Data has already been taken.'
358
              ]);
359
            }
360
        }
361
362
        $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

362
        $date = explode("-",/** @scrutinizer ignore-type */ $format);
Loading history...
363
        $dates = date('Y-m-d', strtotime($result->tanggal));
364
        if($dates != $format){
365
            $resultcek = EpormasCounter::whereNull('deleted_at')
366
            		                 ->where('category_id',$request->category_id)
367
            		                 ->where('city_id',$request->city_id)
368
                                 ->where('tanggal','like','%'.$format.'%')
369
                                 ->groupBy('tahun','bulan','category_id','city_id')
370
                                 ->orderBy('bulan')
371
                                 ->count();
372
            if($resultcek > 0){
373
              return response()->json([
374
                  'title' => 'Error',
375
                  'type'  => 'error',
376
                  'message' => 'Data has already been taken.'
377
              ]);
378
            }
379
380
            $data = EpormasCounter::whereNull('deleted_at')
381
                           ->where('tahun', $date[0])
382
                           ->where('bulan', $date[1])
383
            		           ->where('category_id',$request->category_id)
384
            		           ->where('city_id',$request->city_id)
385
                           ->groupBy('tahun','bulan','category_id','city_id')
386
                           ->orderBy('bulan')
387
                           ->count();
388
            if($data > 0){
389
              return response()->json([
390
                  'title' => 'Error',
391
                  'type'  => 'error',
392
                  'message' => 'Data has already been taken.'
393
              ]);
394
            }
395
        }
396
397
        $path = \Request::path();
398
        $explode = explode('/', $path);
399
400
        $from = $result->via;
401
        $user_id = $result->user_id;
402
        if(in_array('api',$explode)){
403
          $from = 'api';
404
          $user_id = $request->user_id;
405
        }
406
407
        $via = $from;
408
        if($version != '' && $version != 'update'){
409
          $via .= '-'.$version;
410
        }
411
412
        try {
413
            $error = false;
414
            $statusCode = 200;
415
            $title = 'Success';
416
            $type = 'success';
417
            $message = 'Data updated successfully';
418
            $result->update([
419
                'count' => $request->count,
420
                'category_id' => $request->category_id,
421
                'city_id' => $request->city_id,
422
                'tahun' => $date[0],
423
                'bulan' => $date[1],
424
                'tanggal' => $format,
425
                'user_id' => $user_id,
426
                'via' => $via
427
            ]);
428
            return Response::json(array(
429
              'error' => $error,
430
              'status' => $statusCode,
431
              'title' => $title,
432
              'type' => $type,
433
              'message' => $message,
434
              'result' => $result
435
            ));
436
        } catch (Exception $e) {
437
            $error = true;
438
            $statusCode = 404;
439
            $title = 'Error';
440
            $type = 'error';
441
            $message = 'Error';
442
            $result = 'Not Found';
443
            return Response::json(array(
444
              'error' => $error,
445
              'status' => $statusCode,
446
              'title' => $title,
447
              'type' => $type,
448
              'message' => $message,
449
              'result' => $result
450
            ));
451
        }
452
  }
453
454
  /**
455
   * Remove the specified resource from storage.
456
   *
457
   * @param  int  $id
458
   * @return Response
459
   */
460
  public function destroy($id)
461
  {
462
      try {
463
          EpormasCounter::find($id)->delete();
464
          $error = false;
465
          $statusCode = 200;
466
          $title = 'Success';
467
          $type = 'success';
468
          $message = 'Data deleted successfully';
469
          return Response::json(array(
470
            'error' => $error,
471
            'status' => $statusCode,
472
            'title' => $title,
473
            'type' => $type,
474
            'message' => $message
475
          ));
476
      } catch (Exception $e) {
477
          $error = true;
478
          $statusCode = 404;
479
          $title = 'Error';
480
          $type = 'error';
481
          $message = 'Error';
482
          return Response::json(array(
483
            'error' => $error,
484
            'status' => $statusCode,
485
            'title' => $title,
486
            'type' => $type,
487
            'message' => $message
488
          ));
489
      }
490
  }
491
492
}
493
494
?>
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...
495