Passed
Push — master ( da4f7e...b51252 )
by Mohammad
02:42
created

Controller::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Hamza Alayed
5
 * Date: 12/29/18
6
 * Time: 9:53 AM.
7
 */
8
9
namespace Shamaseen\Repository\Generator\Utility;
10
11
use Illuminate\Http\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\JsonResponse 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...
12
use Illuminate\Support\Collection;
13
14
/**
15
 * Class BaseController.
16
 */
17
class Controller extends \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...
18
{
19
    /**
20
     * @var ContractInterface
21
     */
22
    protected $interface;
23
24
    protected $limit = 10;
25
26
    protected $routeIndex = '';
27
28
    protected $pageTitle = '';
29
    protected $createRoute = '';
30
31
    protected $viewIndex = '';
32
    protected $viewCreate = '';
33
    protected $viewEdit = '';
34
    protected $viewShow = '';
35
    protected $breadcrumbs;
36
37
    protected $menu;
38
    protected $search;
39
    protected $selectedMenu = [];
40
    protected $isAPI = false;
41
    protected $trash = false;
42
    protected $params = [];
43
    /**
44
     * @var Request
45
     */
46
    private $request;
47
48
    /**
49
     * BaseController constructor.
50
     *
51
     * @param ContractInterface $interface
52
     * @param Request           $request
53
     */
54
    public function __construct(ContractInterface $interface, Request $request)
55
    {
56
        $this->menu = new Collection();
57
        $this->breadcrumbs = new Collection();
58
59
        $language = $request->header('Language', 'en');
60
        if (! in_array($language, \Config::get('app.locales', []))) {
61
            $language = 'en';
62
        }
63
        $limit = $request->get('limit', 10);
64
65
        if ((bool) $request->get('with-trash', false)) {
66
            $interface->withTrash();
67
        }
68
        if ((bool) $request->get('only-trash', false)) {
69
            $interface->trash();
70
        }
71
72
        $request->offsetUnset('only-trash');
73
        $request->offsetUnset('with-trash');
74
75
        if (in_array($limit, [20, 30, 40, 50])) {
76
            $this->limit = $limit;
77
        }
78
79
        \App::setLocale($language);
80
        $this->interface = $interface;
81
        $this->isAPI = $request->expectsJson();
82
83
        if (! $this->isAPI) {
84
            $this->breadcrumbs = new Collection();
85
            $this->search = new Collection();
86
            \View::share('pageTitle', $this->pageTitle.' | '.\Config::get('app.name'));
87
            \View::share('breadcrumbs', $this->breadcrumbs);
88
            \View::share('menu', $this->menu);
89
            \View::share('search', $this->search);
90
            \View::share('selectedMenu', $this->selectedMenu);
91
        }
92
        $this->request = $request;
93
    }
94
95
    /**
96
     * Display a listing of the resource.
97
     *
98
     *
99
     * @return \Illuminate\Http\Response
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Response 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...
100
     */
101
    public function index()
102
    {
103
        $data = $this->interface->simplePaginate($this->limit, $this->request->all());
104
        if (! $this->isAPI) {
105
            \View::share('pageTitle', 'List '.$this->pageTitle.' | '.\Config::get('app.name'));
106
            $this->breadcrumbs->put('index', [
107
                'link' => $this->routeIndex,
108
                'text' => $this->pageTitle,
109
            ]);
110
111
            return view($this->viewIndex, $this->params)
0 ignored issues
show
Bug introduced by
The function view 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

111
            return /** @scrutinizer ignore-call */ view($this->viewIndex, $this->params)
Loading history...
112
                ->with('entities', $data)
113
                ->with('createRoute', $this->createRoute)
114
                ->with('filters', $this->request->all());
115
        }
116
        if ($data->hasMorePages()) {
117
            return response()->json($data, JsonResponse::HTTP_PARTIAL_CONTENT);
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

117
            return /** @scrutinizer ignore-call */ response()->json($data, JsonResponse::HTTP_PARTIAL_CONTENT);
Loading history...
118
        }
119
        if (0 == $data->count()) {
120
            return response()->json($data, JsonResponse::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
121
        }
122
123
        return response()->json($data, JsonResponse::HTTP_OK);
124
    }
125
126
    /**
127
     * Show the form for creating a new resource.
128
     *
129
     * @return \Illuminate\Http\Response
130
     */
131
    public function create()
132
    {
133
        if (! $this->isAPI) {
134
            \View::share('pageTitle', 'Create '.$this->pageTitle.' | '.\Config::get('app.name'));
135
            $this->breadcrumbs->put('create', [
136
                'link' => $this->createRoute,
137
                'text' => trans('common/others.create'),
0 ignored issues
show
Bug introduced by
The function trans 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

137
                'text' => /** @scrutinizer ignore-call */ trans('common/others.create'),
Loading history...
138
            ]);
139
140
            return view($this->viewCreate, $this->params);
0 ignored issues
show
Bug introduced by
The function view 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

140
            return /** @scrutinizer ignore-call */ view($this->viewCreate, $this->params);
Loading history...
141
        }
142
143
        return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
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

143
        return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NO_CONTENT);
Loading history...
144
    }
145
146
    /**
147
     * Store a newly created resource in storage.
148
     *
149
     * @return \Illuminate\Http\RedirectResponse|mixed
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\RedirectResponse 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...
150
     */
151
    public function store()
152
    {
153
        $entity = $this->interface->create($this->request->except(['_token', '_method']));
154
        if (! $this->isAPI) {
155
            if ($entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
156
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
0 ignored issues
show
Bug introduced by
The function __ 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

156
                return \Redirect::to($this->routeIndex)->with('message', /** @scrutinizer ignore-call */ __('messages.success'));
Loading history...
157
            }
158
159
            return \Redirect::to($this->routeIndex)->with('error', __('messages.error'));
160
        }
161
162
        if ($entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
163
            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

163
            return /** @scrutinizer ignore-call */ response()->json(
Loading history...
164
                ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
165
                JsonResponse::HTTP_OK
166
            );
167
        }
168
169
        return response()->json(
170
            ['status' => false, 'message' => __('messages.error')],
171
            JsonResponse::HTTP_OK
172
        );
173
    }
174
175
    /**
176
     * Display the specified resource.
177
     *
178
     * @param int $entityId
179
     *
180
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
181
     */
182
    public function show($entityId)
183
    {
184
        $entity = $this->interface->find($entityId);
185
        if (! $this->isAPI) {
186
            if (! $entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
187
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
0 ignored issues
show
Bug introduced by
The function __ 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

187
                return \Redirect::to($this->routeIndex)->with('warning', /** @scrutinizer ignore-call */ __('messages.not_found'));
Loading history...
188
            }
189
            \View::share('pageTitle', 'View '.$this->pageTitle.' | '.\Config::get('app.name'));
190
            $this->breadcrumbs->put('view', [
191
                'link' => '',
192
                'text' => $entity->name ?? $entity->title ?? __('messages.view'),
193
            ]);
194
195
            return view($this->viewShow, $this->params)
0 ignored issues
show
Bug introduced by
The function view 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

195
            return /** @scrutinizer ignore-call */ view($this->viewShow, $this->params)
Loading history...
196
                ->with('entity', $entity);
197
        }
198
        if (! $entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
199
            return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
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

199
            return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NOT_FOUND);
Loading history...
200
        }
201
202
        return response()->json(
203
            ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
204
            JsonResponse::HTTP_OK
205
        );
206
    }
