Completed
Push — master ( 40cd73...37f317 )
by Chris
01:33
created

PermissionRegistrar::getPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Contracts\Auth\Access\Gate;
7
use Illuminate\Contracts\Cache\Repository;
8
use Spatie\Permission\Contracts\Permission;
9
use Illuminate\Contracts\Auth\Access\Authorizable;
10
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
11
12
class PermissionRegistrar
13
{
14
    /** @var \Illuminate\Contracts\Auth\Access\Gate */
15
    protected $gate;
16
17
    /** @var \Illuminate\Contracts\Cache\Repository */
18
    protected $cache;
19
20
    /** @var string */
21
    protected $cacheKey = 'spatie.permission.cache';
22
23
    /** @var string */
24
    protected $permissionClass;
25
26
    /** @var string */
27
    protected $roleClass;
28
29
    public function __construct(Gate $gate, Repository $cache)
30
    {
31
        $this->gate = $gate;
32
        $this->cache = $cache;
33
        $this->permissionClass = config('permission.models.permission');
34
        $this->roleClass = config('permission.models.role');
35
    }
36
37
    public function registerPermissions(): bool
38
    {
39
        $this->gate->before(function (Authorizable $user, string $ability) {
40
            try {
41
                if (method_exists($user, 'hasPermissionTo')) {
42
                    return $user->hasPermissionTo($ability) ?: null;
0 ignored issues
show
Bug introduced by
The method hasPermissionTo() does not seem to exist on object<Illuminate\Contra...th\Access\Authorizable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
                }
44
            } catch (PermissionDoesNotExist $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
45
            }
46
        });
47
48
        return true;
49
    }
50
51
    public function forgetCachedPermissions()
52
    {
53
        $this->cache->forget($this->cacheKey);
54
    }
55
56
    public function getPermissions(): Collection
57
    {
58
        $permissionClass = $this->getPermissionClass();
59
60
        return $this->cache->remember($this->cacheKey, config('permission.cache_expiration_time'), function () use ($permissionClass) {
61
            return $permissionClass->with('roles')->get();
62
        });
63
    }
64
65
    public function getPermissionClass()
66
    {
67
        return app($this->permissionClass);
68
    }
69
70
    public function getRoleClass()
71
    {
72
        return app($this->roleClass);
73
    }
74
}
75