Completed
Push — master ( 8da79d...fd43b2 )
by ARCANEDEV
03:03
created

Role   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeAdmin() 0 4 1
A scopeModerator() 0 4 1
A scopeMember() 0 4 1
A getHashedIdAttribute() 0 4 1
A firstHashedOrFail() 0 6 1
A makeSlugName() 0 4 1
A hasher() 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  admin()
13
 * @method static \Illuminate\Database\Eloquent\Builder  moderator()
14
 * @method static \Illuminate\Database\Eloquent\Builder  member()
15
 */
16
class Role extends BaseRoleModel
17
{
18
    /* ------------------------------------------------------------------------------------------------
19
     |  Constants
20
     | ------------------------------------------------------------------------------------------------
21
     */
22
    const ADMINISTRATOR = 'administrator';
23
    const MODERATOR     = 'moderator';
24
    const MEMBER        = 'member';
25
26
    /* ------------------------------------------------------------------------------------------------
27
     |  Scopes
28
     | ------------------------------------------------------------------------------------------------
29
     */
30
    /**
31
     * Scope only with administrator role.
32
     *
33
     * @param  \Illuminate\Database\Eloquent\Builder  $query
34
     *
35
     * @return \Illuminate\Database\Eloquent\Builder
36
     */
37
    public function scopeAdmin(Builder $query)
38
    {
39
        return $query->where('slug', Role::ADMINISTRATOR);
40
    }
41
42
    /**
43
     * Scope only with moderator role.
44
     *
45
     * @param  \Illuminate\Database\Eloquent\Builder  $query
46
     *
47
     * @return \Illuminate\Database\Eloquent\Builder
48
     */
49
    public function scopeModerator(Builder $query)
50
    {
51
        return $query->where('slug', Role::MODERATOR);
52
    }
53
54
    /**
55
     * Scope only with member role.
56
     *
57
     * @param  \Illuminate\Database\Eloquent\Builder  $query
58
     *
59
     * @return \Illuminate\Database\Eloquent\Builder
60
     */
61
    public function scopeMember(Builder $query)
62
    {
63
        return $query->where('slug', Role::MEMBER);
64
    }
65
66
    /* ------------------------------------------------------------------------------------------------
67
     |  Getters & Setters
68
     | ------------------------------------------------------------------------------------------------
69
     */
70
    /**
71
     * Get the role hash id.
72
     *
73
     * @return string
74
     */
75
    public function getHashedIdAttribute()
76
    {
77
        return self::hasher()->encode($this->id);
78
    }
79
80
    /* ------------------------------------------------------------------------------------------------
81
     |  Main Function
82
     | ------------------------------------------------------------------------------------------------
83
     */
84
    /**
85
     * Get a role from a hashed id or fail if not found.
86
     *
87
     * @param  string  $hashedId
88
     *
89
     * @return self
90
     *
91
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
92
     */
93
    public static function firstHashedOrFail($hashedId)
94
    {
95
        $id = self::hasher()->decode($hashedId);
96
97
        return self::where('id', $id)->firstOrFail();
98
    }
99
100
    /**
101
     * @param  string  $value
102
     *
103
     * @return string
104
     */
105
    public function makeSlugName($value)
106
    {
107
        return $this->slugify($value);
108
    }
109
110
    /* ------------------------------------------------------------------------------------------------
111
     |  Other Functions
112
     | ------------------------------------------------------------------------------------------------
113
     */
114
    /**
115
     * Get the hasher.
116
     *
117
     * @return \Arcanedev\Hasher\Contracts\HashManager
118
     */
119
    protected static function hasher()
120
    {
121
        return hasher();
122
    }
123
}
124