Completed
Push — master ( 876e58...7fc0d0 )
by Arjay
18:23 queued 04:39
created

GateRegistrar::getPermissionClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Yajra\Acl;
4
5
use Illuminate\Support\Str;
6
use Yajra\Acl\Models\Permission;
7
use Illuminate\Support\Collection;
8
use Illuminate\Contracts\Cache\Repository;
9
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
10
11
class GateRegistrar
12
{
13
    /**
14
     * @var GateContract
15
     */
16
    private $gate;
17
18
    /**
19
     * @var Repository
20
     */
21
    private $cache;
22
23
    /**
24
     * GateRegistrar constructor.
25
     *
26
     * @param GateContract $gate
27
     * @param Repository $cache
28
     */
29
    public function __construct(GateContract $gate, Repository $cache)
30
    {
31
        $this->gate  = $gate;
32
        $this->cache = $cache;
33
    }
34
35
    /**
36
     * Handle permission gate registration.
37
     */
38
    public function register()
39
    {
40
        $this->getPermissions()->each(function ($permission) {
41
            $ability = $permission->slug;
42
            $policy  = function ($user) use ($permission) {
43
                return $user->hasRole($permission->roles);
44
            };
45
46
            if (Str::contains($permission->slug, '@')) {
47
                $policy  = $permission->slug;
48
                $ability = $permission->name;
49
            }
50
51
            $this->gate->define($ability, $policy);
52
        });
53
    }
54
55
    /**
56
     * Get all permissions.
57
     *
58
     * @return Collection
59
     */
60
    protected function getPermissions()
61
    {
62
        $key = config('acl.cache.key', 'permissions.policies');
63
        try {
64
            if (config('acl.cache.enabled', true)) {
65
                return $this->cache->rememberForever($key, function () {
66
                    return $this->getPermissionClass()->with('roles')->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
                });
68
            } else {
69
                return $this->getPermissionClass()->with('roles')->get();
70
            }
71
        } catch (\Exception $exception) {
72
            $this->cache->forget($key);
73
74
            return new Collection;
75
        }
76
    }
77
78
    /**
79
     * @return Permission
80
     */
81
    protected function getPermissionClass()
82
    {
83
        return resolve(config('acl.permission', Permission::class));
84
    }
85
}
86