Completed
Push — develop ( 98197c...3d5642 )
by Abdelrahman
01:54
created

Ability   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 7
dl 0
loc 137
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A setRolesAttribute() 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\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
        'name' => 'nullable|string|max:150',
69
        'title' => 'nullable|string',
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
     * Create a new Eloquent model instance.
110
     *
111
     * @param array $attributes
112
     */
113
    public function __construct(array $attributes = [])
114
    {
115
        parent::__construct($attributes);
116
117
        $this->setTable(config('cortex.auth.tables.abilities'));
118
        $this->setRules([
119
            'name' => 'required|string|max:150|unique:'.config('cortex.auth.tables.abilities').',name',
120
            'title' => 'nullable|string',
121
            'entity_id' => 'nullable|integer',
122
            'entity_type' => 'nullable|string',
123
            'only_owned' => 'sometimes|boolean',
124
            'scope' => 'nullable|integer',
125
        ]);
126
    }
127
128
    /**
129
     * Attach the given roles to the model.
130
     *
131
     * @param mixed $roles
132
     *
133
     * @return void
134
     */
135
    public function setRolesAttribute($roles): void
136
    {
137
        static::saved(function (self $model) use ($roles) {
138
            $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...
139
140
            $model->roles->pluck('id')->similar($roles)
141
            || activity()
142
                ->performedOn($model)
143
                ->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...
144
                ->log('updated');
145
146
            $model->roles()->sync($roles, true);
147
        });
148
    }
149
}
150