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

EpormasCityController::edit()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 24
Code Lines 22

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 22
nc 3
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\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
      } catch (Exception $e) {
30
          $error = true;
31
          $statusCode = 404;
32
          $title = 'Error';
33
          $type = 'error';
34
          $message = 'Error';
35
          $result = 'Not Found';
36
      } finally {
37
          return Response::json(array(
38
            'error' => $error,
39
            'status' => $statusCode,
40
            'title' => $title,
41
            'type' => $type,
42
            'message' => $message,
43
            'result' => $result
44
          ));
45
      }
46
  }
47
48
  /**
49
   * Show the form for creating a new resource.
50
   *
51
   * @return Response
52
   */
53
  public function create()
54
  {
55
56
  }
57
58
  /**
59
   * Store a newly created resource in storage.
60
   *
61
   * @return Response
62
   */
63
  public function store(Request $request, $version='')
64
  {
65
        $path = \Request::path();
66
        $explode = explode('/', $path);
67
68
        $from = 'form';
69
        if(in_array('api',$explode)){
70
          $from = 'api';
71
        }
72
73
        $via = $from;
74
        if($version != '' && $version != 'store'){
75
          $via .= '-'.$version;
76
        }
77
78
      	$rules = [
79
            'name' => 'required'
80
        ];
81
82
        $validator = Validator::make($request->all(), $rules);
83
        if ($validator->fails()) {
84
          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

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

214
          return /** @scrutinizer ignore-call */ response()->json([
Loading history...
215
              'title' => 'Error',
216
              'type'  => 'error',
217
              'message' => $validator->errors()->all()
218
          ]);
219
        }
220
221
        if($request->name != $result->name){
222
          $data = EpormasCity::whereNull('deleted_at')
223
                          ->where('name', $request->name)
224
                          ->count();
225
          if($data > 0){
226
            return response()->json([
227
                'title' => 'Error',
228
                'type'  => 'error',
229
                'message' => 'Data has already been taken.'
230
            ]);
231
          }
232
        }
233
234
        $path = \Request::path();
235
        $explode = explode('/', $path);
236
237
        $from = 'form';
238
        if(in_array('api',$explode)){
239
          $from = 'api';
240
        }
241
242
        $via = $from;
243
        if($version != '' && $version != 'update'){
244
          $via .= '-'.$version;
245
        }
246
247
        try {
248
            $error = false;
249
            $statusCode = 200;
250
            $title = 'Success';
251
            $type = 'success';
252
            $message = 'Data updated successfully';
253
            $result->update([
254
                'name' => $request->name
255
            ]);
256
        } catch (Exception $e) {
257
            $error = true;
258
            $statusCode = 404;
259
            $title = 'Error';
260
            $type = 'error';
261
            $message = 'Error';
262
            $result = 'Not Found';
263
        } finally {
264
            return Response::json(array(
265
              'error' => $error,
266
              'status' => $statusCode,
267
              'title' => $title,
268
              'type' => $type,
269
              'message' => $message,
270
              'result' => $result
271
            ));
272
        }
273
  }
274
275
  /**
276
   * Remove the specified resource from storage.
277
   *
278
   * @param  int  $id
279
   * @return Response
280
   */
281
  public function destroy($id)
282
  {
283
      try {
284
          EpormasCity::find($id)->delete();
285
          $error = false;
286
          $statusCode = 200;
287
          $title = 'Success';
288
          $type = 'success';
289
          $message = 'Data deleted successfully';
290
      } catch (Exception $e) {
291
          $error = true;
292
          $statusCode = 404;
293
          $title = 'Error';
294
          $type = 'error';
295
          $message = 'Error';
296
      } finally {
297
          return Response::json(array(
298
            '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...
299
            '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...
300
            '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...
301
            '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...
302
            '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...
303
          ));
304
      }
305
  }
306
307
}
308
309
?>
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...
310