Passed
Push — master ( fac26f...940892 )
by Mohammad
03:07 queued 10s
created

Controller::index()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 23
rs 9.7666
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
        return $this->makeResponse($entity,true);
155
    }
156
157
    /**
158
     * Display the specified resource.
159
     *
160
     * @param int $entityId
161
     *
162
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
163
     */
164
    public function show($entityId)
165
    {
166
        $entity = $this->interface->find($entityId);
167
        if (! $this->isAPI) {
168
            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...
169
                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

169
                return \Redirect::to($this->routeIndex)->with('warning', /** @scrutinizer ignore-call */ __('messages.not_found'));
Loading history...
170
            }
171
            \View::share('pageTitle', 'View '.$this->pageTitle.' | '.\Config::get('app.name'));
172
            $this->breadcrumbs->put('view', [
173
                'link' => '',
174
                'text' => $entity->name ?? $entity->title ?? __('messages.view'),
175
            ]);
176
177
            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

177
            return /** @scrutinizer ignore-call */ view($this->viewShow, $this->params)
Loading history...
178
                ->with('entity', $entity);
179
        }
180
        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...
181
            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

181
            return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NOT_FOUND);
Loading history...
182
        }
183
184
        return response()->json(
185
            ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
186
            JsonResponse::HTTP_OK
187
        );
188
    }
189
190
    /**
191
     * Show the form for editing the specified resource.
192
     *
193
     * @param int $entityId
194
     *
195
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
196
     */
197
    public function edit($entityId)
198
    {
199
        $entity = $this->interface->find($entityId);
200
        if (! $this->isAPI) {
201
            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...
202
                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

202
                return \Redirect::to($this->routeIndex)->with('warning', /** @scrutinizer ignore-call */ __('messages.not_found'));
Loading history...
203
            }
204
            $this->breadcrumbs->put('edit', [
205
                'link' => '',
206
                'text' => $entity->name ?? $entity->title ?? __('messages.view'),
207
            ]);
208
209
            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

209
            return /** @scrutinizer ignore-call */ view($this->viewEdit, $this->params)
Loading history...
210
                ->with('entity', $entity);
211
        }
212
213
        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

213
        return /** @scrutinizer ignore-call */ response()->json(null, JsonResponse::HTTP_NOT_FOUND);
Loading history...
214
    }
215
216
    /**
217
     * Update the specified resource in storage.
218
     *
219
     * @param int $entityId
220
     *
221
     * @return \Illuminate\Http\RedirectResponse
222
     */
223
    public function update($entityId)
224
    {
225
        $entity = $this->interface->update($entityId, $this->request->except(['_token', '_method']));
226
227
        return $this->makeResponse($entity,ture);
0 ignored issues
show
Bug introduced by
The constant Shamaseen\Repository\Generator\Utility\ture was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
228
    }
229
230
    /**
231
     * Remove the specified resource from storage.
232
     *
233
     * @param int $entityId
234
     *
235
     * @return \Illuminate\Http\RedirectResponse
236
     * @throws \Exception
237
     */
238
    public function destroy($entityId)
239
    {
240
        $deleted = $this->interface->delete($entityId);
241
242
        return $this->makeResponse($deleted);
243
    }
244
245
    /**
246
     * Restore the specified resource from storage.
247
     *
248
     * @param int $entityId
249
     *
250
     * @return \Illuminate\Http\RedirectResponse
251
     */
252
    public function restore($entityId)
253
    {
254
        $entity = $this->interface->restore($entityId);
255
256
        return $this->makeResponse($entity);
257
    }
258
259
    /**
260
     * Restore the specified resource from storage.
261
     *
262
     * @param int $entityId
263
     *
264
     * @return \Illuminate\Http\RedirectResponse
265
     */
266
    public function forceDelete($entityId)
267
    {
268
        $entity = $this->interface->forceDelete($entityId);
269
270
        return $this->makeResponse($entity);
271
    }
272
273
    /**
274
     * Make response for web or json.
275
     *
276
     * @param mixed $entity
277
     * @param boolean $appendEntity
278
     *
279
     * @return \Illuminate\Http\RedirectResponse
280
     */
281
    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...
282
    {
283
        if (! $this->isAPI) {
284
            if ($entity) {
285
                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

285
                return \Redirect::to($this->routeIndex)->with('message', /** @scrutinizer ignore-call */ __('messages.success'));
Loading history...
286
            }
287
288
            if ($entity === null) {
289
                return \Redirect::to($this->routeIndex)->with('warning', __('messages.not_found'));
290
            }
291
292
            return \Redirect::to($this->routeIndex)->with('error', __('messages.not_modified'));
293
        }
294
295
        if ($entity) {
296
            if($appendEntity)
297
                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

297
                return /** @scrutinizer ignore-call */ response()->json(  ['status' => true, 'message' => __('messages.success'), 'data' => $entity],
Loading history...
298
                    JsonResponse::HTTP_OK);
299
            return response()->json(null, JsonResponse::HTTP_NO_CONTENT);
300
        }
301
302
        if ($entity === null) {
303
            return response()->json(null, JsonResponse::HTTP_NOT_FOUND);
304
        }
305
306
        return response()->json(null, JsonResponse::HTTP_NOT_MODIFIED);
307
    }
308
}
309