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

EpormasCategoryController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
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\EpormasCategory;
10
use Validator, Image, Session, File, Response, Redirect, Exception;
11
12
class EpormasCategoryController 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 = EpormasCategory::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 = EpormasCategory::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 = EpormasCategory::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 = EpormasCategory::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 = EpormasCategory::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 = EpormasCategory::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
              'type'  => 'error',
206
              'message' => $validator->errors()->all()
207
          ));
208
        }
209
210
        if($request->name != $result->name){
211
          $data = EpormasCategory::whereNull('deleted_at')
212
                          ->where('name', $request->name)
213
                          ->count();
214
          if($data > 0){
215
            return Response::json(array(
216
                'title' => 'Error',
217
                'type'  => 'error',
218
                'message' => 'Data has already been taken.'
219
            ));
220
          }
221
        }
222
223
        $path = \Request::path();
224
        $explode = explode('/', $path);
225
226
        $from = 'form';
227
        if(in_array('api',$explode)){
228
          $from = 'api';
229
        }
230
231
        $via = $from;
232
        if($version != '' && $version != 'update'){
233
          $via .= '-'.$version;
234
        }
235
236
        $error = false;
237
        $statusCode = 200;
238
        $title = 'Success';
239
        $type = 'success';
240
        $message = 'Data updated successfully';
241
        $result->update([
242
            'name' => $request->name
243
        ]);
244
        return Response::json(array(
245
          'error' => $error,
246
          'status' => $statusCode,
247
          'title' => $title,
248
          'type' => $type,
249
          'message' => $message,
250
          'result' => $result
251
        ));
252
  }
253
254
  /**
255
   * Remove the specified resource from storage.
256
   *
257
   * @param  int  $id
258
   * @return Response
259
   */
260 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...
261
  {
262
      EpormasCategory::find($id)->delete();
263
      $error = false;
264
      $statusCode = 200;
265
      $title = 'Success';
266
      $type = 'success';
267
      $message = 'Data deleted successfully';
268
      return Response::json(array(
269
        'error' => $error,
270
        'status' => $statusCode,
271
        'title' => $title,
272
        'type' => $type,
273
        'message' => $message
274
      ));
275
  }
276
277
}
278
279
?>
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...
280