Completed
Pull Request — develop (#48)
by
unknown
01:11
created

TenantsMediaController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Http\Controllers\Managerarea;
6
7
use Cortex\Tenants\Models\Tenant;
8
use Spatie\MediaLibrary\Models\Media;
9
use Cortex\Foundation\Http\Controllers\AuthenticatedController;
10
11
class TenantsMediaController extends AuthenticatedController
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    protected $resource = Tenant::class;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function authorizeResource($model, $parameter = null, array $options = [], $request = null): void
22
    {
23
        $middleware = [];
24
        $parameter = $parameter ?: snake_case(class_basename($model));
0 ignored issues
show
Deprecated Code introduced by
The function snake_case() has been deprecated with message: Str::snake() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
25
26
        foreach ($this->mapResourceAbilities() as $method => $ability) {
0 ignored issues
show
Documentation Bug introduced by
The method mapResourceAbilities does not exist on object<Cortex\Tenants\Ht...TenantsMediaController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
27
            $modelName = in_array($method, $this->resourceMethodsWithoutModels()) ? $model : $parameter;
28
29
            $middleware["can:update,{$modelName}"][] = $method;
30
            $middleware["can:{$ability},media"][] = $method;
31
        }
32
33
        foreach ($middleware as $middlewareName => $methods) {
34
            $this->middleware($middlewareName, $options)->only($methods);
35
        }
36
    }
37
    /**
38
     * Destroy given tenant media.
39
     *
40
     * @param \Spatie\MediaLibrary\Models\Media $media
41
     *
42
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
43
     */
44
    public function destroy(Media $media)
45
    {
46
        $tenant = config('rinvex.tenants.active');
47
        $tenant->media()->where($media->getKeyName(), $media->getKey())->first()->delete();
48
49
        return intend([
50
            'back' => true,
51
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/foundation::common.media'), 'identifier' => $media->getRouteKey()])],
52
        ]);
53
    }
54
}
55