207
208
    /**
209
     * Show the form for editing the specified resource.
210
     *
211
     * @param int $entityId
212
     *
213
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
214
     */
215
    public function edit($entityId)
216
    {
217
        $entity = $this->interface->find($entityId);
218
        if (! $this->isAPI) {
219
            if (! $entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
220
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
0 ignored issues
show
Bug introduced by
The function __ 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

220
                return \Redirect::to($this->routeIndex)->with('warning', /** @scrutinizer ignore-call */ __('messages.not_found'));
Loading history...
221
            }
222
            $this->breadcrumbs->put('edit', [
223
                'link' => '',
224
                'text' => $entity->name ?? $entity->title ?? __('messages.view'),
225
            ]);
226
227
            return view($this->viewEdit, $this->params)
0 ignored issues
show
Bug introduced by
The function view 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

227
            return /** @scrutinizer ignore-call */ view($this->viewEdit, $this->params)
Loading history...
228
                ->with('entity', $entity);
229
        }
230
231
        return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
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

231
        return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NOT_FOUND);
Loading history...
232
    }
233
234
    /**
235
     * Update the specified resource in storage.
236
     *
237
     * @param int $entityId
238
     *
239
     * @return \Illuminate\Http\RedirectResponse
240
     */
241
    public function update($entityId)
