Completed
Push — master ( e0683b...eed8e4 )
by ARCANEDEV
09:48 queued 08:04
created

AuthRoleTrait::detachRole()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php namespace Arcanedev\LaravelAuth\Models\Traits;
2
3
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract;
4
use Illuminate\Database\Eloquent\Model as Eloquent;
5
6
/**
7
 * Trait     AuthRoleTrait
8
 *
9
 * @package  Arcanedev\LaravelAuth\Traits
10
 * @author   ARCANEDEV <[email protected]>
11
 *
12
 * @property  \Illuminate\Database\Eloquent\Collection  roles
13
 *
14
 * @method    \Illuminate\Database\Eloquent\Relations\BelongsToMany  roles()
15
 * @method    \Arcanedev\LaravelAuth\Models\Traits\AuthRoleTrait     load(mixed $relations)
16
 */
17
trait AuthRoleTrait
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Role CRUD Functions
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /**
24
     * Attach a role to a user.
25
     *
26
     * @param  \Arcanesoft\Contracts\Auth\Models\Role|int  $role
27
     * @param  bool                                    $reload
28
     */
29 135
    public function attachRole($role, $reload = true)
30
    {
31 135
        if ( ! $this->hasRole($role)) {
32 135
            $this->roles()->attach($role);
33 135
            $this->loadRoles($reload);
34 45
        }
35 135
    }
36
37
    /**
38
     * Sync the roles by its slugs.
39
     *
40
     * @param  array  $slugs
41
     * @param  bool   $reload
42
     *
43
     * @return array
44
     */
45 18
    public function syncRoles(array $slugs, $reload = true)
46
    {
47
        /** @var \Illuminate\Database\Eloquent\Collection $roles */
48 18
        $roles  = app(RoleContract::class)->whereIn('slug', $slugs)->get();
49 18
        $synced = $this->roles()->sync(
50 18
            $roles->pluck('id')->toArray()
51 6
        );
52
53 18
        $this->loadRoles($reload);
54
55 18
        return $synced;
56
    }
57
58
    /**
59
     * Detach a role from a user.
60
     *
61
     * @param  \Arcanesoft\Contracts\Auth\Models\Role|int  $role
62
     * @param  bool                                        $reload
63
     *
64
     * @return int
65
     */
66 18
    public function detachRole($role, $reload = true)
67
    {
68 18
        if ($role instanceof Eloquent) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
69 18
            $role = (array) $role->getKey();
70 6
        }
71
72 18
        $results = $this->roles()->detach($role);
73 18
        $this->loadRoles($reload);
74
75 18
        return $results;
76
    }
77
78
    /**
79
     * Detach all roles from a user.
80
     *
81
     * @param  bool  $reload
82
     *
83
     * @return int
84
     */
85 18
    public function detachAllRoles($reload = true)
86
    {
87 18
        $results = $this->roles()->detach();
88 18
        $this->loadRoles($reload);
89
90 18
        return $results;
91
    }
92
93
    /* ------------------------------------------------------------------------------------------------
94
     |  Check Functions
95
     | ------------------------------------------------------------------------------------------------
96
     */
97
    /**
98
     * Check if user has the given role (Role Model or Id).
99
     *
100
     * @param  mixed  $id
101
     *
102
     * @return bool
103
     */
104 135
    public function hasRole($id)
105
    {
106 135
        if ($id instanceof Eloquent)
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
107 135
            $id = $id->getKey();
108
109 135
        return $this->roles->contains($id);
110
    }
111
112
    /**
113
     * Check if has all roles.
114
     *
115
     * @param  array  $roles
116
     * @param  array  &$failedRoles
117
     *
118
     * @return bool
119
     */
120 18
    public function isAll(array $roles, array &$failedRoles = [])
121
    {
122 18
        $this->isOne($roles, $failedRoles);
123
124 18
        return count($failedRoles) === 0;
125
    }
126
127
    /**
128
     * Check if has at least one role.
129
     *
130
     * @param  array  $roles
131
     * @param  array  &$failedRoles
132
     *
133
     * @return bool
134
     */
135 36
    public function isOne(array $roles, array &$failedRoles = [])
136
    {
137 36
        foreach ($roles as $role) {
138 36
            if ( ! $this->hasRoleSlug($role))
139 36
                $failedRoles[] = $role;
140 12
        }
141
142 36
        return count($roles) !== count($failedRoles);
143
    }
144
145
    /**
146
     * Check if has a role by its slug.
147
     *
148
     * @param  string  $slug
149
     *
150
     * @return bool
151
     */
152
    public function hasRoleSlug($slug)
153
    {
154 54
        $roles = $this->roles->filter(function(RoleContract $role) use ($slug) {
155 54
            return $role->checkSlug($slug);
156 54
        });
157
158 54
        return ! $roles->isEmpty();
159
    }
160
161
    /* ------------------------------------------------------------------------------------------------
162
     |  Other Functions
163
     | ------------------------------------------------------------------------------------------------
164
     */
165
    /**
166
     * Load all roles.
167
     *
168
     * @param  bool  $load
169
     *
170
     * @return self
171
     */
172 153
    protected function loadRoles($load = true)
173
    {
174 153
        return $load ? $this->load('roles') : $this;
175
    }
176
}
177