Completed
Push — develop ( 5abd4c...dae2aa )
by Abdelrahman
09:07
created

Role   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A setAbilitiesAttribute() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Models;
6
7
use Rinvex\Tenants\Traits\Tenantable;
8
use Cortex\Foundation\Traits\Auditable;
9
use Rinvex\Support\Traits\HasTranslations;
10
use Rinvex\Support\Traits\ValidatingTrait;
11
use Spatie\Activitylog\Traits\LogsActivity;
12
use Silber\Bouncer\Database\Role as BaseRole;
13
14
class Role extends BaseRole
15
{
16
    use Auditable;
17
    //use Tenantable;
18
    use LogsActivity;
19
    use ValidatingTrait;
20
    use HasTranslations;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected $fillable = [
26
        'name',
27
        'title',
28
        'scope',
29
        'abilities',
30
    ];
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected $casts = [
36
        'name' => 'string',
37
        'title' => 'string',
38
        'scope' => 'integer',
39
    ];
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected $observables = [
45
        'validating',
46
        'validated',
47
    ];
48
49
    /**
50
     * The attributes that are translatable.
51
     *
52
     * @var array
53
     */
54
    public $translatable = [
55
        'title',
56
    ];
57
58
    /**
59
     * The default rules that the model will validate against.
60
     *
61
     * @var array
62
     */
63
    protected $rules = [];
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected $validationMessages = [
69
        'name.unique' => 'The combination of (name & scope) fields has already been taken.',
70
        'scope.unique' => 'The combination of (name & scope) fields has already been taken.',
71
    ];
72
73
    /**
74
     * Whether the model should throw a
75
     * ValidationException if it fails validation.
76
     *
77
     * @var bool
78
     */
79
    protected $throwValidationExceptions = true;
80
81
    /**
82
     * Indicates whether to log only dirty attributes or all.
83
     *
84
     * @var bool
85
     */
86
    protected static $logOnlyDirty = true;
87
88
    /**
89
     * The attributes that are logged on change.
90
     *
91
     * @var array
92
     */
93
    protected static $logFillable = true;
94
95
    /**
96
     * The attributes that are ignored on change.
97
     *
98
     * @var array
99
     */
100
    protected static $ignoreChangedAttributes = [
101
        'created_at',
102
        'updated_at',
103
    ];
104
105
    /**
106
     * Create a new Eloquent model instance.
107
     *
108
     * @param array $attributes
109
     */
110
    public function __construct(array $attributes = [])
111
    {
112
        parent::__construct($attributes);
113
114
        $this->setRules([
115
            'title' => 'nullable|string|max:150',
116
            'name' => 'required|string|max:150|unique:'.config('cortex.auth.tables.roles').',name,NULL,id,scope,'.($this->scope ?? 'null'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
117
            'scope' => 'nullable|integer|unique:'.config('cortex.auth.tables.roles').',scope,NULL,id,name,'.($this->name ?? 'null'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
118
        ]);
119
    }
120
121
    /**
122
     * Attach the given abilities to the model.
123
     *
124
     * @param mixed $abilities
125
     *
126
     * @return void
127
     */
128
    public function setAbilitiesAttribute($abilities): void
129
    {
130
        static::saved(function (self $model) use ($abilities) {
131
            activity()
132
                ->performedOn($model)
133
                ->withProperties(['attributes' => ['abilities' => $abilities], 'old' => ['abilities' => $model->abilities->pluck('id')->toArray()]])
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 148 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
134
                ->log('updated');
135
136
            $model->abilities()->sync($abilities, true);
137
        });
138
    }
139
}
140