Passed
Push — master ( 9de83d...7f6306 )
by Andrey
33:39 queued 31:07
created

HasRoles::assignPermission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Helldar\Roles\Traits;
4
5
use Helldar\Roles\Facades\Database\Search;
6
use Helldar\Roles\Models\Permission;
7
use Helldar\Roles\Models\Role;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
11
12
/**
13
 * @property \Helldar\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles
14
 * @property \Helldar\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions
15
 *
16
 * @mixin \Illuminate\Database\Eloquent\Model
17
 */
18
trait HasRoles
19
{
20
    use Searchable;
21
    use Cacheable;
22
23
    /**
24
     * @return \Helldar\Roles\Models\BaseModel|\Illuminate\Database\Eloquent\Relations\BelongsToMany
25
     */
26 108
    public function roles(): BelongsToMany
27
    {
28 108
        return $this->belongsToMany(Role::class, 'user_role');
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

28
        return $this->/** @scrutinizer ignore-call */ belongsToMany(Role::class, 'user_role');
Loading history...
29
    }
30
31 30
    public function hasRootRole(): bool
32
    {
33
        return $this->cache(__FUNCTION__, function () {
34 30
            return $this->roles()
35 30
                ->where('is_root', true)
36 30
                ->exists();
37 30
        });
38
    }
39
40
    public function createRole(string $slug): Model
41
    {
42
        return $this->roles()->create(compact('slug'));
43
    }
44
45
    /**
46
     * @param  \Helldar\Roles\Models\Role|string  $role
47
     *
48
     * @throws \Throwable
49
     */
50 18
    public function assignRole($role): void
51
    {
52 18
        $role = $this->findRole($role);
53
54 18
        $this->roles()->attach($role->id);
55 18
    }
56
57
    /**
58
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
59
     *
60
     * @throws \Throwable
61
     */
62 12
    public function assignRoles(...$roles): void
63
    {
64 12
        foreach ($roles as $role) {
65 12
            $this->assignRole($role);
0 ignored issues
show
Bug introduced by
$role of type Helldar\Roles\Models\Role[]|string[] is incompatible with the type Helldar\Roles\Models\Role|string expected by parameter $role of Helldar\Roles\Traits\HasRoles::assignRole(). ( Ignorable by Annotation )

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

65
            $this->assignRole(/** @scrutinizer ignore-type */ $role);
Loading history...
66
        }
67 12
    }
68
69
    /**
70
     * @param  \Helldar\Roles\Models\Role|string  $role
71
     *
72
     * @throws \Throwable
73
     */
74 6
    public function revokeRole($role): void
75
    {
76 6
        $role = $this->findRole($role);
77
78 6
        $this->roles()->detach($role->id);
79 6
    }
80
81
    /**
82
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
83
     *
84
     * @throws \Throwable
85
     */
86 6
    public function revokeRoles(...$roles): void
87
    {
88 6
        foreach ($roles as $role) {
89 6
            $this->revokeRole($role);
0 ignored issues
show
Bug introduced by
$role of type Helldar\Roles\Models\Role[]|string[] is incompatible with the type Helldar\Roles\Models\Role|string expected by parameter $role of Helldar\Roles\Traits\HasRoles::revokeRole(). ( Ignorable by Annotation )

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

89
            $this->revokeRole(/** @scrutinizer ignore-type */ $role);
Loading history...
90
        }
91 6
    }
92
93
    /**
94
     * @param  int[]  $roles_ids
95
     */
96 108
    public function syncRoles(array $roles_ids): void
97
    {
98 108
        $this->roles()->sync($roles_ids);
99 108
    }
100
101
    /**
102
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
103
     *
104
     * @return bool
105
     */
106 18
    public function hasRole(...$roles): bool
107
    {
108
        return $this->cache(__FUNCTION__, function () use ($roles) {
109 18
            return Search::by($this->roles(), $roles)->exists();
110 18
        }, $roles);
111
    }
112
113
    /**
114
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
115
     *
116
     * @return bool
117
     */
118 24
    public function hasRoles(...$roles): bool
119
    {
120
        return $this->cache(__FUNCTION__, function () use ($roles) {
121 24
            $count = Search::by($this->roles(), $roles)->count();
122
123 24
            return $count === count($roles);
124 24
        }, $roles);
125
    }
126
127
    /**
128
     * @return \Helldar\Roles\Models\BaseModel|\Illuminate\Database\Eloquent\Relations\BelongsToMany
129
     */
130 108
    public function permissions(): BelongsToMany
131
    {
132 108
        return $this->belongsToMany(Permission::class, 'user_permission');
133
    }
134
135
    public function createPermission(string $slug): Model
136
    {
137
        return $this->permissions()->create(compact('slug'));
138
    }
139
140
    /**
141
     * @param  \Helldar\Roles\Models\Permission|string  $permission
142
     *
143
     * @throws \Throwable
144
     */
145 18
    public function assignPermission($permission): void
146
    {
147 18
        $permission = $this->findPermission($permission);
148
149 18
        $this->permissions()->attach($permission->id);
150 18
    }
151
152
    /**
153
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
154
     *
155
     * @throws \Throwable
156
     */
157 12
    public function assignPermissions(...$permissions): void
158
    {
159 12
        foreach ($permissions as $permission) {
160 12
            $this->assignPermission($permission);
0 ignored issues
show
Bug introduced by
$permission of type Helldar\Roles\Models\Permission[]|string[] is incompatible with the type Helldar\Roles\Models\Permission|string expected by parameter $permission of Helldar\Roles\Traits\HasRoles::assignPermission(). ( Ignorable by Annotation )

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

160
            $this->assignPermission(/** @scrutinizer ignore-type */ $permission);
Loading history...
161
        }
162 12
    }
163
164
    /**
165
     * @param  \Helldar\Roles\Models\Permission|string  $permission
166
     *
167
     * @throws \Throwable
168
     */
169 6
    public function revokePermission($permission): void
170
    {
171 6
        $permission = $this->findPermission($permission);
172
173 6
        $this->permissions()->detach($permission->id);
174 6
    }
175
176
    /**
177
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
178
     *
179
     * @throws \Throwable
180
     */
181 6
    public function revokePermissions(...$permissions): void
182
    {
183 6
        foreach ($permissions as $permission) {
184 6
            $this->revokePermission($permission);
0 ignored issues
show
Bug introduced by
$permission of type Helldar\Roles\Models\Permission[]|string[] is incompatible with the type Helldar\Roles\Models\Permission|string expected by parameter $permission of Helldar\Roles\Traits\HasRoles::revokePermission(). ( Ignorable by Annotation )

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

184
            $this->revokePermission(/** @scrutinizer ignore-type */ $permission);
Loading history...
185
        }
186 6
    }
187
188
    /**
189
     * @param  int[]  $permissions_ids
190
     */
191 108
    public function syncPermissions(array $permissions_ids): void
192
    {
193 108
        $this->permissions()->sync($permissions_ids);
194 108
    }
195
196
    /**
197
     * @param  \Helldar\Roles\Models\Permission[]|int[]|string[]  $permission
198
     *
199
     * @return bool
200
     */
201 24
    public function hasPermission(...$permission): bool
202
    {
203
        return $this->cache(__FUNCTION__, function () use ($permission) {
204 24
            $first = Search::by($this->permissions(), $permission)->exists();
205
206 24
            $second = $this->roles()
207
                ->whereHas('permissions', function (Builder $builder) use ($permission) {
208 24
                    return Search::by($builder, $permission);
209 24
                })->exists();
210
211 24
            return $first || $second;
212 24
        }, $permission);
213
    }
214
215
    /**
216
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
217
     *
218
     * @return bool
219
     */
220 24
    public function hasPermissions(...$permissions): bool
221
    {
222
        return $this->cache(__FUNCTION__, function () use ($permissions) {
223 24
            $count = Search::by($this->permissions(), $permissions)->count();
224
225 24
            return $count === count($permissions);
226 24
        }, $permissions);
227
    }
228
}
229