Completed
Push — master ( 1875e0...21079a )
by Sebastian
05:51 queued 03:33
created

HasPermissions::getDefaultGuardName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission\Traits;
4
5
use Illuminate\Support\Collection;
6
use Spatie\Permission\Contracts\Permission;
7
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
8
use Spatie\Permission\PermissionRegistrar;
9
10
trait HasPermissions
11
{
12
    /**
13
     * Grant the given permission(s) to a role.
14
     *
15
     * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
16
     *
17
     * @return $this
18
     */
19 View Code Duplication
    public function givePermissionTo(...$permissions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        $permissions = collect($permissions)
22
            ->flatten()
23
            ->map(function ($permission) {
24
                return $this->getStoredPermission($permission);
25
            })
26
            ->each(function ($permission) {
27
                $this->ensureModelSharesGuard($permission);
28
            })
29
            ->all();
30
31
        $this->permissions()->saveMany($permissions);
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
32
33
        $this->forgetCachedPermissions();
34
35
        return $this;
36
    }
37
38
    /**
39
     * Remove all current permissions and set the given ones.
40
     *
41
     * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
42
     *
43
     * @return $this
44
     */
45
    public function syncPermissions(...$permissions)
46
    {
47
        $this->permissions()->detach();
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
48
49
        return $this->givePermissionTo($permissions);
50
    }
51
52
    /**
53
     * Revoke the given permission.
54
     *
55
     * @param \Spatie\Permission\Contracts\Permission|string $permission
56
     *
57
     * @return $this
58
     */
59
    public function revokePermissionTo($permission)
60
    {
61
        $this->permissions()->detach($this->getStoredPermission($permission));
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
63
        $this->forgetCachedPermissions();
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
70
     *
71
     * @return \Spatie\Permission\Contracts\Permission
72
     */
73
    protected function getStoredPermission($permissions): Permission
74
    {
75
        if (is_string($permissions)) {
76
            return app(Permission::class)->findByName($permissions, $this->getDefaultGuardName());
77
        }
78
79
        if (is_array($permissions)) {
80
            return app(Permission::class)
81
                ->whereIn('name', $permissions)
82
                ->whereId('guard_name', $this->getGuardNames())
83
                ->get();
84
        }
85
86
        return $permissions;
87
    }
88
89
    /**
90
     * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission
91
     *
92
     * @throws \Spatie\Permission\Exceptions\GuardMismatch
93
     */
94
    protected function ensureModelSharesGuard($roleOrPermission)
95
    {
96
        if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) {
97
            throw GuardDoesNotMatch::create($roleOrPermission->guard_name, $this->getGuardNames());
98
        }
99
    }
100
101
    protected function getGuardNames(): Collection
102
    {
103
        if ($this->guard_name) {
104
            return collect($this->guard_name);
0 ignored issues
show
Bug introduced by
The property guard_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
105
        }
106
107
        return collect(config('auth.guards'))
108
            ->map(function ($guard) {
109
                return config("auth.providers.{$guard['provider']}.model");
110
            })
111
            ->filter(function ($model) {
112
                return get_class($this) === $model;
113
            })
114
            ->keys();
115
    }
116
117
    protected function getDefaultGuardName(): string
118
    {
119
        $default = config('auth.defaults.guard');
120
121
        return $this->getGuardNames()->first() ?: $default;
122
    }
123
124
    /**
125
     * Forget the cached permissions.
126
     */
127
    public function forgetCachedPermissions()
128
    {
129
        app(PermissionRegistrar::class)->forgetCachedPermissions();
130
    }
131
}
132