Passed
Branch v1.3 (5b46b6)
by Mostafa Abd El-Salam
02:58
created

HasPermissions::convertToPermissionModels()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Maklad\Permission\Traits;
5
6
use Illuminate\Support\Collection;
7
use Jenssegers\Mongodb\Eloquent\Model;
8
use Maklad\Permission\Contracts\PermissionInterface as Permission;
9
use Maklad\Permission\Exceptions\GuardDoesNotMatch;
10
use Maklad\Permission\Helpers;
11
use Maklad\Permission\PermissionRegistrar;
12
13
/**
14
 * Trait HasPermissions
15
 * @package Maklad\Permission\Traits
16
 */
17
trait HasPermissions
18
{
19
    /**
20
     * Grant the given permission(s) to a role.
21
     *
22
     * @param string|array|Permission|\Illuminate\Support\Collection $permissions
23
     *
24
     * @return $this
25
     * @throws GuardDoesNotMatch
26
     */
27 39 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...
28
    {
29 39
        $permissions = \collect($permissions)
30 39
            ->flatten()
31 39
            ->map(function ($permission) {
32 39
                return $this->getStoredPermission($permission);
33 39
            })
34 37
            ->each(function ($permission) {
35 37
                $this->ensureModelSharesGuard($permission);
36 37
            })
37 34
            ->all();
38
39 34
        $this->permissions()->saveMany($permissions);
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Maklad\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...
40
41 34
        $this->forgetCachedPermissions();
42
43 34
        return $this;
44
    }
45
46
    /**
47
     * Remove all current permissions and set the given ones.
48
     *
49
     * @param string|array|Permission|\Illuminate\Support\Collection $permissions
50
     *
51
     * @return $this
52
     * @throws GuardDoesNotMatch
53
     */
54 4
    public function syncPermissions(...$permissions)
55
    {
56 4
        $this->permissions()->detach();
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Maklad\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...
57
58 4
        return $this->givePermissionTo($permissions);
59
    }
60
61
    /**
62
     * Revoke the given permission.
63
     *
64
     * @param Permission|string $permission
65
     *
66
     * @return $this
67
     */
68 5
    public function revokePermissionTo($permission)
69
    {
70 5
        $this->permissions()->detach($this->getStoredPermission($permission));
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Maklad\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...
71
72 5
        $this->forgetCachedPermissions();
73
74 5
        return $this;
75
    }
76
77
    /**
78
     * @param string|Permission $permissions
79
     *
80
     * @return Permission
81
     */
82 40
    protected function getStoredPermission($permissions): Permission
83
    {
84 40
        if (\is_string($permissions)) {
85 28
            return \app(Permission::class)->findByName($permissions, $this->getDefaultGuardName());
86
        }
87
88 15
        return $permissions;
89
    }
90
91
    /**
92
     * @param Model $roleOrPermission
93
     *
94
     * @throws GuardDoesNotMatch
95
     */
96 73
    protected function ensureModelSharesGuard(Model $roleOrPermission)
97
    {
98 73 View Code Duplication
        if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
99 5
            $expected = $this->getGuardNames();
100 5
            $given = $roleOrPermission->guard_name;
101 5
            $helpers = new Helpers();
102
103 5
            throw new GuardDoesNotMatch($helpers->getGuardDoesNotMatchMessage($expected, $given));
104
        }
105 69
    }
106
107 87
    protected function getGuardNames(): Collection
108
    {
109 87
        if ($this->guard_name) {
110 27
            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...
111
        }
112
113 71
        return \collect(\config('auth.guards'))
114 71
            ->map(function ($guard) {
115 71
                return \config("auth.providers.{$guard['provider']}.model");
116 71
            })
117 71
            ->filter(function ($model) {
118 71
                return \get_class($this) === $model;
119 71
            })
120 71
            ->keys();
121
    }
122
123 68
    protected function getDefaultGuardName(): string
124
    {
125 68
        $default = \config('auth.defaults.guard');
126
127 68
        return $this->getGuardNames()->first() ?: $default;
128
    }
129
130
    /**
131
     * Forget the cached permissions.
132
     */
133 68
    public function forgetCachedPermissions()
134
    {
135 68
        \app(PermissionRegistrar::class)->forgetCachedPermissions();
136 68
    }
137
138
    /**
139
     * Convert to Permission Models
140
     *
141
     * @param string|array|Collection $permissions
142
     *
143
     * @return Collection
144
     */
145 7 View Code Duplication
    private function convertToPermissionModels($permissions): Collection
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...
146
    {
147 7
        if (\is_array($permissions)) {
148 3
            $permissions = \collect($permissions);
149
        }
150
151 7
        if (! $permissions instanceof Collection) {
152 5
            $permissions = \collect([$permissions]);
153
        }
154
155 7
        $permissions = $permissions->map(function ($permission) {
156 7
            return $this->getStoredPermission($permission);
157 7
        });
158
159
        return $permissions;
160
    }
161
}
162