Role   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 8
dl 0
loc 129
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A setAbilitiesAttribute() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Models;
6
7
use Cortex\Foundation\Traits\Auditable;
8
use Rinvex\Support\Traits\HashidsTrait;
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 HashidsTrait;
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'),
117
            'scope' => 'nullable|integer|unique:'.config('cortex.auth.tables.roles').',scope,NULL,id,name,'.($this->name ?? 'null'),
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
            $abilities = collect($abilities)->filter();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $abilities, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
132
133
            $model->abilities->pluck('id')->similar($abilities)
134
            || activity()
135
                ->performedOn($model)
136
                ->withProperties(['attributes' => ['abilities' => $abilities], 'old' => ['abilities' => $model->abilities->pluck('id')->toArray()]])
137
                ->log('updated');
138
139
            $model->abilities()->sync($abilities, true);
140
        });
141
    }
142
}
143