242
    {
243
        //Todo Should we do find or fail here ?
244
        $entity = $this->interface->find($entityId);
245
        $saved = false;
246
247
        if ($entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
248
            $saved = $this->interface->update($entityId, $this->request->except(['_token', '_method']));
249
        }
250
251
        if (! $this->isAPI) {
252
            if (! $entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
253
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
0 ignored issues
show
Bug introduced by
The function __ 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

253
                return \Redirect::to($this->routeIndex)->with('warning', /** @scrutinizer ignore-call */ __('messages.not_found'));
Loading history...
254
            }
255
256
            if ($saved) {
257
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
258
            }
259
260
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
261
        }
262
263
        if (! $entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
264
            return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
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

264
            return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NOT_FOUND);
Loading history...
265
        }
266
267
        if ($saved) {
268
            return response()->json(
269
                ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
270
                JsonResponse::HTTP_OK
271
            );
272
        }
273
274
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
275
    }
276
277
    /**
278
     * Remove the specified resource from storage.
279
     *
280
     * @param int $entityId
281
     *
282
     * @return \Illuminate\Http\RedirectResponse
283
     * @throws \Exception
284
     */
285
    public function destroy($entityId)
286
    {
287
        $entity = $this->interface->find($entityId);
288
        $deleted = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $deleted is dead and can be removed.
Loading history...
289
290
        if ($entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
291
            $deleted = $this->interface->delete($entityId);
292
        }
293
        return $this->makeResponse($entity);
294
    }
295
296
    /**
297
     * Restore the specified resource from storage.
298
     *
299
     * @param int $entityId
300
     *
301
     * @return \Illuminate\Http\RedirectResponse
302
     */
303
    public function restore($entityId)
304
    {
305
        $entity = $this->interface->restore($entityId);
306
307
        return $this->makeResponse($entity);
308
    }
309
310
    /**
311
     * Restore the specified resource from storage.
312
     *
313
     * @param int $entityId
314
     *
315
     * @return \Illuminate\Http\RedirectResponse
316
     */
317
    public function forceDelete($entityId)
318
    {
319
        $entity = $this->interface->forceDelete($entityId);
320
321
        return $this->makeResponse($entity);
322
    }
323
324
    /**
325
     * Make response for web or json.
326
     *
327
     * @param mixed $entity
328
     * @param boolean $appendEntity
329
     *
330
     * @return \Illuminate\Http\RedirectResponse
331
     */
332
    function makeResponse($entity,$appendEntity = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
333
    {
334
        if (! $this->isAPI) {
335
            if ($entity) {
336
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
0 ignored issues
show
Bug introduced by
The function __ 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

336
                return \Redirect::to($this->routeIndex)->with('message', /** @scrutinizer ignore-call */ __('messages.success'));
Loading history...
337
            }
338
339
            if ($entity === null) {
340
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
341
            }
342
343
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
344
        }
345
346
        if ($entity) {
347
            if($appendEntity)
348
                return response()->json(  ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
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

348
                return /** @scrutinizer ignore-call */ response()->json(  ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
Loading history...
349
                    JsonResponse::HTTP_OK);
350
            return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
351
        }
352
353
        if ($entity === null) {
354
            return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
355
        }
356
357
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
358
    }
359
}
360