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

Role::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 Rinvex\Tenants\Traits\Tenantable;
9
use Cortex\Foundation\Traits\Auditable;
10
use Rinvex\Support\Traits\HasTranslations;
11
use Rinvex\Support\Traits\ValidatingTrait;
12
use Spatie\Activitylog\Traits\LogsActivity;
13
use Silber\Bouncer\Database\Role as BaseRole;
14
15
class Role extends BaseRole
16
{
17
    use Auditable;
18
    use Tenantable;
19
    use LogsActivity;
20
    use ValidatingTrait;
21
    use HasTranslations;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected $fillable = [
27
        'name',
28
        'title',
29
        'scope',
30
        'abilities',
31
    ];
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected $casts = [
37
        'name' => 'string',
38
        'title' => 'string',
39
        'scope' => 'integer',
40
    ];
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected $observables = [
46
        'validating',
47
        'validated',
48
    ];
49
50
    /**
51
     * The attributes that are translatable.
52
     *
53
     * @var array
54
     */
55
    public $translatable = [
56
        'title',
57
    ];
58
59
    /**
60
     * The default rules that the model will validate against.
61
     *
62
     * @var array
63
     */
64
    protected $rules = [];
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected $validationMessages = [
70
        'name.unique' => 'The combination of (name & scope) fields has already been taken.',
71
        'scope.unique' => 'The combination of (name & scope) fields has already been taken.',
72
    ];
73
74
    /**
75
     * Whether the model should throw a
76
     * ValidationException if it fails validation.
77
     *
78
     * @var bool
79
     */
80
    protected $throwValidationExceptions = true;
81
82
    /**
83
     * Indicates whether to log only dirty attributes or all.
84
     *
85
     * @var bool
86
     */
87
    protected static $logOnlyDirty = true;
88
89
    /**
90
     * The attributes that are logged on change.
91
     *
92
     * @var array
93
     */
94
    protected static $logFillable = true;
95
96
    /**
97
     * The attributes that are ignored on change.
98
     *
99
     * @var array
100
     */
101
    protected static $ignoreChangedAttributes = [
102
        'created_at',
103
        'updated_at',
104
    ];
105
106
    /**
107
     * Create a new Eloquent model instance.
108
     *
109
     * @param array $attributes
110
     */
111
    public function __construct(array $attributes = [])
112
    {
113
        parent::__construct($attributes);
114
115
        $this->setRules([
116
            'title' => 'nullable|string|max:150',
117
            '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...
118
            '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...
119
        ]);
120
    }
121
122
    /**
123
     * Attach the given abilities to the model.
124
     *
125
     * @param mixed $abilities
126
     *
127
     * @return void
128
     */
129
    public function setAbilitiesAttribute($abilities): void
130
    {
131
        static::saved(function (self $model) use ($abilities) {
132
            $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...
133
134
            $model->abilities->pluck('id')->similar($abilities)
135
            || activity()
136
                ->performedOn($model)
137
                ->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...
138
                ->log('updated');
139
140
            $model->abilities()->sync($abilities, true);
141
        });
142
    }
143
144
    /**
145
     * Get the value of the model's route key.
146
     *
147
     * @return mixed
148
     */
149
    public function getRouteKey()
150
    {
151
        return Hashids::encode($this->getAttribute($this->getRouteKeyName()));
152
    }
153
154
    /**
155
     * Retrieve the model for a bound value.
156
     *
157
     * @param  mixed  $value
158
     * @return \Illuminate\Database\Eloquent\Model|null
159
     */
160
    public function resolveRouteBinding($value)
161
    {
162
        $value = Hashids::decode($value)[0];
163
164
        return $this->where($this->getRouteKeyName(), $value)->first();
165
    }
166
}
167