Passed
Push — master ( 0a4c8d...e29dcc )
by Hamzah
04:13 queued 22s
created

Controller::show()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
rs 9.472
cc 4
nc 4
nop 1
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\Bases;
10
11
use Illuminate\Http\JsonResponse;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Collection;
14
15
/**
16
 * Class BaseController.
17
 */
18
class Controller extends \App\Http\Controllers\Controller
19
{
20
    /**
21
     * @var Contract
22
     */
23
    protected $interface;
24
25
    protected $limit = 10;
26
27
    protected $routeIndex = '';
28
29
    protected $pageTitle = '';
30
    protected $createRoute = '';
31
32
    protected $viewIndex = '';
33
    protected $viewCreate = '';
34
    protected $viewEdit = '';
35
    protected $viewShow = '';
36
    protected $breadcrumbs;
37
38
    protected $menu;
39
    protected $search = [];
40
    protected $selectedMenu = [];
41
    protected $isAPI = false;
42
    protected $trash = false;
43
    protected $params = [];
44
    /**
45
     * @var Request
46
     */
47
    private $request;
48
49
    /**
50
     * BaseController constructor.
51
     *
52
     * @param Contract $interface
53
     * @param Request  $request
54
     */
55
    public function __construct(Contract $interface, Request $request)
56
    {
57
        $this->menu = new Collection();
58
        $this->breadcrumbs = new Collection();
59
60
        $language = $request->header('Language', 'en');
61
        if (! in_array($language, \Config::get('app.locales'))) {
62
            $language = 'en';
63
        }
64
        $limit = $request->get('limit', 10);
65
66
        if ((bool) $request->get('with-trash', false)) {
67
            $interface->withTrash();
68
        }
69
        if ((bool) $request->get('only-trash', false)) {
70
            $interface->trash();
71
        }
72
73
        $request->offsetUnset('only-trash');
74
        $request->offsetUnset('with-trash');
75
76
        if (in_array($limit, [20, 30, 40, 50])) {
77
            $this->limit = $limit;
78
        }
79
80
        \App::setLocale($language);
81
        $this->interface = $interface;
82
        $this->isAPI = $request->expectsJson();
83
84
        if (! $this->isAPI) {
85
            $this->breadcrumbs = new Collection();
86
            $this->search = new Collection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Illuminate\Support\Collection() of type object<Illuminate\Support\Collection> is incompatible with the declared type array of property $search.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
            \View::share('pageTitle', $this->pageTitle.' | '.\Config::get('app.name'));
88
            \View::share('breadcrumbs', $this->breadcrumbs);
89
            \View::share('menu', $this->menu);
90
            \View::share('search', $this->search);
91
            \View::share('selectedMenu', $this->selectedMenu);
92
        }
93
        $this->request = $request;
94
    }
95
96
    /**
97
     * Display a listing of the resource.
98
     *
99
     * @param Request $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
100
     *
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function index()
104
    {
105
        $data = $this->interface->simplePaginate($this->limit, $this->request->all());
106
        if (! $this->isAPI) {
107
            \View::share('pageTitle', 'List '.$this->pageTitle.' | '.\Config::get('app.name'));
108
            $this->breadcrumbs->put('index', [
109
                'link' => $this->routeIndex,
110
                'text' => $this->pageTitle,
111
            ]);
112
113
            return view($this->viewIndex, $this->params)
114
                ->with('entities', $data)
115
                ->with('createRoute', $this->createRoute)
116
                ->with('filters', $this->request->all())
117
                ->with('activities', $this->interface->activities())
0 ignored issues
show
Bug introduced by
The method activities() does not seem to exist on object<Shamaseen\Reposit...nerator\Bases\Contract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
                ->with('notes', $this->interface->notes())
0 ignored issues
show
Bug introduced by
The method notes() does not seem to exist on object<Shamaseen\Reposit...nerator\Bases\Contract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
                ->with('files', $this->interface->files());
0 ignored issues
show
Bug introduced by
The method files() does not seem to exist on object<Shamaseen\Reposit...nerator\Bases\Contract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
        }
121
        if ($data->hasMorePages()) {
122
            return response()->json($data, JsonResponse::HTTP_PARTIAL_CONTENT);
123
        }
124
        if (0 == $data->count()) {
125
            return response()->json($data, JsonResponse::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
126
        }
127
128
        return response()->json($data, JsonResponse::HTTP_OK);
129
    }
130
131
    /**
132
     * Show the form for creating a new resource.
133
     *
134
     * @return \Illuminate\Http\Response
135
     */
136
    public function create()
137
    {
138
        if (! $this->isAPI) {
139
            \View::share('pageTitle', 'Create '.$this->pageTitle.' | '.\Config::get('app.name'));
140
            $this->breadcrumbs->put('create', [
141
                'link' => $this->createRoute,
142
                'text' => trans('common/others.create'),
143
            ]);
144
145
            return view($this->viewCreate, $this->params);
146
        }
147
148
        return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
149
    }
150
151
    /**
152
     * Store a newly created resource in storage.
153
     *
154
     * @return \Illuminate\Http\RedirectResponse|mixed
155
     */
156
    public function baseStore()
157
    {
158
        $entity = $this->interface->create($this->request->except(['_token', '_method']));
159
        if (! $this->isAPI) {
160
            if ($entity) {
161
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
162
            }
163
164
            return \Redirect::to($this->routeIndex)->with('error', __('messages.error'));
165
        }
166
167
        if ($entity) {
168
            return response()->json(
169
                ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
170
                JsonResponse::HTTP_OK
171
            );
172
        }
173
174
        return response()->json(
175
            ['status' => false, 'message' => __('messages.error')],
176
            JsonResponse::HTTP_OK
177
        );
178
    }
179
180
    /**
181
     * Display the specified resource.
182
     *
183
     * @param int $entityId
184
     *
185
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
186
     */
187
    public function show($entityId)
188
    {
189
        $entity = $this->interface->find($entityId);
190
        if (! $this->isAPI) {
191
            if (! $entity) {
192
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
193
            }
194
            \View::share('pageTitle', 'View '.$this->pageTitle.' | '.\Config::get('app.name'));
195
            $this->breadcrumbs->put('view', [
196
                'link' => '',
197
                'text' => $entity->name ?? $entity->title ?? __('messages.view'),
198
            ]);
199
200
            return view($this->viewShow, $this->params)
201
                ->with('entity', $entity)
202
                ->with('activities', $this->interface->activities($entityId))
0 ignored issues
show
Bug introduced by
The method activities() does not seem to exist on object<Shamaseen\Reposit...nerator\Bases\Contract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
                ->with('notes', $this->interface->notes($entityId))
0 ignored issues
show
Bug introduced by
The method notes() does not seem to exist on object<Shamaseen\Reposit...nerator\Bases\Contract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
204
                ->with('files', $this->interface->files($entityId));
0 ignored issues
show
Bug introduced by
The method files() does not seem to exist on object<Shamaseen\Reposit...nerator\Bases\Contract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
205
        }
206
        if (! $entity) {
207
            return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
208
        }
209
210
        return response()->json(
211
            ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
212
            JsonResponse::HTTP_OK
213
        );
214
    }
215
216
    /**
217
     * Show the form for editing the specified resource.
218
     *
219
     * @param int $entityId
220
     *
221
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
222
     */
223
    public function edit($entityId)
224
    {
225
        $entity = $this->interface->find($entityId);
226
        if (! $this->isAPI) {
227
            if (! $entity) {
228
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
229
            }
230
            $this->breadcrumbs->put('edit', [
231
                'link' => '',
232
                'text' => $entity->name ?? $entity->title ?? __('messages.view'),
233
            ]);
234
235
            return view($this->viewEdit, $this->params)
236
                ->with('entity', $entity)
237
                ->with('activities', $this->interface->activities($entityId))
0 ignored issues
show
Bug introduced by
The method activities() does not seem to exist on object<Shamaseen\Reposit...nerator\Bases\Contract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
238
                ->with('notes', $this->interface->notes($entityId))
0 ignored issues
show
Bug introduced by
The method notes() does not seem to exist on object<Shamaseen\Reposit...nerator\Bases\Contract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
239
                ->with('files', $this->interface->files($entityId))
0 ignored issues
show
Bug introduced by
The method files() does not seem to exist on object<Shamaseen\Reposit...nerator\Bases\Contract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
240
                ->with('uploadable_type', $this->interface->entityName())
241
                ->with('uploadable_id', $entityId)
242
                ->with('notable_type', $this->interface->entityName())
243
                ->with('notable_id', $entityId);
244
        }
245
246
        return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
247
    }
248
249
    /**
250
     * Update the specified resource in storage.
251
     *
252
     * @param \Illuminate\Http\Request $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
253
     * @param int                      $entityId
254
     *
255
     * @return \Illuminate\Http\RedirectResponse
256
     */
257
    public function baseUpdate($entityId)
258
    {
259
        $entity = $this->interface->find($entityId);
260
        $saved = false;
261
262
        if ($entity) {
263
            $saved = $this->interface->update($entityId, $this->request->except(['_token', '_method']));
264
        }
265
266
        if (! $this->isAPI) {
267
            if (! $entity) {
268
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
269
            }
270
271
            if ($saved) {
272
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
273
            }
274
275
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
276
        }
277
278
        if (! $entity) {
279
            return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
280
        }
281
282
        if ($saved) {
283
            return response()->json(
284
                ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
285
                JsonResponse::HTTP_OK
286
            );
287
        }
288
289
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
290
    }
291
292
    /**
293
     * Remove the specified resource from storage.
294
     *
295
     * @param int $entityId
296
     *
297
     * @return \Illuminate\Http\RedirectResponse
298
     */
299
    public function destroy($entityId)
300
    {
301
        $entity = $this->interface->find($entityId);
302
        $deleted = false;
303
304
        if ($entity) {
305
            $deleted = $this->interface->delete($entityId);
306
        }
307
        if (! $this->isAPI) {
308
            if (! $entity) {
309
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
310
            }
311
            if ($deleted) {
312
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
313
            }
314
315
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
316
        }
317
318
        if ($entity) {
319
            return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
320
        }
321
322
        if ($deleted) {
323
            return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
324
        }
325
326
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
327
    }
328
    /**
329
     * Restore the specified resource from storage.
330
     *
331
     * @param int $entityId
332
     *
333
     * @return \Illuminate\Http\RedirectResponse
334
     */
335
    public function restore($entityId)
336
    {
337
        $entity = $this->interface->restore($entityId);
338
339
        if (! $this->isAPI) {
340
            if (! $entity) {
341
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
342
            }
343
            if ($entity) {
344
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
345
            }
346
347
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
348
        }
349
350
        if ($entity) {
351
            return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
352
        }
353
354
        if ($entity) {
355
            return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
356
        }
357
358
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
359
    }
360
361
    /**
362
     * Restore the specified resource from storage.
363
     *
364
     * @param int $entityId
365
     *
366
     * @return \Illuminate\Http\RedirectResponse
367
     */
368
    public function forceDelete($entityId)
369
    {
370
        $entity = $this->interface->forceDelete($entityId);
371
372
        if (! $this->isAPI) {
373
            if (! $entity) {
374
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
375
            }
376
            if ($entity) {
377
                return \Redirect::to($this->routeIndex)->with('message', __('messages.success'));
378
            }
379
380
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
381
        }
382
383
        if ($entity) {
384
            return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
385
        }
386
387
        if ($entity) {
388
            return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
389
        }
390
391
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
392
    }
393
}
394