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

EpormasCityController::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\EpormasCity;
10
use Validator, Image, Session, File, Response, Redirect, Exception;
11
12
class EpormasCityController extends Controller
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(array(
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(array(
96
              'title' => 'Error',
97
              'type'  => 'error',
98
              'message' => 'Data has already been taken.'
99
          ));
100
        }
101
102
        $error = false;
103
        $statusCode = 200;
104
        $title = 'Success';
105
        $type = 'success';
106
        $message = 'Data created successfully';
107
        $result = EpormasCity::create([
108
            'name' => $request->name
109
        ]);
110
        return Response::json(array(
111
          'error' => $error,
112
          'status' => $statusCode,
113
          'title' => $title,
114
          'type' => $type,
115
          'message' => $message,
116
          'result' => $result
117
        ));
118
  }
119
120
  /**
121
   * Display the specified resource.
122
   *
123
   * @param  int  $id
124
   * @return Response
125
   */
126
  public function show($version='', $id)
127
  {
128
        try {
129
            $error = false;
130
            $statusCode = 200;
131
            $title = 'Success';
132
            $type = 'success';
133
            $message = 'Success';
134
            $result = EpormasCity::findOrFail($id);
135
        } catch (Exception $e) {
136
            $error = true;
137
            $statusCode = 404;
138
            $title = 'Error';
139
            $type = 'error';
140
            $message = 'Error';
141
            $result = 'Not Found';
142
        } finally {
143
            return Response::json(array(
144
              'error' => $error,
145
              'status' => $statusCode,
146
              'title' => $title,
147
              'type' => $type,
148
              'message' => $message,
149
              'result' => $result
150
            ));
151
        }
152
  }
153
154
  /**
155
   * Show the form for editing the specified resource.
156
   *
157
   * @param  int  $id
158
   * @return Response
159
   */
160
  public function edit($id)
161
  {
162
        try {
163
            $error = false;
164
            $statusCode = 200;
165
            $title = 'Success';
166
            $type = 'success';
167
            $message = 'Success';
168
            $result = EpormasCity::findOrFail($id);
169
        } catch (Exception $e) {
170
            $error = true;
171
            $statusCode = 404;
172
            $title = 'Error';
173
            $type = 'error';
174
            $message = 'Error';
175
            $result = 'Not Found';
176
        } finally {
177
            return Response::json(array(
178
              'error' => $error,
179
              'status' => $statusCode,
180
              'title' => $title,
181
              'type' => $type,
182
              'message' => $message,
183
              'result' => $result
184
            ));
185
        }
186
  }
187
188
  /**
189
   * Update the specified resource in storage.
190
   *
191
   * @param  int  $id
192
   * @return Response
193
   */
194
  public function update(Request $request, $version='', $id)
195
  {
196
        $result = EpormasCity::find($id);
197
198
        $rules = [
199
            'name' => 'required'
200
        ];
201
202
        $validator = Validator::make($request->all(), $rules);
203
        if ($validator->fails()) {
204
          return Response::json(array(
205
              'title' => 'Error',
206
              'type'  => 'error',
207
              'message' => $validator->errors()->all()
208
          ));
209
        }
210
211
        if($request->name != $result->name){
212
          $data = EpormasCity::whereNull('deleted_at')
213
                          ->where('name', $request->name)
214
                          ->count();
215
          if($data > 0){
216
            return Response::json(array(
217
                'title' => 'Error',
218
                'type'  => 'error',
219
                'message' => 'Data has already been taken.'
220
            ));
221
          }
222
        }
223
224
        $path = \Request::path();
225
        $explode = explode('/', $path);
226
227
        $from = 'form';
228
        if(in_array('api',$explode)){
229
          $from = 'api';
230
        }
231
232
        $via = $from;
233
        if($version != '' && $version != 'update'){
234
          $via .= '-'.$version;
235
        }
236
237
        $error = false;
238
        $statusCode = 200;
239
        $title = 'Success';
240
        $type = 'success';
241
        $message = 'Data updated successfully';
242
        $result->update([
243
            'name' => $request->name
244
        ]);
245
        return Response::json(array(
246
          'error' => $error,
247
          'status' => $statusCode,
248
          'title' => $title,
249
          'type' => $type,
250
          'message' => $message,
251
          'result' => $result
252
        ));
253
  }
254
255
  /**
256
   * Remove the specified resource from storage.
257
   *
258
   * @param  int  $id
259
   * @return Response
260
   */
261 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...
262
  {
263
      EpormasCity::find($id)->delete();
264
      $error = false;
265
      $statusCode = 200;
266
      $title = 'Success';
267
      $type = 'success';
268
      $message = 'Data deleted successfully';
269
      return Response::json(array(
270
        'error' => $error,
271
        'status' => $statusCode,
272
        'title' => $title,
273
        'type' => $type,
274
        'message' => $message
275
      ));
276
  }
277
278
}
279
280
?>
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...
281