Completed
Push — master ( a07a51...d6976d )
by Mostafa Abd El-Salam
15s
created

PermissionRegistrar::getRoleClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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