HasRoles   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOverridePermissionCacheTags() 0 4 1
A getOverridePermissionCacheKey() 0 8 1
A forgetOverridePermission() 0 7 1
A canOverridePermission() 0 20 2
A hasPermissionToOnModel() 0 29 4
1
<?php
2
3
namespace BBSLab\NovaPermission\Traits;
4
5
use BBSLab\NovaPermission\Contracts\HasAuthorizations;
6
use Illuminate\Support\Str;
7
use Spatie\Permission\PermissionRegistrar;
8
use Spatie\Permission\Traits\HasRoles as BaseTrait;
9
10
trait HasRoles
11
{
12
    use BaseTrait;
13
14
    public function getOverridePermissionCacheTags(): array
15
    {
16
        return ['nova-permission', 'can-override'];
17
    }
18
19
    public function getOverridePermissionCacheKey(): string
20
    {
21
        return implode(':', [
22
            'nova-permission',
23
            'can-override',
24
            'user:'.$this->getKey(),
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
        ]);
26
    }
27
28
    public function forgetOverridePermission(): void
29
    {
30
        /** @var \Illuminate\Contracts\Cache\Factory $cacheManager */
31
        $cacheManager = app('cache');
32
33
        $cacheManager->flush($this->getOverridePermissionCacheKey());
0 ignored issues
show
Bug introduced by
The method flush() does not seem to exist on object<Illuminate\Contracts\Cache\Factory>.

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...
34
    }
35
36
    public function canOverridePermission(): bool
37
    {
38
        /** @var \Illuminate\Contracts\Cache\Factory $cacheManager */
39
        $cacheManager = app('cache');
40
41
        $cache = method_exists($cacheManager->store(), 'tags')
42
            ? $cacheManager->store()->tags($this->getOverridePermissionCacheTags())
0 ignored issues
show
Bug introduced by
The method tags() does not seem to exist on object<Illuminate\Contracts\Cache\Repository>.

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
            : $cacheManager->store();
44
45
        $key = $this->getOverridePermissionCacheKey();
46
47
        return $cache->remember($key, PermissionRegistrar::$cacheExpirationTime, function () {
48
            $guard = config('nova.guard') ?? config('auth.defaults.guard');
49
50
            return $this->roles()
51
                ->where('guard_name', '=', $guard)
52
                ->where('override_permission', '=', true)
53
                ->exists();
54
        });
55
    }
56
57
    public function hasPermissionToOnModel($permission, $model = null, $guardName = null): bool
58
    {
59
        if (empty($model) || ! $model instanceof HasAuthorizations) {
60
            return $this->can($permission);
0 ignored issues
show
Bug introduced by
It seems like can() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
61
        }
62
63
        $key = implode(':', [
64
            'nova-permission',
65
            'authorization',
66
            class_basename($model),
67
            $model->getKey(),
0 ignored issues
show
Bug introduced by
The method getKey() does not seem to exist on object<BBSLab\NovaPermis...acts\HasAuthorizations>.

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...
68
            Str::snake($permission),
69
        ]);
70
71
        /** @var \Illuminate\Contracts\Cache\Factory $cacheManager */
72
        $cacheManager = app('cache');
73
74
        $authorization = $cacheManager->store()
75
            ->remember($key, PermissionRegistrar::$cacheExpirationTime, function () use ($permission, $model, $guardName) {
76
                return $model->authorizations()
77
                    ->where('name', '=', $permission)
78
                    ->where('guard_name', '=', $guardName ?? $this->getDefaultGuardName())
79
                    ->first();
80
            });
81
82
        return ! empty($authorization)
83
            ? $this->hasPermissionTo($authorization)
84
            : $this->can($permission);
0 ignored issues
show
Bug introduced by
It seems like can() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
85
    }
86
}
87