Passed
Push — master ( 734cc2...f72bc1 )
by Hamzah
02:25
created

Controller::restore()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 6
nop 1
dl 0
loc 24
rs 9.2222
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 baseStore()
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 baseUpdate($entityId)
242
    {
243
        $entity = $this->interface->find($entityId);
244
        $saved = false;
245
246
        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...
247
            $saved = $this->interface->update($entityId, $this->request->except(['_token', '_method']));
248
        }
249
250
        if (! $this->isAPI) {
251
            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...
252
                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

252
                return \Redirect::to($this->routeIndex)->with('warning', /** @scrutinizer ignore-call */ __('messages.not_found'));
Loading history...
253
            }
254
255
            if ($saved) {
256
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
257
            }
258
259
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
260
        }
261
262
        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...
263
            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

263
            return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NOT_FOUND);
Loading history...
264
        }
265
266
        if ($saved) {
267
            return response()->json(
268
                ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
269
                JsonResponse::HTTP_OK
270
            );
271
        }
272
273
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
274
    }
275
276
    /**
277
     * Remove the specified resource from storage.
278
     *
279
     * @param int $entityId
280
     *
281
     * @return \Illuminate\Http\RedirectResponse
282
     * @throws \Exception
283
     */
284
    public function destroy($entityId)
285
    {
286
        $entity = $this->interface->find($entityId);
287
        $deleted = false;
288
289
        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...
290
            $deleted = $this->interface->delete($entityId);
291
        }
292
        if (! $this->isAPI) {
293
            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...
294
                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

294
                return \Redirect::to($this->routeIndex)->with('warning', /** @scrutinizer ignore-call */ __('messages.not_found'));
Loading history...
295
            }
296
            if ($deleted) {
297
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
298
            }
299
300
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
301
        }
302
303
        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...
304
            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

304
            return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NOT_FOUND);
Loading history...
305
        }
306
307
        if ($deleted) {
308
            return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
309
        }
310
311
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
312
    }
313
314
    /**
315
     * Restore the specified resource from storage.
316
     *
317
     * @param int $entityId
318
     *
319
     * @return \Illuminate\Http\RedirectResponse
320
     */
321
    public function restore($entityId)
322
    {
323
        $entity = $this->interface->restore($entityId);
324
325
        if (! $this->isAPI) {
326
            if (! $entity) {
327
                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

327
                return \Redirect::to($this->routeIndex)->with('warning', /** @scrutinizer ignore-call */ __('messages.not_found'));
Loading history...
328
            }
329
            if ($entity) {
0 ignored issues
show
introduced by
The condition $entity is always true.
Loading history...
330
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
331
            }
332
333
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
334
        }
335
336
        if (! $entity) {
337
            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

337
            return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NOT_FOUND);
Loading history...
338
        }
339
340
        if ($entity) {
0 ignored issues
show
introduced by
The condition $entity is always true.
Loading history...
341
            return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
342
        }
343
344
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
345
    }
346
347
    /**
348
     * Restore the specified resource from storage.
349
     *
350
     * @param int $entityId
351
     *
352
     * @return \Illuminate\Http\RedirectResponse
353
     */
354
    public function forceDelete($entityId)
355
    {
356
        $entity = $this->interface->forceDelete($entityId);
357
358
        if (! $this->isAPI) {
359
            if (! $entity) {
360
                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

360
                return \Redirect::to($this->routeIndex)->with('warning', /** @scrutinizer ignore-call */ __('messages.not_found'));
Loading history...
361
            }
362
            if ($entity) {
0 ignored issues
show
introduced by
The condition $entity is always true.
Loading history...
363
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
364
            }
365
366
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
367
        }
368
369
        if (! $entity) {
370
            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

370
            return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NOT_FOUND);
Loading history...
371
        }
372
373
        if ($entity) {
0 ignored issues
show
introduced by
The condition $entity is always true.
Loading history...
374
            return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
375
        }
376
377
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
378
    }
379
}
380