Completed
Pull Request — master (#23)
by ARCANEDEV
08:29
created

AuthRoleTrait::syncRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php namespace Arcanedev\LaravelAuth\Models\Traits;
2
3
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract;
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  mixed  $id
26
     *
27
     * @return bool
28
     */
29 45
    public function hasRole($id)
30
    {
31 45
        return $this->roles->contains($id);
32
    }
33
34
    /**
35
     * Check if has all roles.
36
     *
37
     * @param  array  $roles
38
     * @param  array  &$failedRoles
39
     *
40
     * @return bool
41
     */
42 6
    public function isAll(array $roles, array &$failedRoles = [])
43
    {
44 6
        $this->isOne($roles, $failedRoles);
45
46 6
        return count($failedRoles) === 0;
47
    }
48
49
    /**
50
     * Check if has at least one role.
51
     *
52
     * @param  array  $roles
53
     * @param  array  &$failedRoles
54
     *
55
     * @return bool
56
     */
57 12
    public function isOne(array $roles, array &$failedRoles = [])
58
    {
59 12
        foreach ($roles as $role) {
60 12
            if ( ! $this->hasRoleSlug($role))
61 12
                $failedRoles[] = $role;
62
        }
63
64 12
        return count($roles) !== count($failedRoles);
65
    }
66
67
    /**
68
     * Check if has a role by its slug.
69
     *
70
     * @param  string  $slug
71
     *
72
     * @return bool
73
     */
74
    public function hasRoleSlug($slug)
75
    {
76 18
        $roles = $this->roles->filter(function(RoleContract $role) use ($slug) {
77 18
            return $role->checkSlug($slug);
78 18
        });
79
80 18
        return ! $roles->isEmpty();
81
    }
82
83
    /* -----------------------------------------------------------------
84
     |  Other Methods
85
     | -----------------------------------------------------------------
86
     */
87
    /**
88
     * Load all roles.
89
     *
90
     * @param  bool  $load
91
     *
92
     * @return self
93
     */
94 51
    protected function loadRoles($load = true)
95
    {
96 51
        return $load ? $this->load('roles') : $this;
97
    }
98
}
99