Passed
Push — master ( 522ba7...dac3f8 )
by Andrey
13:44
created

HasRoles::assignPermissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Helldar\Roles\Traits;
4
5
use Helldar\Roles\Models\Permission;
6
use Helldar\Roles\Models\Role;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
10
use Illuminate\Support\Arr;
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 24
23
    /**
24
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany|\Helldar\Roles\Models\BaseModel
25 24
     */
26 24
    public function roles(): BelongsToMany
27 24
    {
28 24
        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 42
    public function hasRootRole(): bool
32
    {
33 42
        return $this->cache(__FUNCTION__, function () {
34
            return $this->roles()
35
                ->where('is_root', true)
36
                ->exists();
37
        });
38
    }
39
40
    public function createRole(string $name): Model
41
    {
42
        return $this->roles()->create(compact('name'));
43
    }
44
45
    /**
46
     * @param  \Helldar\Roles\Models\Role|string  $role
47
     *
48
     * @throws \Throwable
49
     */
50
    public function assignRole($role): void
51
    {
52
        $role = $this->findRole($role);
53
54
        $this->roles()->attach($role->id);
55
    }
56
57
    /**
58
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
59
     *
60
     * @throws \Throwable
61
     */
62
    public function assignRoles(...$roles): void
63
    {
64
        foreach ($roles as $role) {
65
            $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
    }
68
69
    /**
70
     * @param  \Helldar\Roles\Models\Role|string  $role
71
     *
72
     * @throws \Throwable
73
     */
74
    public function revokeRole($role): void
75
    {
76
        $role = $this->findRole($role);
77
78
        $this->roles()->detach($role->id);
79
    }
80
81
    /**
82
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
83
     *
84
     * @throws \Throwable
85
     */
86
    public function revokeRoles(...$roles): void
87
    {
88
        foreach ($roles as $role) {
89
            $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
    }
92 42
93
    /**
94 42
     * @param  int[]  $roles_ids
95 42
     */
96
    public function syncRoles(array $roles_ids): void
97
    {
98
        $this->roles()->sync($roles_ids);
99
    }
100
101
    /**
102
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
103
     *
104
     * @return bool
105
     */
106
    public function hasRole(...$roles): bool
107
    {
108
        return $this->cache(__FUNCTION__, function () use ($roles) {
109
            foreach (Arr::flatten($roles) as $role) {
110
                if ($this->roles->contains('name', $role)) {
111
                    return true;
112
                }
113
            }
114
115
            return false;
116
        }, $roles);
117
    }
118
119
    /**
120 6
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
121
     *
122
     * @return bool
123 6
     */
124 6
    public function hasRoles(...$roles): bool
125 6
    {
126
        return $this->cache(__FUNCTION__, function () use ($roles) {
127
            foreach (Arr::flatten($roles) as $role) {
128
                if (! $this->roles->contains('name', $role)) {
129
                    return false;
130 6
                }
131
            }
132
133
            return true;
134
        }, $roles);
135
    }
136
137
    /**
138 6
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany|\Helldar\Roles\Models\BaseModel
139
     */
140
    public function permissions(): BelongsToMany
141 6
    {
142
        return $this->belongsToMany(Permission::class, 'user_permission');
143 6
    }
144
145
    public function createPermission(string $name): Model
146 6
    {
147 6
        return $this->permissions()->create(compact('name'));
148 6
    }
149 6
150 6
    /**
151
     * @param  \Helldar\Roles\Models\Permission|string  $permission
152
     *
153
     * @throws \Throwable
154
     */
155
    public function assignPermission($permission): void
156
    {
157
        $permission = $this->findPermission($permission);
158
159
        $this->permissions()->attach($permission->id);
160
    }
161
162
    /**
163
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
164
     *
165
     * @throws \Throwable
166
     */
167
    public function assignPermissions(...$permissions): void
168
    {
169
        foreach ($permissions as $permission) {
170
            $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

170
            $this->assignPermission(/** @scrutinizer ignore-type */ $permission);
Loading history...
171
        }
172
    }
173
174
    /**
175
     * @param  \Helldar\Roles\Models\Permission|string  $permission
176 6
     *
177
     * @throws \Throwable
178 6
     */
179
    public function revokePermission($permission): void
180 6
    {
181
        $permission = $this->findPermission($permission);
182
183
        $this->permissions()->detach($permission->id);
184
    }
185
186
    /**
187
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
188
     *
189
     * @throws \Throwable
190
     */
191
    public function revokePermissions(...$permissions): void
192
    {
193
        foreach ($permissions as $permission) {
194
            $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

194
            $this->revokePermission(/** @scrutinizer ignore-type */ $permission);
Loading history...
195
        }
196
    }
197
198
    /**
199
     * @param  int[]  $permissions_ids
200
     */
201
    public function syncPermissions(array $permissions_ids): void
202
    {
203
        $this->permissions()->sync($permissions_ids);
204
    }
205
206
    /**
207
     * @param  \Helldar\Roles\Models\Permission|string  $permission
208
     *
209
     * @return bool
210
     */
211
    public function hasPermission($permission): bool
212
    {
213
        return $this->cache(__FUNCTION__, function () use ($permission) {
214
            $permission = $this->permissionId($permission);
215
216
            return $this->permissions()
217
                    ->searchBy($permission)
218
                    ->exists()
219
                ||
220
                $this->roles()
221
                    ->whereHas('permissions', function (Builder $builder) use ($permission) {
222
                        $builder->searchBy($permission);
223
                    })->exists();
224
        }, $permission);
225
    }
226
227
    /**
228
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
229
     *
230
     * @return bool
231
     */
232
    public function hasPermissions(...$permissions): bool
233
    {
234
        return $this->cache(__FUNCTION__, function () use ($permissions) {
235
            foreach (Arr::flatten($permissions) as $permission) {
236
                if (! $this->hasPermission($permission)) {
237
                    return false;
238
                }
239
            }
240
241
            return true;
242
        }, $permissions);
243
    }
244
245
    /**
246
     * @param  \Helldar\Roles\Models\Permission|string  $permission
247
     *
248
     * @return int|string
249
     */
250
    protected function permissionId($permission)
251
    {
252
        return $permission instanceof Permission
253
            ? $permission->id
254
            : $permission;
255
    }
256
}
257