Completed
Push — develop ( d15310...a763d0 )
by Abdelrahman
11:20 queued 09:46
created

TenantsMediaController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
lcom 2
cbo 6
dl 0
loc 81
rs 10
c 2
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B authorizeResource() 0 16 5
A index() 0 10 1
A store() 0 8 1
A destroy() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Http\Controllers\Adminarea;
6
7
use Illuminate\Support\Str;
8
use Rinvex\Tenants\Models\Tenant;
9
use Spatie\MediaLibrary\Models\Media;
10
use Cortex\Foundation\DataTables\MediaDataTable;
11
use Cortex\Foundation\Http\Requests\ImageFormRequest;
12
use Cortex\Foundation\Http\Controllers\AuthorizedController;
13
14
class TenantsMediaController extends AuthorizedController
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected $resource = 'tenant';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function authorizeResource($model, $parameter = null, array $options = [], $request = null): void
25
    {
26
        $middleware = [];
27
        $parameter = $parameter ?: Str::snake(class_basename($model));
28
29
        foreach ($this->mapResourceAbilities() as $method => $ability) {
30
            $modelName = in_array($method, $this->resourceMethodsWithoutModels()) ? $model : $parameter;
31
32
            $middleware["can:update,{$modelName}"][] = $method;
33
            $middleware["can:{$ability},media"][] = $method;
34
        }
35
36
        foreach ($middleware as $middlewareName => $methods) {
37
            $this->middleware($middlewareName, $options)->only($methods);
38
        }
39
    }
40
41
    /**
42
     * List tenant media.
43
     *
44
     * @param \Cortex\Tenants\Models\Tenant                $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
45
     * @param \Cortex\Foundation\DataTables\MediaDataTable $mediaDataTable
46
     *
47
     * @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...
48
     */
49
    public function index(Tenant $tenant, MediaDataTable $mediaDataTable)
50
    {
51
        return $mediaDataTable->with([
52
            'resource' => $tenant,
53
            'tabs' => 'adminarea.tenants.tabs',
54
            'phrase' => trans('cortex/tenants::common.tenants'),
55
            'id' => "adminarea-tenants-{$tenant->getKey()}-media-table",
56
            'url' => route('adminarea.tenants.media.store', ['tenant' => $tenant]),
57
        ])->render('cortex/foundation::adminarea.pages.datatable-media');
58
    }
59
60
    /**
61
     * Store new tenant media.
62
     *
63
     * @param \Cortex\Foundation\Http\Requests\ImageFormRequest $request
64
     * @param \Cortex\Tenants\Models\Tenant                     $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
65
     *
66
     * @return void
67
     */
68
    public function store(ImageFormRequest $request, Tenant $tenant): 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...
69
    {
70
        $tenant->addMediaFromRequest('file')
71
               ->sanitizingFileName(function ($fileName) {
72
                   return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
73
               })
74
               ->toMediaCollection('default', config('cortex.tenants.media.disk'));
75
    }
76
77
    /**
78
     * Destroy given tenant media.
79
     *
80
     * @param \Cortex\Tenants\Models\Tenant     $tenant
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $tenant a bit more specific; maybe use Tenant.
Loading history...
81
     * @param \Spatie\MediaLibrary\Models\Media $media
82
     *
83
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
84
     */
85
    public function destroy(Tenant $tenant, Media $media)
86
    {
87
        $tenant->media()->where($media->getKeyName(), $media->getKey())->first()->delete();
88
89
        return intend([
90
            'url' => route('adminarea.tenants.media.index', ['tenant' => $tenant]),
91
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'media', 'id' => $media->getKey()])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 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...
92
        ]);
93
    }
94
}
95