Completed
Push — master ( ad6b4f...e57368 )
by Terzi
05:01 queued 10s
created

ScaffoldController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 82
dl 0
loc 283
ccs 0
cts 65
cp 0
rs 10
c 0
b 0
f 0
wmc 21

13 Methods

Rating   Name   Duplication   Size   Complexity  
A view() 0 6 1
A index() 0 7 1
A edit() 0 6 1
A deleteAttachment() 0 10 1
A fetchMedia() 0 16 1
A update() 0 16 2
A search() 0 26 4
A create() 0 5 1
A detachMedia() 0 10 1
A delete() 0 15 2
A action() 0 16 3
A store() 0 16 2
A attachMedia() 0 12 1
1
<?php
2
3
namespace Terranet\Administrator\Controllers;
4
5
use Illuminate\Contracts\Support\Renderable;
6
use Illuminate\Http\RedirectResponse;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\URL;
9
use Spatie\MediaLibrary\Models\Media;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\Models\Media 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...
10
use Symfony\Component\HttpFoundation\Response;
11
use Terranet\Administrator\Contracts\Module;
12
use Terranet\Administrator\Requests\UpdateRequest;
13
use Terranet\Administrator\Scaffolding;
14
use Terranet\Administrator\Services\MediaLibraryProvider;
15
16
class ScaffoldController extends AdminController
17
{
18
    /**
19
     * @param        $page
20
     * @param Module $resource
21
     *
22
     * @return \Illuminate\View\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
     *
39
     * @return \Illuminate\View\View
40
     */
41
    public function view($page, $id)
42
    {
43
        $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

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

45
        return /** @scrutinizer ignore-call */ view(app('scaffold.template')->view('index'), [
Loading history...
46
            'item' => $eloquent,
47
        ]);
48
    }
49
50
    /**
51
     * Edit resource.
52
     *
53
     * @param $page
54
     * @param $id
55
     *
56
     * @return \Illuminate\View\View
57
     */
58
    public function edit($page, $id)
59
    {
60
        $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

60
        $this->authorize('update', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.model'));
Loading history...
61
62
        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

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

77
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
78
79
        $this->authorize('update', $eloquent = app('scaffold.model'));
80
81
        try {
82
            $resource->actionsManager()->exec('save', [$eloquent, $request]);
83
        } catch (\Exception $e) {
84
            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

84
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
85
        }
86
87
        return $this->redirectTo($page, $id, $request)->with(
88
            'messages',
89
            [trans('administrator::messages.update_success')]
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

89
            [/** @scrutinizer ignore-call */ trans('administrator::messages.update_success')]
Loading history...
90
        );
91
    }
92
93
    /**
94
     * Create new item.
95
     *
96
     * @return \Illuminate\View\View
97
     */
98
    public function create()
99
    {
100
        $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

100
        $this->authorize('create', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.module')->model());
Loading history...
101
102
        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

102
        return /** @scrutinizer ignore-call */ view(app('scaffold.template')->edit('index'), ['item' => $eloquent]);
Loading history...
103
    }
104
105
    /**
106
     * Store new item.
107
     *
108
     * @param                    $page
109
     * @param null|UpdateRequest $request
110
     *
111
     * @return \Illuminate\Http\RedirectResponse
112
     */
113
    public function store($page, UpdateRequest $request)
114
    {
115
        /** @var Scaffolding $resource */
116
        $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

116
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
117
118
        $this->authorize('create', $eloquent = $resource->model());
119
120
        try {
121
            $eloquent = $resource->actionsManager()->exec('save', [$eloquent, $request]);
122
        } catch (\Exception $e) {
123
            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

123
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
124
        }
125
126
        return $this->redirectTo($page, $eloquent->id, $request)->with(
127
            'messages',
128
            [trans('administrator::messages.create_success')]
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

128
            [/** @scrutinizer ignore-call */ trans('administrator::messages.create_success')]
Loading history...
129
        );
130
    }
131
132
    /**
133
     * Destroy item.
134
     *
135
     * @param Module $module
136
     *
137
     * @return \Illuminate\Http\RedirectResponse
138
     */
139
    public function delete(Module $module)
140
    {
141
        $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

141
        $this->authorize('delete', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.model'));
Loading history...
142
143
        $id = $eloquent->id;
144
145
        $module->actionsManager()->exec('delete', [$eloquent]);
146
147
        $message = trans('administrator::messages.remove_success');
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

147
        $message = /** @scrutinizer ignore-call */ trans('administrator::messages.remove_success');
Loading history...
148
149
        if (URL::previous() === route('scaffold.view', ['module' => $module, 'id' => $id])) {
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

149
        if (URL::previous() === /** @scrutinizer ignore-call */ route('scaffold.view', ['module' => $module, 'id' => $id])) {
Loading history...
150
            return back()->with('messages', [$message]);
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

150
            return /** @scrutinizer ignore-call */ back()->with('messages', [$message]);
Loading history...
151
        }
152
153
        return redirect()->to(route('scaffold.index', ['module' => $module]))->with('messages', [$message]);
0 ignored issues
show
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

153
        return /** @scrutinizer ignore-call */ redirect()->to(route('scaffold.index', ['module' => $module]))->with('messages', [$message]);
Loading history...
154
    }
155
156
    /**
157
     * Destroy attachment.
158
     *
159
     * @param $page
160
     * @param $id
161
     * @param $attachment
162
     *
163
     * @return \Illuminate\Http\RedirectResponse
164
     */
165
    public function deleteAttachment($page, $id, $attachment)
166
    {
167
        /** @var Module $resource */
168
        $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

168
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
169
170
        $this->authorize('update', $eloquent = app('scaffold.model'));
171
172
        $resource->actionsManager()->exec('detachFile', [$eloquent, $attachment]);
173
174
        return back()->with('messages', [trans('administrator::messages.remove_success')]);
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

174
        return /** @scrutinizer ignore-call */ back()->with('messages', [trans('administrator::messages.remove_success')]);
Loading history...
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

174
        return back()->with('messages', [/** @scrutinizer ignore-call */ trans('administrator::messages.remove_success')]);
Loading history...
175
    }
176
177
    /**
178
     * @param $module
179
     * @param $id
180
     * @param Request $request
181
     */
182
    public function fetchMedia($module, $id, Request $request)
183
    {
184
        /** @var Module $resource */
185
        $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

185
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
Unused Code introduced by
The assignment to $resource is dead and can be removed.
Loading history...
186
187
        $this->authorize('view', $eloquent = app('scaffold.model'));
188
189
        $media = MediaLibraryProvider::forModel($eloquent)->fetch(
190
            $request->get('collection', 'default'), 20
191
        );
192
193
        $items = collect($media->items())->map([MediaLibraryProvider::class, 'toJson']);
194
195
        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

195
        return /** @scrutinizer ignore-call */ response()->json(array_merge(
Loading history...
196
            $media->toArray(),
197
            ['data' => $items->toArray()]
198
        ));
199
    }
200
201
    /**
202
     * @param $page
203
     * @param $id
204
     * @param string $conversion
205
     * @param Request $request
206
     * @return RedirectResponse
207
     */
208
    public function attachMedia($page, $id, string $collection, Request $request)
209
    {
210
        /** @var Module $resource */
211
        $resource = app('scaffold.module');
0 ignored issues
show
Unused Code introduced by
The assignment to $resource is dead and can be removed.
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

211
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
212
213
        $this->authorize('update', $eloquent = app('scaffold.model'));
214
215
        $file = $request->file('_media_')[$collection];
216
217
        $mediaItem = MediaLibraryProvider::forModel($eloquent)->attach($file, $collection);
218
219
        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

219
        return /** @scrutinizer ignore-call */ response()->json(MediaLibraryProvider::toJson($mediaItem));
Loading history...
220
    }
221
222
    /**
223
     * @param $page
224
     * @param $id
225
     * @param $mediaId
226
     */
227
    public function detachMedia($page, $id, $mediaId)
228
    {
229
        /** @var Module $resource */
230
        $resource = app('scaffold.module');
0 ignored issues
show
Unused Code introduced by
The assignment to $resource is dead and can be removed.
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

230
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
231
232
        $this->authorize('update', $eloquent = app('scaffold.model'));
233
234
        MediaLibraryProvider::forModel($eloquent)->detach($mediaId);
235
236
        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

236
        return /** @scrutinizer ignore-call */ response()->json([], \Illuminate\Http\Response::HTTP_NO_CONTENT);
Loading history...
237
    }
238
239
    /**
240
     * Search for a model(s).
241
     *
242
     * @param Request $request
243
     *
244
     * @return \Illuminate\Http\JsonResponse
245
     */
246
    public function search(Request $request): \Illuminate\Http\JsonResponse
247
    {
248
        $eloquent = $request->get('searchable');
249
        $column = $request->get('field');
250
        $term = $request->get('query');
251
252
        $items = [];
253
254
        if ($eloquent && $column) {
255
            $eloquent = new $eloquent();
256
            $searchByKey = is_numeric($term);
257
            $searchableKey = $searchByKey ? $eloquent->getKeyName() : $column;
258
259
            $instance = $eloquent
260
                ->when($searchByKey, function ($query) use ($searchableKey, $term) {
261
                    return $query->where($searchableKey, (int)$term);
262
                })
263
                ->when(!$searchByKey, function ($query) use ($searchableKey, $term) {
264
                    return $query->orWhere($searchableKey, 'LIKE', "%{$term}%");
265
                })
266
                ->get(["{$eloquent->getKeyName()} as id", "{$column} as name"]);
267
268
            $items = $instance->toArray();
269
        }
270
271
        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

271
        return /** @scrutinizer ignore-call */ response()->json(['items' => $items]);
Loading history...
272
    }
273
274
    /**
275
     * Custom action related to item.
276
     *
277
     * @param $page
278
     * @param $id
279
     * @param $action
280
     *
281
     * @return \Illuminate\Http\RedirectResponse
282
     */
283
    public function action($page, $id, $action)
284
    {
285
        /** @var Module $resource */
286
        $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

286
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
287
288
        $this->authorize($action, $eloquent = app('scaffold.model'));
289
290
        $response = $resource->actionsManager()->exec('action::'.$action, [$eloquent]);
291
292
        if ($response instanceof Response || $response instanceof Renderable) {
293
            return $response;
294
        }
295
296
        return back()->with(
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

296
        return /** @scrutinizer ignore-call */ back()->with(
Loading history...
297
            'messages',
298
            [trans('administrator::messages.action_success')]
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

298
            [/** @scrutinizer ignore-call */ trans('administrator::messages.action_success')]
Loading history...
299
        );
300
    }
301
}
302