RoleAccess   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 19
dl 0
loc 83
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A detachPermissions() 0 4 2
A attachPermissions() 0 4 2
A detachPermission() 0 11 3
A attachPermission() 0 11 3
A savePermissions() 0 6 2
1
<?php
2
3
namespace App\Models\Access\Role\Traits;
4
5
/**
6
 * Class RoleAccess.
7
 */
8
trait RoleAccess
9
{
10
    /**
11
     * Save the inputted permissions.
12
     *
13
     * @param mixed $inputPermissions
14
     *
15
     * @return void
16
     */
17
    public function savePermissions($inputPermissions)
18
    {
19
        if (! empty($inputPermissions)) {
20
            $this->permissions()->sync($inputPermissions);
0 ignored issues
show
Bug introduced by
It seems like permissions() 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

20
            $this->/** @scrutinizer ignore-call */ 
21
                   permissions()->sync($inputPermissions);
Loading history...
21
        } else {
22
            $this->permissions()->detach();
23
        }
24
    }
25
26
    /**
27
     * Attach permission to current role.
28
     *
29
     * @param object|array $permission
30
     *
31
     * @return void
32
     */
33
    public function attachPermission($permission)
34
    {
35
        if (is_object($permission)) {
36
            $permission = $permission->getKey();
37
        }
38
39
        if (is_array($permission)) {
40
            $permission = $permission['id'];
41
        }
42
43
        $this->permissions()->attach($permission);
44
    }
45
46
    /**
47
     * Detach permission form current role.
48
     *
49
     * @param object|array $permission
50
     *
51
     * @return void
52
     */
53
    public function detachPermission($permission)
54
    {
55
        if (is_object($permission)) {
56
            $permission = $permission->getKey();
57
        }
58
59
        if (is_array($permission)) {
60
            $permission = $permission['id'];
61
        }
62
63
        $this->permissions()->detach($permission);
64
    }
65
66
    /**
67
     * Attach multiple permissions to current role.
68
     *
69
     * @param mixed $permissions
70
     *
71
     * @return void
72
     */
73
    public function attachPermissions($permissions)
74
    {
75
        foreach ($permissions as $permission) {
76
            $this->attachPermission($permission);
77
        }
78
    }
79
80
    /**
81
     * Detach multiple permissions from current role.
82
     *
83
     * @param mixed $permissions
84
     *
85
     * @return void
86
     */
87
    public function detachPermissions($permissions)
88
    {
89
        foreach ($permissions as $permission) {
90
            $this->detachPermission($permission);
91
        }
92
    }
93
}
94