Completed
Push — develop ( 3d5642...4ccd66 )
by Abdelrahman
03:11
created

Ability::setRolesAttribute()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 1
nop 1
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\HasTranslations;
9
use Rinvex\Support\Traits\ValidatingTrait;
10
use Spatie\Activitylog\Traits\LogsActivity;
11
use Silber\Bouncer\Database\Ability as BaseAbility;
12
13
class Ability extends BaseAbility
14
{
15
    use Auditable;
16
    use LogsActivity;
17
    use HasTranslations;
18
    use ValidatingTrait;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected $fillable = [
24
        'name',
25
        'title',
26
        'entity_id',
27
        'entity_type',
28
        'only_owned',
29
        'scope',
30
        'roles',
31
    ];
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected $casts = [
37
        'name' => 'string',
38
        'title' => 'string',
39
        'entity_id' => 'integer',
40
        'entity_type' => 'string',
41
        'only_owned' => 'boolean',
42
        'scope' => 'integer',
43
    ];
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected $observables = [
49
        'validating',
50
        'validated',
51
    ];
52
53
    /**
54
     * The attributes that are translatable.
55
     *
56
     * @var array
57
     */
58
    public $translatable = [
59
        'title',
60
    ];
61
62
    /**
63
     * The default rules that the model will validate against.
64
     *
65
     * @var array
66
     */
67
    protected $rules = [
68
        'title' => 'required|string',
69
        'name' => 'required|string|max:150',
70
        'entity_id' => 'nullable|integer',
71
        'entity_type' => 'nullable|string',
72
        'only_owned' => 'sometimes|boolean',
73
        'scope' => 'nullable|integer',
74
    ];
75
76
    /**
77
     * Whether the model should throw a
78
     * ValidationException if it fails validation.
79
     *
80
     * @var bool
81
     */
82
    protected $throwValidationExceptions = true;
83
84
    /**
85
     * Indicates whether to log only dirty attributes or all.
86
     *
87
     * @var bool
88
     */
89
    protected static $logOnlyDirty = true;
90
91
    /**
92
     * The attributes that are logged on change.
93
     *
94
     * @var array
95
     */
96
    protected static $logFillable = true;
97
98
    /**
99
     * The attributes that are ignored on change.
100
     *
101
     * @var array
102
     */
103
    protected static $ignoreChangedAttributes = [
104
        'created_at',
105
        'updated_at',
106
    ];
107
108
    /**
109
     * Attach the given roles to the model.
110
     *
111
     * @param mixed $roles
112
     *
113
     * @return void
114
     */
115
    public function setRolesAttribute($roles): void
116
    {
117
        static::saved(function (self $model) use ($roles) {
118
            $roles = collect($roles)->filter();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $roles, 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...
119
120
            $model->roles->pluck('id')->similar($roles)
121
            || activity()
122
                ->performedOn($model)
123
                ->withProperties(['attributes' => ['roles' => $roles], 'old' => ['roles' => $model->roles->pluck('id')->toArray()]])
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...
124
                ->log('updated');
125
126
            $model->roles()->sync($roles, true);
127
        });
128
    }
129
}
130