Completed
Push — master ( 3d5caa...ae7583 )
by ARCANEDEV
04:59
created

Role::scopeMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: forceFill, getKey
Loading history...
17
{
18
    /* -----------------------------------------------------------------
19
     |  Constants
20
     | -----------------------------------------------------------------
21
     */
22
23
    const ADMINISTRATOR = 'administrator';
24
    const MODERATOR     = 'moderator';
25
    const MEMBER        = 'member';
26
27
    /* -----------------------------------------------------------------
28
     |  Traits
29
     | -----------------------------------------------------------------
30
     */
31
32
    use Presenters\RolePresenter;
33
34
    /* -----------------------------------------------------------------
35
     |  Relationships
36
     | -----------------------------------------------------------------
37
     */
38
39
    /**
40
     * Role belongs to many users.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
43
     */
44
    public function users()
45
    {
46
        return parent::users()->protectAdmins();
47
    }
48
49
    /* -----------------------------------------------------------------
50
     |  Scopes
51
     | -----------------------------------------------------------------
52
     */
53
54
    /**
55
     * Scope only with administrator role.
56
     *
57
     * @param  \Illuminate\Database\Eloquent\Builder  $query
58
     *
59
     * @return \Illuminate\Database\Eloquent\Builder
60
     */
61
    public function scopeAdmin(Builder $query)
62
    {
63
        return $query->where('slug', Role::ADMINISTRATOR);
64
    }
65
66
    /**
67
     * Scope only with moderator role.
68
     *
69
     * @param  \Illuminate\Database\Eloquent\Builder  $query
70
     *
71
     * @return \Illuminate\Database\Eloquent\Builder
72
     */
73
    public function scopeModerator(Builder $query)
74
    {
75
        return $query->where('slug', Role::MODERATOR);
76
    }
77
78
    /**
79
     * Scope only with member role.
80
     *
81
     * @param  \Illuminate\Database\Eloquent\Builder  $query
82
     *
83
     * @return \Illuminate\Database\Eloquent\Builder
84
     */
85
    public function scopeMember(Builder $query)
86
    {
87
        return $query->where('slug', Role::MEMBER);
88
    }
89
90
    /* -----------------------------------------------------------------
91
     |  Main Methods
92
     | -----------------------------------------------------------------
93
     */
94
95
    /**
96
     * Get a role from a hashed id or fail if not found.
97
     *
98
     * @param  string  $hashedId
99
     *
100
     * @return self
101
     *
102
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
103
     */
104
    public static function firstHashedOrFail($hashedId)
105
    {
106
        return self::withHashedId($hashedId)->firstOrFail();
107
    }
108
109
    /**
110
     * Make the slug.
111
     *
112
     * @param  string  $value
113
     *
114
     * @return string
115
     */
116
    public function makeSlugName($value)
117
    {
118
        return $this->slugify($value);
119
    }
120
}
121