Test Failed
Push — master ( 509deb...470d4b )
by Terzi
03:37
created

ScaffoldController::searchableQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Terranet\Administrator\Controllers;
4
5
use Illuminate\Contracts\Support\Renderable;
6
use Illuminate\Database\Eloquent\Builder;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Builder 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\Http\JsonResponse;
8
use Illuminate\Http\RedirectResponse;
9
use Illuminate\Http\Request;
10
use Illuminate\View\View;
11
use Symfony\Component\HttpFoundation\Response;
12
use Terranet\Administrator\Contracts\Module;
13
use Terranet\Administrator\Requests\UpdateRequest;
14
use Terranet\Administrator\Scaffolding;
15
use Terranet\Administrator\Services\MediaLibraryProvider;
16
17
class ScaffoldController extends AdminController
18
{
19
    /**
20
     * @param        $page
21
     * @param  Module  $resource
22
     * @return View
23
     */
24
    public function index($page, Module $resource)
25
    {
26
        $this->authorize('index', $resource->model());
27
28
        $items = $resource->finderInstance()->fetchAll();
29
30
        return view(app('scaffold.template')->index('index'), ['items' => $items]);
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

30
        return /** @scrutinizer ignore-call */ view(app('scaffold.template')->index('index'), ['items' => $items]);
Loading history...
Bug introduced by
The function app 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

30
        return view(/** @scrutinizer ignore-call */ app('scaffold.template')->index('index'), ['items' => $items]);
Loading history...
31
    }
32
33
    /**
34
     * View resource.
35
     *
36
     * @param $page
37
     * @param $id
38
     * @return View
39
     */
40
    public function view($page, $id)
41
    {
42
        $this->authorize('view', $eloquent = app('scaffold.model'));
0 ignored issues
show
Bug introduced by
The function app 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

42
        $this->authorize('view', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.model'));
Loading history...
43
44
        return view(app('scaffold.template')->view('index'), [
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

44
        return /** @scrutinizer ignore-call */ view(app('scaffold.template')->view('index'), [
Loading history...
45
            'item' => $eloquent,
46
        ]);
47
    }
48
49
    /**
50
     * Edit resource.
51
     *
52
     * @param $page
53
     * @param $id
54
     * @return View
55
     */
56
    public function edit($page, $id): View
57
    {
58
        $this->authorize('update', $eloquent = app('scaffold.model'));
0 ignored issues
show
Bug introduced by
The function app 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

58
        $this->authorize('update', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.model'));
Loading history...
59
60
        return view(app('scaffold.template')->edit('index'), [
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

60
        return /** @scrutinizer ignore-call */ view(app('scaffold.template')->edit('index'), [
Loading history...
61
            'item' => $eloquent,
62
        ]);
63
    }
64
65
    /**
66
     * @param                    $page
67
     * @param                    $id
68
     * @param  null|UpdateRequest  $request
69
     * @return RedirectResponse
70
     */
71
    public function update($page, $id, UpdateRequest $request)
72
    {
73
        /** @var Scaffolding $resource */
74
        $resource = app('scaffold.module');
0 ignored issues
show
Bug introduced by
The function app 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

74
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
75
76
        $this->authorize('update', $eloquent = app('scaffold.model'));
77
78
        try {
79
            $resource->actionsManager()->exec('save', [$eloquent, $request]);
80
        } catch (\Exception $e) {
81
            return back()->withErrors([$e->getMessage()]);
0 ignored issues
show
Bug introduced by
The function back 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

81
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
82
        }
83
84
        return $this->redirectTo($page, $id, $request)->with(
85
            'messages',
86
            [$this->translatedMessage('update_success', $resource)]
87
        );
88
    }
89
90
    /**
91
     * Create new item.
92
     *
93
     * @return View
94
     */
95
    public function create()
96
    {
97
        $this->authorize('create', $eloquent = app('scaffold.module')->model());
0 ignored issues
show
Bug introduced by
The function app 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

97
        $this->authorize('create', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.module')->model());
Loading history...
98
99
        return view(app('scaffold.template')->edit('index'), ['item' => $eloquent]);
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

99
        return /** @scrutinizer ignore-call */ view(app('scaffold.template')->edit('index'), ['item' => $eloquent]);
Loading history...
100
    }
101
102
    /**
103
     * Store new item.
104
     *
105
     * @param                    $page
106
     * @param  null|UpdateRequest  $request
107
     * @return RedirectResponse
108
     */
109
    public function store($page, UpdateRequest $request)
110
    {
111
        /** @var Scaffolding $resource */
112
        $resource = app('scaffold.module');
0 ignored issues
show
Bug introduced by
The function app 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

112
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
113
114
        $this->authorize('create', $eloquent = $resource->model());
115
116
        try {
117
            $eloquent = $resource->actionsManager()->exec('save', [$eloquent, $request]);
118
        } catch (\Exception $e) {
119
            return back()->withErrors([$e->getMessage()]);
0 ignored issues
show
Bug introduced by
The function back 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

119
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
120
        }
121
122
        return $this->redirectTo($page, $eloquent->id, $request)->with('messages', [$this->translatedMessage('create_success', $resource)]);
123
    }
124
125
    /**
126
     * Destroy item.
127
     *
128
     * @param  Module  $module
129
     * @return RedirectResponse
130
     */
131
    public function delete(Module $module)
132
    {
133
        $this->authorize('delete', $eloquent = app('scaffold.model'));
0 ignored issues
show
Bug introduced by
The function app 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

133
        $this->authorize('delete', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.model'));
Loading history...
134
135
        try {
136
            $module->actionsManager()->exec('delete', [$eloquent]);
137
        } catch (\Exception $e) {
138
            return back()->withErrors([$e->getMessage()]);
0 ignored issues
show
Bug introduced by
The function back 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

138
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
139
        }
140
141
        $message = $this->translatedMessage('remove_success', $module);
142
143
        return redirect()->to(route('scaffold.index', ['module' => $module]))->with('messages', [$message]);
0 ignored issues
show
Bug introduced by
The function route 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 redirect()->to(/** @scrutinizer ignore-call */ route('scaffold.index', ['module' => $module]))->with('messages', [$message]);
Loading history...
Bug introduced by
The function redirect 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 */ redirect()->to(route('scaffold.index', ['module' => $module]))->with('messages', [$message]);
Loading history...
144
    }
145
146
    /**
147
     * Destroy attachment.
148
     *
149
     * @param $page
150
     * @param $id
151
     * @param $attachment
152
     * @return RedirectResponse
153
     */
154
    public function deleteAttachment($page, $id, $attachment)
155
    {
156
        /** @var Module $resource */
157
        $resource = app('scaffold.module');
0 ignored issues
show
Bug introduced by
The function app 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

157
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
158
159
        $this->authorize('update', $eloquent = app('scaffold.model'));
160
161
        try {
162
            $resource->actionsManager()->exec('detachFile', [$eloquent, $attachment]);
163
        } catch (\Exception $e) {
164
            return back()->withErrors([$e->getMessage()]);
0 ignored issues
show
Bug introduced by
The function back 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

164
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
165
        }
166
167
        return back()->with('messages', [$this->translatedMessage('attachment_remove_success', $resource)]);
168
    }
169
170
    /**
171
     * @param $module
172
     * @param $id
173
     * @param  Request  $request
174
     * @return JsonResponse
175
     */
176
    public function fetchMedia($module, $id, Request $request)
177
    {
178
        $this->authorize('view', $eloquent = app('scaffold.model'));
0 ignored issues
show
Bug introduced by
The function app 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

178
        $this->authorize('view', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.model'));
Loading history...
179
180
        $media = MediaLibraryProvider::forModel($eloquent)->fetch(
181
            $request->get('collection', 'default'),
182
            20
183
        );
184
185
        $items = collect($media->items())->map([MediaLibraryProvider::class, 'toJson']);
186
187
        return response()->json(array_merge(
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

187
        return /** @scrutinizer ignore-call */ response()->json(array_merge(
Loading history...
188
            $media->toArray(),
189
            ['data' => $items->toArray()]
190
        ));
191
    }
192
193
    /**
194
     * @param $page
195
     * @param $id
196
     * @param  string  $collection
197
     * @param  Request  $request
198
     * @return RedirectResponse
199
     */
200
    public function attachMedia($page, $id, string $collection, Request $request)
201
    {
202
        $this->authorize('update', $eloquent = app('scaffold.model'));
0 ignored issues
show
Bug introduced by
The function app 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
        $this->authorize('update', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.model'));
Loading history...
203
204
        $file = $request->file('_media_')[$collection];
205
206
        $mediaItem = MediaLibraryProvider::forModel($eloquent)->attach($file, $collection);
207
208
        return response()->json(MediaLibraryProvider::toJson($mediaItem));
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

208
        return /** @scrutinizer ignore-call */ response()->json(MediaLibraryProvider::toJson($mediaItem));
Loading history...
209
    }
210
211
    /**
212
     * @param $page
213
     * @param $id
214
     * @param $mediaId
215
     * @return JsonResponse
216
     */
217
    public function detachMedia($page, $id, $mediaId)
218
    {
219
        $this->authorize('update', $eloquent = app('scaffold.model'));
0 ignored issues
show
Bug introduced by
The function app 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

219
        $this->authorize('update', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.model'));
Loading history...
220
221
        MediaLibraryProvider::forModel($eloquent)->detach($mediaId);
222
223
        return response()->json([], \Illuminate\Http\Response::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

223
        return /** @scrutinizer ignore-call */ response()->json([], \Illuminate\Http\Response::HTTP_NO_CONTENT);
Loading history...
224
    }
225
226
    /**
227
     * Search for a model(s).
228
     *
229
     * @param  Request  $request
230
     * @return JsonResponse
231
     */
232
    public function search(Request $request): JsonResponse
233
    {
234
        $term = $request->get('query');
235
        $eloquent = $request->get('searchable');
236
237
        $items = [];
238
239
        if ($eloquent) {
240
            $column = $request->get('field');
241
            $eloquent = new $eloquent();
242
243
            $query = method_exists($eloquent, 'searchableQuery')
244
                ? $eloquent->searchableQuery($term)
245
                : $this->searchableQuery($term, $eloquent, $column);
246
247
            $items = $query
248
                ->get(["{$eloquent->getKeyName()} as id", "{$column} as name"])
249
                ->toArray();
250
        }
251
252
        return response()->json(['items' => $items]);
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

252
        return /** @scrutinizer ignore-call */ response()->json(['items' => $items]);
Loading history...
253
    }
254
255
    /**
256
     * Custom action related to item.
257
     *
258
     * @param $page
259
     * @param $id
260
     * @param $action
261
     * @return RedirectResponse
262
     */
263
    public function action($page, $id, $action)
264
    {
265
        /** @var Module $resource */
266
        $resource = app('scaffold.module');
0 ignored issues
show
Bug introduced by
The function app 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

266
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
267
268
        $this->authorize($action, $eloquent = app('scaffold.model'));
269
270
        $response = $resource->actionsManager()->exec('action::'.$action, [$eloquent]);
271
272
        if ($response instanceof Response || $response instanceof Renderable) {
273
            return $response;
274
        }
275
276
        return back()->with('messages', [$this->translatedMessage('action_success', $resource)]);
0 ignored issues
show
Bug introduced by
The function back 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

276
        return /** @scrutinizer ignore-call */ back()->with('messages', [$this->translatedMessage('action_success', $resource)]);
Loading history...
277
    }
278
279
    /**
280
     * Generate action message.
281
     *
282
     * @param  string  $action
283
     * @param  Module  $resource
284
     * @return string
285
     */
286
    protected function translatedMessage(string $action, $resource): string
287
    {
288
        if (method_exists($resource, 'flashMessage')) {
289
            return $resource->flashMessage(request(), $action);
0 ignored issues
show
Bug introduced by
The function request 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

289
            return $resource->flashMessage(/** @scrutinizer ignore-call */ request(), $action);
Loading history...
290
        }
291
292
        return $this->translator->has($key = sprintf('administrator::messages.%s.%s', $resource->url(), $action))
293
            ? trans($key)
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

293
            ? /** @scrutinizer ignore-call */ trans($key)
Loading history...
294
            : trans(sprintf('administrator::messages.%s', $action));
295
    }
296
297
    /**
298
     * @param $term
299
     * @param $eloquent
300
     * @param $column
301
     * @return mixed
302
     */
303
    protected function searchableQuery($term, $eloquent, $column): Builder
304
    {
305
        $searchByKey = is_numeric($term);
306
        $searchableKey = $searchByKey ? $eloquent->getKeyName() : $column;
307
308
        return $eloquent->newQuery()
309
            ->when($searchByKey, function ($query) use ($searchableKey, $term) {
310
                return $query->where($searchableKey, (int) $term);
311
            })
312
            ->when(!$searchByKey, function ($query) use ($searchableKey, $term) {
313
                return $query->orWhere($searchableKey, 'LIKE', "%{$term}%");
314
            });
315
    }
316
}
317