PermissionRegistrar   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 84
ccs 15
cts 15
cp 1
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPermissionClass() 0 3 1
A getRoleClass() 0 3 1
A registerPermissions() 0 9 2
A forgetCachedPermissions() 0 3 1
A getPermissions() 0 4 1
A __construct() 0 6 1
1
<?php
2
3
namespace Maklad\Permission;
4
5
use Illuminate\Contracts\Auth\Access\Authorizable;
6
use Illuminate\Contracts\Auth\Access\Gate;
7
use Illuminate\Contracts\Cache\Repository;
8
use Illuminate\Support\Collection;
9
use Maklad\Permission\Contracts\PermissionInterface as Permission;
10
11
/**
12
 * Class PermissionRegistrar
13
 * @package Maklad\Permission
14
 */
15
class PermissionRegistrar
16
{
17
    /** @var \Illuminate\Contracts\Auth\Access\Gate */
18
    protected $gate;
19
20
    /** @var \Illuminate\Contracts\Cache\Repository */
21
    protected $cache;
22
23
    /** @var string */
24
    protected $cacheKey = 'maklad.permission.cache';
25
26 123
    /** @var string */
27
    protected $permissionClass;
28 123
29 123
    /** @var string */
30 123
    protected $roleClass;
31
32
    /**
33
     * PermissionRegistrar constructor.
34
     * @param Gate $gate
35 123
     * @param Repository $cache
36 7
     */
37 123
    public function __construct(Gate $gate, Repository $cache)
38 123
    {
39
        $this->gate = $gate;
40 123
        $this->cache = $cache;
41
        $this->permissionClass = config('permission.models.permission');
42
        $this->roleClass = config('permission.models.role');
43 123
    }
44
45 123
    /**
46 123
     * Register Permissions
47
     *
48
     * @return bool
49
     */
50 123
    public function registerPermissions(): bool
51 123
    {
52 123
        $this->getPermissions()->map(function (Permission $permission) {
53
            $this->gate->define($permission->name, function (Authorizable $user) use ($permission) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface Maklad\Permission\Contracts\PermissionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
54
                return $user->hasPermissionTo($permission) ?: null;
55
            });
56
        });
57
58
        return true;
59
    }
60
61
    /**
62
     * Forget cached permission
63
     */
64
    public function forgetCachedPermissions()
65
    {
66
        $this->cache->forget($this->cacheKey);
67
    }
68
69
    /**
70
     * Get Permissions
71
     *
72
     * @return Collection
73
     */
74
    public function getPermissions(): Collection
75
    {
76
        return $this->cache->remember($this->cacheKey, config('permission.cache_expiration_time'), function () {
77
            return $this->getPermissionClass()->with('roles')->get();
0 ignored issues
show
Bug introduced by
The method with() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            return $this->getPermissionClass()->/** @scrutinizer ignore-call */ with('roles')->get();

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...
78
        });
79
    }
80
81
    /**
82
     * Get Permission class
83
     *
84
     * @return \Illuminate\Foundation\Application|mixed
85
     */
86
    public function getPermissionClass()
87
    {
88
        return app($this->permissionClass);
89
    }
90
91
    /**
92
     * Get Role class
93
     *
94
     * @return \Illuminate\Foundation\Application|mixed
95
     */
96
    public function getRoleClass()
97
    {
98
        return app($this->roleClass);
99
    }
100
}
101