Completed
Pull Request — master (#23)
by ARCANEDEV
07:14
created

AuthRoleTrait::attachRole()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php namespace Arcanedev\LaravelAuth\Models\Traits;
2
3
use Arcanesoft\Contracts\Auth\Models\Role;
4
5
/**
6
 * Trait     AuthRoleTrait
7
 *
8
 * @package  Arcanedev\LaravelAuth\Traits
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  \Illuminate\Database\Eloquent\Collection  roles
12
 *
13
 * @method    \Illuminate\Database\Eloquent\Relations\BelongsToMany  roles()
14
 * @method    \Arcanedev\LaravelAuth\Models\Traits\AuthRoleTrait     load(mixed $relations)
15
 */
16
trait AuthRoleTrait
17
{
18
    /* -----------------------------------------------------------------
19
     |  Check Methods
20
     | -----------------------------------------------------------------
21
     */
22
    /**
23
     * Check if user has the given role (Role Model or Id).
24
     *
25
     * @param  \Arcanesoft\Contracts\Auth\Models\Role|int  $id
26
     *
27
     * @return bool
28
     */
29 45
    public function hasRole($id)
30
    {
31 45
        if ($id instanceof Role) $id = $id->getKey();
0 ignored issues
show
Bug introduced by
The method getKey() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\Role>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
33 45
        return $this->roles->contains('id', $id);
34
    }
35
36
    /**
37
     * Check if has all roles.
38
     *
39
     * @param  array  $roles
40
     * @param  array  &$failedRoles
41
     *
42
     * @return bool
43
     */
44 6
    public function isAll(array $roles, array &$failedRoles = [])
45
    {
46 6
        $this->isOne($roles, $failedRoles);
47
48 6
        return count($failedRoles) === 0;
49
    }
50
51
    /**
52
     * Check if has at least one role.
53
     *
54
     * @param  array  $roles
55
     * @param  array  &$failedRoles
56
     *
57
     * @return bool
58
     */
59 12
    public function isOne(array $roles, array &$failedRoles = [])
60
    {
61 12
        foreach ($roles as $role) {
62 12
            if ( ! $this->hasRoleSlug($role)) $failedRoles[] = $role;
63
        }
64
65 12
        return count($roles) !== count($failedRoles);
66
    }
67
68
    /**
69
     * Check if has a role by its slug.
70
     *
71
     * @param  string  $slug
72
     *
73
     * @return bool
74
     */
75 18
    public function hasRoleSlug($slug)
76
    {
77 18
        return ! $this->roles->filter->hasSlug($slug)->isEmpty();
78
    }
79
80
    /* -----------------------------------------------------------------
81
     |  Other Methods
82
     | -----------------------------------------------------------------
83
     */
84
    /**
85
     * Load all roles.
86
     *
87
     * @param  bool  $load
88
     *
89
     * @return self
90
     */
91 51
    protected function loadRoles($load = true)
92
    {
93 51
        return $load ? $this->load('roles') : $this;
94
    }
95
}
96