Test Failed
Push — master ( 46dc5e...5b4f22 )
by Terzi
05:21
created

ScaffoldController::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 4
b 0
f 0
nc 3
nop 1
dl 0
loc 19
ccs 0
cts 8
cp 0
crap 12
rs 9.9332
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
     * @return \Illuminate\View\View
22
     */
23
    public function index($page, Module $resource)
24
    {
25
        $this->authorize('index', $resource->model());
26
27
        $items = $resource->finderInstance()->fetchAll();
28
29
        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

29
        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

29
        return view(/** @scrutinizer ignore-call */ app('scaffold.template')->index('index'), ['items' => $items]);
Loading history...
30
    }
31
32
    /**
33
     * View resource.
34
     *
35
     * @param $page
36
     * @param $id
37
     * @return \Illuminate\View\View
38
     */
39
    public function view($page, $id)
40
    {
41
        $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

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

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

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

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

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

80
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
81
        }
82
83
        return $this->redirectTo($page, $id, $request)->with(
84
            'messages',
85
            [$this->translatedMessage('update_success', $resource)]
86
        );
87
    }
88
89
    /**
90
     * Create new item.
91
     *
92
     * @return \Illuminate\View\View
93
     */
94
    public function create()
95
    {
96
        $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

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

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

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

118
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
119
        }
120
121
        return $this->redirectTo($page, $eloquent->id, $request)->with('messages', [$this->translatedMessage('create_success', $resource)]);
122
    }
123
124
    /**
125
     * Destroy item.
126
     *
127
     * @param  Module  $module
128
     * @return \Illuminate\Http\RedirectResponse
129
     */
130
    public function delete(Module $module)
131
    {
132
        $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

132
        $this->authorize('delete', $eloquent = /** @scrutinizer ignore-call */ app('scaffold.model'));
Loading history...
133
134
        $id = $eloquent->id;
135
136
        try {
137
            $module->actionsManager()->exec('delete', [$eloquent]);
138
        } catch (\Exception $e) {
139
            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

139
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
140
        }
141
142
        $message = $this->translatedMessage('remove_success', $module);
143
144
        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

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

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

162
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
163
164
        $this->authorize('update', $eloquent = app('scaffold.model'));
165
166
        try {
167
            $resource->actionsManager()->exec('detachFile', [$eloquent, $attachment]);
168
        } catch (\Exception $e) {
169
            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

169
            return /** @scrutinizer ignore-call */ back()->withErrors([$e->getMessage()]);
Loading history...
170
        }
171
172
173
        return back()->with('messages', [$this->translatedMessage('attachment_remove_success', $resource)]);
174
    }
175
176
    /**
177
     * @param $module
178
     * @param $id
179
     * @param  Request  $request
180
     */
181
    public function fetchMedia($module, $id, Request $request)
182
    {
183
        /** @var Module $resource */
184
        $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

184
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
185
186
        $this->authorize('view', $eloquent = app('scaffold.model'));
187
188
        $media = MediaLibraryProvider::forModel($eloquent)->fetch(
189
            $request->get('collection', 'default'),
190
            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
     * @return \Illuminate\Http\JsonResponse
244
     */
245
    public function search(Request $request): \Illuminate\Http\JsonResponse
246
    {
247
        $eloquent = $request->get('searchable');
248
        $column = $request->get('field');
249
        $term = $request->get('query');
250
251
        $items = [];
252
253
        if ($eloquent && $column) {
254
            $eloquent = new $eloquent();
255
            $searchByKey = is_numeric($term);
256
            $searchableKey = $searchByKey ? $eloquent->getKeyName() : $column;
257
258
            $instance = $eloquent
259
                ->when($searchByKey, function ($query) use ($searchableKey, $term) {
260
                    return $query->where($searchableKey, (int) $term);
261
                })
262
                ->when(!$searchByKey, function ($query) use ($searchableKey, $term) {
263
                    return $query->orWhere($searchableKey, 'LIKE', "%{$term}%");
264
                })
265
                ->get(["{$eloquent->getKeyName()} as id", "{$column} as name"]);
266
267
            $items = $instance->toArray();
268
        }
269
270
        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

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

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

294
        return /** @scrutinizer ignore-call */ back()->with('messages', [$this->translatedMessage('action_success', $resource)]);
Loading history...
295
    }
296
297
    /**
298
     * Generate action message.
299
     *
300
     * @param  string  $action
301
     * @param  Module  $resource
302
     * @return string
303
     */
304
    protected function translatedMessage(string $action, $resource): string
305
    {
306
        return $this->translator->has($key = sprintf('administrator::messages.%s.%s', $resource->url(), $action))
307
            ? 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

307
            ? /** @scrutinizer ignore-call */ trans($key)
Loading history...
308
            : trans(sprintf('administrator::messages.%s', $action));
309
    }
310
}
311