Completed
Push — master ( 7b13b6...8da79d )
by ARCANEDEV
05:05
created

Role   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 94
ccs 0
cts 7
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeAdmins() 0 4 1
A scopeModerator() 0 4 1
A scopeMembers() 0 4 1
A getHashedIdAttribute() 0 4 1
A firstHashedOrFail() 0 6 1
A makeSlugName() 0 4 1
1
<?php namespace Arcanesoft\Auth\Models;
2
3
use Arcanedev\LaravelAuth\Models\Role as BaseRoleModel;
4
use Illuminate\Database\Eloquent\Builder;
5
6
/**
7
 * Class     Role
8
 *
9
 * @package  Arcanesoft\Auth\Models
10
 * @author   ARCANEDEV <[email protected]>
11
 *
12
 * @method static \Illuminate\Database\Eloquent\Builder  admins()
13
 * @method static \Illuminate\Database\Eloquent\Builder  members()
14
 */
15
class Role extends BaseRoleModel
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Constants
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    const ADMINISTRATOR = 'administrator';
22
    const MODERATOR     = 'moderator';
23
    const MEMBER        = 'member';
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Scopes
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * Scope only with administrator role.
31
     *
32
     * @param  \Illuminate\Database\Eloquent\Builder  $query
33
     *
34
     * @return \Illuminate\Database\Eloquent\Builder
35
     */
36
    public function scopeAdmins(Builder $query)
37
    {
38
        return $query->where('slug', Role::ADMINISTRATOR);
39
    }
40
41
    /**
42
     * Scope only with moderator role.
43
     *
44
     * @param  \Illuminate\Database\Eloquent\Builder  $query
45
     *
46
     * @return \Illuminate\Database\Eloquent\Builder
47
     */
48
    public function scopeModerator(Builder $query)
49
    {
50
        return $query->where('slug', Role::MODERATOR);
51
    }
52
53
    /**
54
     * Scope only with member role.
55
     *
56
     * @param  \Illuminate\Database\Eloquent\Builder  $query
57
     *
58
     * @return \Illuminate\Database\Eloquent\Builder
59
     */
60
    public function scopeMembers(Builder $query)
61
    {
62
        return $query->where('slug', Role::MEMBER);
63
    }
64
65
    /* ------------------------------------------------------------------------------------------------
66
     |  Getters & Setters
67
     | ------------------------------------------------------------------------------------------------
68
     */
69
    /**
70
     * Get the role hash id.
71
     *
72
     * @return string
73
     */
74
    public function getHashedIdAttribute()
75
    {
76
        return hasher()->encode($this->id);
77
    }
78
79
    /* ------------------------------------------------------------------------------------------------
80
     |  Main Function
81
     | ------------------------------------------------------------------------------------------------
82
     */
83
    /**
84
     * Get a role from a hashed id or fail if not found.
85
     *
86
     * @param  string  $hashedId
87
     *
88
     * @return self
89
     *
90
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
91
     */
92
    public static function firstHashedOrFail($hashedId)
93
    {
94
        $id = head(hasher()->decode($hashedId));
95
96
        return self::where('id', $id)->firstOrFail();
97
    }
98
99
    /**
100
     * @param  string  $value
101
     *
102
     * @return string
103
     */
104
    public function makeSlugName($value)
105
    {
106
        return $this->slugify($value);
107
    }
108
}
109