Passed
Pull Request — master (#6)
by
unknown
05:06
created

Role   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 31
c 1
b 0
f 1
dl 0
loc 77
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A users() 0 3 1
A rolePermissions() 0 3 1
A permissions() 0 11 1
1
<?php
2
3
namespace App\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
use Illuminate\Database\Eloquent\SoftDeletes;
11
use Illuminate\Database\Query\Builder as QueryBuilder;
12
use IonGhitun\MysqlEncryption\Models\BaseModel;
13
14
/**
15
 * Class Role
16
 *
17
 * @property int $id
18
 * @property string $name
19
 * @property Carbon|null $created_at
20
 * @property Carbon|null $updated_at
21
 * @property Carbon|null $deleted_at
22
 *
23
 * @property-read Collection|User[] $users
24
 * @property-read Collection|Permission[] $permissions
25
 * @property-read Collection|RolePermission[] $rolePermissions
26
 * @property-read int|null $permissions_count
27
 * @property-read int|null $role_permissions_count
28
 * @property-read int|null $users_count
29
 *
30
 * @method static Builder|Role newModelQuery()
31
 * @method static Builder|Role newQuery()
32
 * @method static Builder|Role query()
33
 * @method static Builder|Role whereCreatedAt($value)
34
 * @method static Builder|Role whereDeletedAt($value)
35
 * @method static Builder|Role whereId($value)
36
 * @method static Builder|Role whereName($value)
37
 * @method static Builder|Role whereUpdatedAt($value)
38
 * @method static QueryBuilder|Role onlyTrashed()
39
 * @method static QueryBuilder|Role withTrashed()
40
 * @method static QueryBuilder|Role withoutTrashed()
41
 * @method static Builder|BaseModel orWhereEncrypted($column, $value)
42
 * @method static Builder|BaseModel orWhereNotEncrypted($column, $value)
43
 * @method static Builder|BaseModel orderByEncrypted($column, $direction)
44
 * @method static Builder|BaseModel whereEncrypted($column, $value)
45
 * @method static Builder|BaseModel whereNotEncrypted($column, $value)
46
 *
47
 * @package App\Models
48
 */
49
class Role extends Model
50
{
51
    use SoftDeletes;
52
53
    /** @var int */
54
    const ID_ADMIN = 1;
55
56
    /** @var int */
57
    const ID_USER = 2;
58
59
    /** @var bool */
60
    public $timestamps = true;
61
62
    /** @var string */
63
    protected $table = 'roles';
64
65
    /** @var array */
66
    protected $fillable = [
67
        'name'
68
    ];
69
70
    /** @var array */
71
    protected $visible = [
72
        'id',
73
        'name',
74
        'users',
75
        'permissions',
76
        'access',
77
        'rolePermissions'
78
    ];
79
80
    /** @var array */
81
    protected $sortable = [
82
        'id',
83
        'name'
84
    ];
85
86
    /** @var array */
87
    protected $searchable = [
88
        'name'
89
    ];
90
91
    /**
92
     * Role users.
93
     *
94
     * @return HasMany
95
     */
96
    public function users()
97
    {
98
        return $this->hasMany(User::class, 'role_id', 'id');
99
    }
100
101
    /**
102
     * @return BelongsToMany
103
     */
104
    public function permissions()
105
    {
106
        return $this->belongsToMany(Permission::class, 'role_permissions', 'role_id', 'permission_id')
107
                    ->as('access')
108
                    ->using(RolePermission::class)
109
                    ->withPivot([
110
                        'read',
111
                        'create',
112
                        'update',
113
                        'delete',
114
                        'manage'
115
                    ]);
116
    }
117
118
    /**
119
     * Role permissions.
120
     *
121
     * @return HasMany
122
     */
123
    public function rolePermissions()
124
    {
125
        return $this->hasMany(RolePermission::class, 'role_id', 'id');
126
    }
127
}
128