Test Failed
Push — master ( 452104...d67f8b )
by
unknown
02:25
created

EpormasCityController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 2
Ratio 100 %

Importance

Changes 0
Metric Value
dl 2
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 0
nc 1
nop 0
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\EpormasCity;
10
use Validator, Image, Session, File, Response, Redirect, Exception;
11
12 View Code Duplication
class EpormasCityController extends Controller
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
15
  /**
16
   * Display a listing of the resource.
17
   *
18
   * @return Response
19
   */
20
  public function index()
21
  {
22
      try {
23
          $error = false;
24
          $statusCode = 200;
25
          $title = 'Success';
26
          $type = 'success';
27
          $message = 'Success';
28
          $result = EpormasCity::all();
29
          return Response::json(array(
30
            'error' => $error,
31
            'status' => $statusCode,
32
            'title' => $title,
33
            'type' => $type,
34
            'message' => $message,
35
            'result' => $result
36
          ));
37
      } catch (Exception $e) {
38
          $error = true;
39
          $statusCode = 404;
40
          $title = 'Error';
41
          $type = 'error';
42
          $message = 'Error';
43
          $result = 'Not Found';
44
          return Response::json(array(
45
            'error' => $error,
46
            'status' => $statusCode,
47
            'title' => $title,
48
            'type' => $type,
49
            'message' => $message,
50
            'result' => $result
51
          ));
52
      }
53
  }
54
55
  /**
56
   * Show the form for creating a new resource.
57
   *
58
   * @return Response
59
   */
60
  public function create()
61
  {
62
63
  }
64
65
  /**
66
   * Store a newly created resource in storage.
67
   *
68
   * @return Response
69
   */
70
  public function store(Request $request, $version='')
71
  {
72
        $path = \Request::path();
73
        $explode = explode('/', $path);
74
75
        $from = 'form';
76
        if(in_array('api',$explode)){
77
          $from = 'api';
78
        }
79
80
        $via = $from;
81
        if($version != '' && $version != 'store'){
82
          $via .= '-'.$version;
83
        }
84
85
      	$rules = [
86
            'name' => 'required'
87
        ];
88
89
        $validator = Validator::make($request->all(), $rules);
90
        if ($validator->fails()) {
91
          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

91
          return /** @scrutinizer ignore-call */ response()->json([
Loading history...
92
              'title' => 'Error',
93
              'type'  => 'error',
94
              'message' => $validator->errors()->all()
95
          ]);
96
        }
97
98
        $data = EpormasCity::whereNull('deleted_at')
99
                        ->where('name', $request->name)
100
                        ->count();
101
        if($data > 0){
102
          return response()->json([
103
              'title' => 'Error',
104
              'type'  => 'error',
105
              'message' => 'Data has already been taken.'
106
          ]);
107
        }
108
109
        try {
110
            $error = false;
111
            $statusCode = 200;
112
            $title = 'Success';
113
            $type = 'success';
114
            $message = 'Data created successfully';
115
            $result = EpormasCity::create([
116
                'name' => $request->name
117
            ]);
118
            return Response::json(array(
119
              'error' => $error,
120
              'status' => $statusCode,
121
              'title' => $title,
122
              'type' => $type,
123
              'message' => $message,
124
              'result' => $result
125
            ));
126
        } catch (Exception $e) {
127
            $error = true;
128
            $statusCode = 404;
129
            $title = 'Error';
130
            $type = 'error';
131
            $message = 'Error';
132
            $result = 'Not Found';
133
            return Response::json(array(
134
              'error' => $error,
135
              'status' => $statusCode,
136
              'title' => $title,
137
              'type' => $type,
138
              'message' => $message,
139
              'result' => $result
140
            ));
141
        }
142
  }
143
144
  /**
145
   * Display the specified resource.
146
   *
147
   * @param  int  $id
148
   * @return Response
149
   */
150
  public function show($version='', $id)
151
  {
152
        try {
153
            $error = false;
154
            $statusCode = 200;
155
            $title = 'Success';
156
            $type = 'success';
157
            $message = 'Success';
158
            $result = EpormasCity::findOrFail($id);
159
            return Response::json(array(
160
              'error' => $error,
161
              'status' => $statusCode,
162
              'title' => $title,
163
              'type' => $type,
164
              'message' => $message,
165
              'result' => $result
166
            ));
167
        } catch (Exception $e) {
168
            $error = true;
169
            $statusCode = 404;
170
            $title = 'Error';
171
            $type = 'error';
172
            $message = 'Error';
173
            $result = 'Not Found';
174
            return Response::json(array(
175
              'error' => $error,
176
              'status' => $statusCode,
177
              'title' => $title,
178
              'type' => $type,
179
              'message' => $message,
180
              'result' => $result
181
            ));
182
        }
183
  }
