Test Failed
Push — master ( 775adc...b03845 )
by Esza
03:16
created

EpormasCounterController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
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(array(
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(array(
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(array(
152
              'title' => 'Error',
153
              'type'  => 'error',
154
              'message' => 'Data has already been taken.'
155
          ));
156
        }
157
158
        $error = false;
159
        $statusCode = 200;
160
        $title = 'Success';
161
        $type = 'success';
162
        $message = 'Data created successfully';
163
        $result = EpormasCounter::create([
164
            'count' => $request->count,
165
            'category_id' => $request->category_id,
166
            'city_id' => $request->city_id,
167
            'tahun' => $date[0],
168
            'bulan' => $date[1],
169
            'tanggal' => $format,
170
            'user_id' => $request->user_id,
171
            'via' => $via
172
        ]);
173
        return Response::json(array(
174
          'error' => $error,
175
          'status' => $statusCode,
176
          'title' => $title,
177
          'type' => $type,
178
          'message' => $message,
179
          'result' => $result
180
        ));
181
  }
182
183
  /**
184
   * Display the specified resource.
185
   *
186
   * @param  int  $id
187
   * @return Response
188
   */
189 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...
190
  {
191
        try {
192
            $error = false;
193
            $statusCode = 200;
194
            $title = 'Success';
195
            $type = 'success';
196
            $message = 'Success';
197
            $result = EpormasCounter::whereNull('deleted_at')
198
                            ->with('getCity')
199
                            ->with('getCategory')
200
                            ->find($id);
201
        } catch (Exception $e) {
202
            $error = true;
203
            $statusCode = 404;
204
            $title = 'Error';
205
            $type = 'error';
206
            $message = 'Error';
207
            $result = 'Not Found';
208
        } finally {
209
            return Response::json(array(
210
              'error' => $error,
211
              'status' => $statusCode,
212
              'title' => $title,
213
              'type' => $type,
214
              'message' => $message,
215
              'result' => $result
216
            ));
217
        }
218
  }
219
220
  /**
221
   * Show the form for editing the specified resource.
222
   *
223
   * @param  int  $id
224
   * @return Response
225
   */
226
  public function edit($id)
227
  {
228
        try {
229
            $error = false;
230
            $statusCode = 200;
231
            $title = 'Success';
232
            $type = 'success';
233
            $message = 'Success';
234
            $city = EpormasCity::all();
235
            $category = EpormasCategory::all();
236
            $result = EpormasCounter::whereNull('deleted_at')
237
                            ->with('getCity')
238
                            ->with('getCategory')
239
                            ->find($id);
240
            $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

240
            $format = date('Y-m-d', /** @scrutinizer ignore-type */ strtotime($result->tanggal));
Loading history...
241
        } catch (Exception $e) {
242
            $error = true;
243
            $statusCode = 404;
244
            $title = 'Error';
245
            $type = 'error';
246
            $message = 'Error';
247
            $result = 'Not Found';
248
            $city = 'Not Found';
249
            $format = 'Not Found';
250
            $category = 'Not Found';
251
        } finally {
252
            return Response::json(array(
253
              'error' => $error,
254
              'status' => $statusCode,
255
              'title' => $title,
256
              'type' => $type,
257
              'message' => $message,
258
              'result' => $result,
259
              'city' => $city,
260
              'tanggal' => $format,
261
              '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...
262
            ));
263
        }
264
  }
265
266
  /**
267
   * Update the specified resource in storage.
268
   *
269
   * @param  int  $id
270
   * @return Response
271
   */
272
  public function update(Request $request, $version='', $id)
273
  {
274
        $result = EpormasCounter::whereNull('deleted_at')
275
                        ->with('getCity')
276
                        ->with('getCategory')
277
                        ->find($id);
278
279
        $rules = [
280
            'count' => 'required|numeric',
281
            'city_id' => 'required|numeric',
282
            'category_id' => 'required|numeric',
283
            'tanggal' => 'required|date',
284
        ];
285
286
        $validator = Validator::make($request->all(), $rules);
287
        if ($validator->fails()) {
288
          return Response::json(array(
289
              'title' => 'Error',
290
              'type'  => 'error',
291
              'message' => $validator->errors()->all()
292
          ));
293
        }
294
295
        $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

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

298
                                 ->where('tanggal','like','%'./** @scrutinizer ignore-type */ $format.'%')
Loading history...
299
            		                 ->where('category_id',$request->category_id)
300
            		                 ->where('city_id',$request->city_id)
301
                                 ->groupBy('tahun','bulan','category_id','city_id')
302
                                 ->orderBy('bulan')
303
                                 ->count();
304
            if($resultcek > 0){
305
              return Response::json(array(
306
                  'title' => 'Error',
307
                  'type'  => 'error',
308
                  'message' => 'Data has already been taken.'
309
              ));
310
            }
311
        }
312
313
        $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

313
        $date = explode("-",/** @scrutinizer ignore-type */ $format);
Loading history...
314
        $dates = date('Y-m-d', strtotime($result->tanggal));
315
        if($dates != $format){
316
            $resultcek = EpormasCounter::whereNull('deleted_at')
317
            		                 ->where('category_id',$request->category_id)
318
            		                 ->where('city_id',$request->city_id)
319
                                 ->where('tanggal','like','%'.$format.'%')
320
                                 ->groupBy('tahun','bulan','category_id','city_id')
321
                                 ->orderBy('bulan')
322
                                 ->count();
323
            if($resultcek > 0){
324
              return Response::json(array(
325
                  'title' => 'Error',
326
                  'type'  => 'error',
327
                  'message' => 'Data has already been taken.'
328
              ));
329
            }
330
331
            $data = EpormasCounter::whereNull('deleted_at')
332
                           ->where('tahun', $date[0])
333
                           ->where('bulan', $date[1])
334
            		           ->where('category_id',$request->category_id)
335
            		           ->where('city_id',$request->city_id)
336
                           ->groupBy('tahun','bulan','category_id','city_id')
337
                           ->orderBy('bulan')
338
                           ->count();
339
            if($data > 0){
340
              return Response::json(array(
341
                  'title' => 'Error',
342
                  'type'  => 'error',
343
                  'message' => 'Data has already been taken.'
344
              ));
345
            }
346
        }
347
348
        $path = \Request::path();
349
        $explode = explode('/', $path);
350
351
        $from = $result->via;
352
        $user_id = $result->user_id;
353
        if(in_array('api',$explode)){
354
          $from = 'api';
355
          $user_id = $request->user_id;
356
        }
357
358
        $via = $from;
359
        if($version != '' && $version != 'update'){
360
          $via .= '-'.$version;
361
        }
362
363
        $error = false;
364
        $statusCode = 200;
365
        $title = 'Success';
366
        $type = 'success';
367
        $message = 'Data updated successfully';
368
        $result->update([
369
            'count' => $request->count,
370
            'category_id' => $request->category_id,
371
            'city_id' => $request->city_id,
372
            'tahun' => $date[0],
373
            'bulan' => $date[1],
374
            'tanggal' => $format,
375
            'user_id' => $user_id,
376
            'via' => $via
377
        ]);
378
        return Response::json(array(
379
          'error' => $error,
380
          'status' => $statusCode,
381
          'title' => $title,
382
          'type' => $type,
383
          'message' => $message,
384
          'result' => $result
385
        ));
386
  }
387
388
  /**
389
   * Remove the specified resource from storage.
390
   *
391
   * @param  int  $id
392
   * @return Response
393
   */
394 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...
395
  {
396
      EpormasCounter::find($id)->delete();
397
      $error = false;
398
      $statusCode = 200;
399
      $title = 'Success';
400
      $type = 'success';
401
      $message = 'Data deleted successfully';
402
      return Response::json(array(
403
        'error' => $error,
404
        'status' => $statusCode,
405
        'title' => $title,
406
        'type' => $type,
407
        'message' => $message
408
      ));
409
  }
410
411
}
412
413
?>
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...
414