Completed
Push — master ( e7495c...b3c94b )
by Abdelrahman
33:39
created

PagesMediaController::authorizeResource()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
cc 5
nc 12
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Pages\Http\Controllers\Adminarea;
6
7
use Cortex\Pages\Models\Page;
8
use Spatie\MediaLibrary\Models\Media;
9
use Cortex\Foundation\DataTables\MediaDataTable;
10
use Cortex\Foundation\Http\Requests\ImageFormRequest;
11
use Cortex\Foundation\Http\Controllers\AuthorizedController;
12
13
class PagesMediaController extends AuthorizedController
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $resource = Page::class;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function authorizeResource($model, $parameter = null, array $options = [], $request = null): void
24
    {
25
        $middleware = [];
26
        $parameter = $parameter ?: snake_case(class_basename($model));
27
28
        foreach ($this->mapResourceAbilities() as $method => $ability) {
29
            $modelName = in_array($method, $this->resourceMethodsWithoutModels()) ? $model : $parameter;
30
31
            $middleware["can:update,{$modelName}"][] = $method;
32
            $middleware["can:{$ability},media"][] = $method;
33
        }
34
35
        foreach ($middleware as $middlewareName => $methods) {
36
            $this->middleware($middlewareName, $options)->only($methods);
37
        }
38
    }
39
40
    /**
41
     * List all page media.
42
     *
43
     * @param \Cortex\Foundation\DataTables\MediaDataTable $mediaDataTable
44
     * @param \Cortex\Pages\Models\Page                    $page
45
     *
46
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...e|\Illuminate\View\View?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
47
     */
48
    public function index(Page $page, MediaDataTable $mediaDataTable)
49
    {
50
        return $mediaDataTable->with([
51
            'resource' => $page,
52
            'tabs' => 'adminarea.pages.tabs',
53
            'id' => "adminarea-pages-{$page->getRouteKey()}-media-table",
54
            'url' => route('adminarea.pages.media.store', ['page' => $page]),
55
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
56
    }
57
58
    /**
59
     * Store new page media.
60
     *
61
     * @param \Cortex\Foundation\Http\Requests\ImageFormRequest $request
62
     * @param \Cortex\Pages\Models\Page                         $page
63
     *
64
     * @return void
65
     */
66
    public function store(ImageFormRequest $request, Page $page): void
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
        $page->addMediaFromRequest('file')
69
             ->sanitizingFileName(function ($fileName) {
70
                 return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
71
             })
72
             ->toMediaCollection('default', config('cortex.pages.media.disk'));
73
    }
74
75
    /**
76
     * Destroy given page media.
77
     *
78
     * @param \Cortex\Pages\Models\Page         $page
79
     * @param \Spatie\MediaLibrary\Models\Media $media
80
     *
81
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
82
     */
83
    public function destroy(Page $page, Media $media)
84
    {
85
        $page->media()->where($media->getKeyName(), $media->getKey())->first()->delete();
86
87
        return intend([
88
            'url' => route('adminarea.pages.media.index', ['page' => $page]),
89
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/foundation::common.media'), 'identifier' => $media->getRouteKey()])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 188 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
90
        ]);
91
    }
92
}
93