Passed
Push — master ( 6da38c...408d25 )
by Andrey
17:24 queued 14:36
created

HasRoles::assignPermission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 48
    public function roles(): BelongsToMany
27
    {
28 48
        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 $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
93
    /**
94
     * @param  int[]  $roles_ids
95
     */
96 48
    public function syncRoles(array $roles_ids): void
97
    {
98 48
        $this->roles()->sync($roles_ids);
99 48
    }
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
            return Search::by($this->roles(), $roles)->exists();
110
        }, $roles);
111
    }
112
113
    /**
114
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
115
     *
116
     * @return bool
117
     */
118 6
    public function hasRoles(...$roles): bool
119
    {
120
        return $this->cache(__FUNCTION__, function () use ($roles) {
121 6
            $count = Search::by($this->roles(), $roles)->count();
122
123 6
            return $count === count($roles);
124 6
        }, $roles);
125
    }
126
127
    /**
128
     * @return \Helldar\Roles\Models\BaseModel|\Illuminate\Database\Eloquent\Relations\BelongsToMany
129
     */
130 48
    public function permissions(): BelongsToMany
131
    {
132 48
        return $this->belongsToMany(Permission::class, 'user_permission');
133
    }
134
135
    public function createPermission(string $name): Model
136
    {
137
        return $this->permissions()->create(compact('name'));
138
    }
139
140
    /**
141
     * @param  \Helldar\Roles\Models\Permission|string  $permission
142
     *
143
     * @throws \Throwable
144
     */
145
    public function assignPermission($permission): void
146
    {
147
        $permission = $this->findPermission($permission);
148
149
        $this->permissions()->attach($permission->id);
150
    }
151
152
    /**
153
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
154
     *
155
     * @throws \Throwable
156
     */
157
    public function assignPermissions(...$permissions): void
158
    {
159
        foreach ($permissions as $permission) {
160
            $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
    }
163
164
    /**
165
     * @param  \Helldar\Roles\Models\Permission|string  $permission
166
     *
167
     * @throws \Throwable
168
     */
169
    public function revokePermission($permission): void
170
    {
171
        $permission = $this->findPermission($permission);
172
173
        $this->permissions()->detach($permission->id);
174
    }
175
176
    /**
177
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
178
     *
179
     * @throws \Throwable
180
     */
181
    public function revokePermissions(...$permissions): void
182
    {
183
        foreach ($permissions as $permission) {
184
            $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
    }
187
188
    /**
189
     * @param  int[]  $permissions_ids
190
     */
191 48
    public function syncPermissions(array $permissions_ids): void
192
    {
193 48
        $this->permissions()->sync($permissions_ids);
194 48
    }
195
196
    /**
197
     * @param  \Helldar\Roles\Models\Permission|string  $permission
198
     *
199
     * @return bool
200
     */
201 6
    public function hasPermission($permission): bool
202
    {
203
        return $this->cache(__FUNCTION__, function () use ($permission) {
204 6
            $first = Search::by($this->permissions(), $permission)->exists();
205
206 6
            $second = $this->roles()
207
                ->whereHas('permissions', function (Builder $builder) use ($permission) {
208 6
                    return Search::by($builder, $permission);
209 6
                })->exists();
210
211 6
            return $first || $second;
212 6
        }, $permission);
213
    }
214
215
    /**
216
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
217
     *
218
     * @return bool
219
     */
220 6
    public function hasPermissions(...$permissions): bool
221
    {
222
        return $this->cache(__FUNCTION__, function () use ($permissions) {
223 6
            $count = Search::by($this->permissions(), $permissions)->count();
224
225 6
            return $count === count($permissions);
226 6
        }, $permissions);
227
    }
228
}
229