Completed
Push — master ( 85d653...679971 )
by Arjay
12:36
created

GateRegistrar   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A register() 0 16 2
A getPermissions() 0 12 2
1
<?php
2
3
namespace Yajra\Acl;
4
5
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
6
use Illuminate\Contracts\Cache\Repository;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
use Yajra\Acl\Models\Permission;
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 $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
        try {
63
            return $this->cache->rememberForever('permissions.policies', function () {
64
                return Permission::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...
65
            });
66
        } catch (\Exception $exception) {
67
            $this->cache->forget('permissions.policies');
68
69
            return new Collection;
70
        }
71
    }
72
}
73