184
185
  /**
186
   * Show the form for editing the specified resource.
187
   *
188
   * @param  int  $id
189
   * @return Response
190
   */
191
  public function edit($id)
192
  {
193
        try {
194
            $error = false;
195
            $statusCode = 200;
196
            $title = 'Success';
197
            $type = 'success';
198
            $message = 'Success';
199
            $result = EpormasCity::findOrFail($id);
200
            return Response::json(array(
201
              'error' => $error,
202
              'status' => $statusCode,
203
              'title' => $title,
204
              'type' => $type,
205
              'message' => $message,
206
              'result' => $result
207
            ));
208
        } catch (Exception $e) {
209
            $error = true;
210
            $statusCode = 404;
211
            $title = 'Error';
212
            $type = 'error';
213
            $message = 'Error';
214
            $result = 'Not Found';
215
            return Response::json(array(
216
              'error' => $error,
217
              'status' => $statusCode,
218
              'title' => $title,
219
              'type' => $type,
220
              'message' => $message,
221
              'result' => $result
222
            ));
223
        }
224
  }
225
226
  /**
227
   * Update the specified resource in storage.
228
   *
229
   * @param  int  $id
230
   * @return Response
231
   */
232
  public function update(Request $request, $version='', $id)
233
  {
234
        $result = EpormasCity::find($id);
235
236
        $rules = [
237
            'name' => 'required'
238
        ];
239
240
        $validator = Validator::make($request->all(), $rules);
241
        if ($validator->fails()) {
242
          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

242
          return /** @scrutinizer ignore-call */ response()->json([
Loading history...
243
              'title' => 'Error',
244
              'type'  => 'error',
245
              'message' => $validator->errors()->all()
246
          ]);
247
        }
248
249
        if($request->name != $result->name){
250
          $data = EpormasCity::whereNull('deleted_at')
251
                          ->where('name', $request->name)
252
                          ->count();
253
          if($data > 0){
254
            return response()->json([
255
                'title' => 'Error',
256
                'type'  => 'error',
257
                'message' => 'Data has already been taken.'
258
            ]);
259
          }
260
        }
261
262
        $path = \Request::path();
263
        $explode = explode('/', $path);
264
265
        $from = 'form';
266
        if(in_array('api',$explode)){
267
          $from = 'api';
268
        }
269
270
        $via = $from;
271
        if($version != '' && $version != 'update'){
272
          $via .= '-'.$version;
273
        }
274
275
        try {
276
            $error = false;
277
            $statusCode = 200;
278
            $title = 'Success';
279
            $type = 'success';
280
            $message = 'Data updated successfully';
281
            $result->update([
282
                'name' => $request->name
283
            ]);
284
            return Response::json(array(
285
              'error' => $error,
286
              'status' => $statusCode,
287
              'title' => $title,
288
              'type' => $type,
289
              'message' => $message,
290
              'result' => $result
291
            ));
292
        } catch (Exception $e) {
293
            $error = true;
294
            $statusCode = 404;
295
            $title = 'Error';
296
            $type = 'error';
297
            $message = 'Error';
298
            $result = 'Not Found';
299
            return Response::json(array(
300
              'error' => $error,
301
              'status' => $statusCode,
302
              'title' => $title,
303
              'type' => $type,
304
              'message' => $message,
305
              'result' => $result
306
            ));
307
        }
308
  }
309
310
  /**
311
   * Remove the specified resource from storage.
312
   *
313
   * @param  int  $id
314
   * @return Response
315
   */
316
  public function destroy($id)
317
  {
318
      try {
319
          EpormasCity::find($id)->delete();
320
          $error = false;
321
          $statusCode = 200;
322
          $title = 'Success';
323
          $type = 'success';
324
          $message = 'Data deleted successfully';
325
          return Response::json(array(
326
            'error' => $error,
327
            'status' => $statusCode,
328
            'title' => $title,
329
            'type' => $type,
330
            'message' => $message
331
          ));
332
      } catch (Exception $e) {
333
          $error = true;
334
          $statusCode = 404;
335
          $title = 'Error';
336
          $type = 'error';
337
          $message = 'Error';
338
          return Response::json(array(
339
            'error' => $error,
340
            'status' => $statusCode,
341
            'title' => $title,
342
            'type' => $type,
343
            'message' => $message
344
          ));
345
      }
346
  }
347
348
}
349
350
?>
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...
351