Passed
Push — master ( c89bf9...5eeb6f )
by Andrey
03:48
created

HasRoles::assignDefaultRole()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Helldar\Roles\Traits;
4
5
use Helldar\Roles\Facades\Config;
6
use Helldar\Roles\Facades\Database\Search;
7
use Helldar\Roles\Models\Permission;
8
use Helldar\Roles\Models\Role;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
12
13
/**
14
 * @property \Helldar\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles
15
 * @property \Helldar\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions
16
 *
17
 * @mixin \Illuminate\Database\Eloquent\Model
18
 */
19
trait HasRoles
20
{
21
    use Searchable;
22
    use Cacheable;
23
24
    /**
25
     * @return \Helldar\Roles\Models\BaseModel|\Illuminate\Database\Eloquent\Relations\BelongsToMany
26
     */
27 114
    public function roles(): BelongsToMany
28
    {
29 114
        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

29
        return $this->/** @scrutinizer ignore-call */ belongsToMany(Role::class, 'user_role');
Loading history...
30
    }
31
32 30
    public function hasRootRole(): bool
33
    {
34
        return $this->cache(__FUNCTION__, function () {
35 30
            return $this->roles()
36 30
                ->where('is_root', true)
37 30
                ->exists();
38 30
        });
39
    }
40
41
    public function createRole(string $slug, string $title = null, bool $is_root = false): Model
42
    {
43
        return $this->roles()->create(compact('slug', 'title', 'is_root'));
44
    }
45
46
    /**
47
     * @param  \Helldar\Roles\Models\Role|string  $role
48
     *
49
     * @throws \Throwable
50
     */
51 24
    public function assignRole($role): void
52
    {
53 24
        $role = $this->findRole($role);
54
55 24
        $this->roles()->attach($role->id);
56 24
    }
57
58
    /**
59
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
60
     *
61
     * @throws \Throwable
62
     */
63 12
    public function assignRoles(...$roles): void
64
    {
65 12
        foreach ($roles as $role) {
66 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

66
            $this->assignRole(/** @scrutinizer ignore-type */ $role);
Loading history...
67
        }
68 12
    }
69
70
    /**
71
     * @throws \Throwable
72
     */
73 6
    public function assignDefaultRole(): void
74
    {
75 6
        if ($role = Config::defaultRole()) {
76 6
            $this->assignRole($role);
77
        }
78 6
    }
79
80
    /**
81
     * @param  \Helldar\Roles\Models\Role|string  $role
82
     *
83
     * @throws \Throwable
84
     */
85 6
    public function revokeRole($role): void
86
    {
87 6
        $role = $this->findRole($role);
88
89 6
        $this->roles()->detach($role->id);
90 6
    }
91
92
    /**
93
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
94
     *
95
     * @throws \Throwable
96
     */
97 6
    public function revokeRoles(...$roles): void
98
    {
99 6
        foreach ($roles as $role) {
100 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

100
            $this->revokeRole(/** @scrutinizer ignore-type */ $role);
Loading history...
101
        }
102 6
    }
103
104
    /**
105
     * @param  int[]  $roles_ids
106
     */
107 114
    public function syncRoles(array $roles_ids): void
108
    {
109 114
        $this->roles()->sync($roles_ids);
110 114
    }
111
112
    /**
113
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
114
     *
115
     * @return bool
116
     */
117 24
    public function hasRole(...$roles): bool
118
    {
119
        return $this->cache(__FUNCTION__, function () use ($roles) {
120 24
            return Search::by($this->roles(), $roles)->exists();
121 24
        }, $roles);
122
    }
123
124
    /**
125
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
126
     *
127
     * @return bool
128
     */
129 24
    public function hasRoles(...$roles): bool
130
    {
131
        return $this->cache(__FUNCTION__, function () use ($roles) {
132 24
            $count = Search::by($this->roles(), $roles)->count();
133
134 24
            return $count === count($roles);
135 24
        }, $roles);
136
    }
137
138
    /**
139
     * @return \Helldar\Roles\Models\BaseModel|\Illuminate\Database\Eloquent\Relations\BelongsToMany
140
     */
141 114
    public function permissions(): BelongsToMany
142
    {
143 114
        return $this->belongsToMany(Permission::class, 'user_permission');
144
    }
145
146
    public function createPermission(string $slug, string $title = null): Model
147
    {
148
        return $this->permissions()->create(compact('slug', 'title'));
149
    }
150
151
    /**
152
     * @param  \Helldar\Roles\Models\Permission|string  $permission
153
     *
154
     * @throws \Throwable
155
     */
156 18
    public function assignPermission($permission): void
157
    {
158 18
        $permission = $this->findPermission($permission);
159
160 18
        $this->permissions()->attach($permission->id);
161 18
    }
162
163
    /**
164
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
165
     *
166
     * @throws \Throwable
167
     */
168 12
    public function assignPermissions(...$permissions): void
169
    {
170 12
        foreach ($permissions as $permission) {
171 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

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

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