Completed
Push — develop ( bdf9f6...524ace )
by Abdelrahman
19:15
created

TenantsMediaController::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Http\Controllers\Adminarea;
6
7
use Spatie\MediaLibrary\Models\Media;
8
use Rinvex\Tenants\Contracts\TenantContract;
9
use Cortex\Foundation\DataTables\MediaDataTable;
10
use Cortex\Foundation\Http\Requests\ImageFormRequest;
11
use Cortex\Foundation\Http\Controllers\AuthorizedController;
12
13
class TenantsMediaController extends AuthorizedController
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $resource = 'tenants';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected $resourceAbilityMap = [
24
        'index' => 'list-media',
25
        'store' => 'create-media',
26
        'delete' => 'delete-media',
27
    ];
28
29
    /**
30
     * Get a listing of the resource media.
31
     *
32
     * @param \Rinvex\Tenants\Contracts\TenantContract $tenant
33
     *
34
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
35
     */
36
    public function index(TenantContract $tenant)
37
    {
38
        return request()->ajax() && request()->wantsJson()
39
            ? app(MediaDataTable::class)->with(['resource' => $tenant])->ajax()
40
            : intend(['url' => route('adminarea.tenants.edit', ['tenant' => $tenant]).'#media-tab']);
41
    }
42
43
    /**
44
     * Store a newly created resource in storage.
45
     *
46
     * @param \Cortex\Foundation\Http\Requests\ImageFormRequest $request
47
     * @param \Rinvex\Tenants\Contracts\TenantContract          $tenant
48
     *
49
     * @return void
50
     */
51
    public function store(ImageFormRequest $request, TenantContract $tenant)
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...
52
    {
53
        $tenant->addMediaFromRequest('file')
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Rinvex\Tenants\Contracts\TenantContract as the method addMediaFromRequest() does only exist in the following implementations of said interface: Cortex\Tenants\Models\Tenant.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
54
               ->sanitizingFileName(function($fileName) { return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION); })
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 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...
55
               ->toMediaCollection('default', config('cortex.tenants.media.disk'));
56
    }
57
58
    /**
59
     * Delete the given resource from storage.
60
     *
61
     * @param \Rinvex\Tenants\Contracts\TenantContract $tenant
62
     * @param \Spatie\MediaLibrary\Models\Media        $media
63
     *
64
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
65
     */
66
    public function delete(TenantContract $tenant, Media $media)
67
    {
68
        $tenant->media()->where('id' , $media->id)->first()->delete();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Rinvex\Tenants\Contracts\TenantContract as the method media() does only exist in the following implementations of said interface: Cortex\Tenants\Models\Tenant.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
69
70
        return intend([
71
            'url' => route('adminarea.tenants.media.index', ['tenant' => $tenant]),
72
            'with' => ['warning' => trans('cortex/tenants::messages.tenant.media_deleted')],
73
        ]);
74
    }
75
}
76