Completed
Push — develop ( 9481b1...1c9b89 )
by Abdelrahman
02:54
created

Ability::getRouteKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Models;
6
7
use Vinkla\Hashids\Facades\Hashids;
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\Ability as BaseAbility;
13
14
class Ability extends BaseAbility
15
{
16
    use Auditable;
17
    use LogsActivity;
18
    use HasTranslations;
19
    use ValidatingTrait;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $fillable = [
25
        'name',
26
        'title',
27
        'entity_id',
28
        'entity_type',
29
        'only_owned',
30
        'scope',
31
        'roles',
32
    ];
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected $casts = [
38
        'name' => 'string',
39
        'title' => 'string',
40
        'entity_id' => 'integer',
41
        'entity_type' => 'string',
42
        'only_owned' => 'boolean',
43
        'scope' => 'integer',
44
    ];
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected $observables = [
50
        'validating',
51
        'validated',
52
    ];
53
54
    /**
55
     * The attributes that are translatable.
56
     *
57
     * @var array
58
     */
59
    public $translatable = [
60
        'title',
61
    ];
62
63
    /**
64
     * The default rules that the model will validate against.
65
     *
66
     * @var array
67
     */
68
    protected $rules = [
69
        'title' => 'nullable|string',
70
        'name' => 'required|string|max:150',
71
        'entity_id' => 'nullable|integer',
72
        'entity_type' => 'nullable|string',
73
        'only_owned' => 'sometimes|boolean',
74
        'scope' => 'nullable|integer',
75
    ];
76
77
    /**
78
     * Whether the model should throw a
79
     * ValidationException if it fails validation.
80
     *
81
     * @var bool
82
     */
83
    protected $throwValidationExceptions = true;
84
85
    /**
86
     * Indicates whether to log only dirty attributes or all.
87
     *
88
     * @var bool
89
     */
90
    protected static $logOnlyDirty = true;
91
92
    /**
93
     * The attributes that are logged on change.
94
     *
95
     * @var array
96
     */
97
    protected static $logFillable = true;
98
99
    /**
100
     * The attributes that are ignored on change.
101
     *
102
     * @var array
103
     */
104
    protected static $ignoreChangedAttributes = [
105
        'created_at',
106
        'updated_at',
107
    ];
108
109
    /**
110
     * Attach the given roles to the model.
111
     *
112
     * @param mixed $roles
113
     *
114
     * @return void
115
     */
116
    public function setRolesAttribute($roles): void
117
    {
118
        static::saved(function (self $model) use ($roles) {
119
            $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...
120
121
            $model->roles->pluck('id')->similar($roles)
122
            || activity()
123
                ->performedOn($model)
124
                ->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...
125
                ->log('updated');
126
127
            $model->roles()->sync($roles, true);
128
        });
129
    }
130
131
    /**
132
     * Get the value of the model's route key.
133
     *
134
     * @return mixed
135
     */
136
    public function getRouteKey()
137
    {
138
        return Hashids::encode($this->getAttribute($this->getRouteKeyName()));
139
    }
140
141
    /**
142
     * Retrieve the model for a bound value.
143
     *
144
     * @param  mixed  $value
145
     * @return \Illuminate\Database\Eloquent\Model|null
146
     */
147
    public function resolveRouteBinding($value)
148
    {
149
        $value = Hashids::decode($value)[0];
150
151
        return $this->where($this->getRouteKeyName(), $value)->first();
152
    }
153
}
154