Passed
Push — master ( 4a474d...df1b75 )
by Stephen
02:14
created

Role::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Sfneal\Users\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Sfneal\Models\AbstractModel;
8
use Sfneal\Scopes\OrderScope;
9
use Sfneal\Users\Builders\RoleBuilder;
10
11
class Role extends AbstractModel
12
{
13
    /**
14
     * The "booting" method of the model.
15
     *
16
     * @return void
17
     */
18
    public static function boot()
19
    {
20
        parent::boot();
21
22
        // Global scopes
23
        static::addGlobalScope(new OrderScope('order', 'asc'));
24
    }
25
26
    protected $connection = 'mysql';
27
    protected $table = 'role';
28
    protected $primaryKey = 'role_id';
29
30
    /**
31
     * The attributes that are mass assignable.
32
     *
33
     * @var array
34
     */
35
    protected $fillable = [
36
        'role_id',
37
        'type',
38
        'name',
39
        'description',
40
        'class',
41
        'order',
42
    ];
43
44
    /**
45
     * Query Builder.
46
     *
47
     * @param $query
48
     *
49
     * @return RoleBuilder
50
     */
51
    public function newEloquentBuilder($query)
52
    {
53
        return new RoleBuilder($query);
54
    }
55
56
    /**
57
     * Custom Role query Builder.
58
     *
59
     * @return RoleBuilder|Builder
60
     */
61
    public static function query(): RoleBuilder
62
    {
63
        return parent::query();
64
    }
65
66
    /**
67
     * 'users' with roles.
68
     *
69
     * @return HasMany
70
     */
71
    public function users()
72
    {
73
        return $this->hasMany(User::class, 'role_id', 'role_id')
74
            ->where('type', '=', 'user');
75
    }
76
77
    /**
78
     * Retrieve the 'class' attribute with a default value.
79
     *
80
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
81
     *
82
     * @return string
83
     */
84
    public function getClassAttribute($value = null): string
85
    {
86
        return 'label label-'.($value ?? 'default');
87
    }
88
}
89