RoleHasRelations::detachAllPermissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ultraware\Roles\Traits;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
use Ultraware\Roles\Models\Permission;
8
9
trait RoleHasRelations
10
{
11
    /**
12
     * Role belongs to many permissions.
13
     *
14
     * @return BelongsToMany
15
     */
16
    public function permissions()
17
    {
18
        return $this->belongsToMany(config('roles.models.permission'))->withTimestamps();
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
19
    }
20
21
    /**
22
     * Role belongs to many users.
23
     *
24
     * @return BelongsToMany
25
     */
26
    public function users()
27
    {
28
        return $this->belongsToMany(config('auth.providers.users.model'))->withTimestamps();
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
    }
30
31
    /**
32
     * Attach permission to a role.
33
     *
34
     * @param int|Permission $permission
35
     * @return int|bool
36
     */
37
    public function attachPermission($permission)
38
    {
39
        return (!$this->permissions()->get()->contains($permission)) ? $this->permissions()->attach($permission) : true;
40
    }
41
42
    /**
43
     * Detach permission from a role.
44
     *
45
     * @param int|Permission $permission
46
     * @return int
47
     */
48
    public function detachPermission($permission)
49
    {
50
        return $this->permissions()->detach($permission);
51
    }
52
53
    /**
54
     * Detach all permissions.
55
     *
56
     * @return int
57
     */
58
    public function detachAllPermissions()
59
    {
60
        return $this->permissions()->detach();
61
    }
62
63
    /**
64
     * Sync permissions for a role.
65
     *
66
     * @param array|Permission[]|Collection $permissions
67
     * @return array
68
     */
69
    public function syncPermissions($permissions)
70
    {
71
        return $this->permissions()->sync($permissions);
72
    }
73
}
74