Completed
Push — master ( f2b66c...15eb15 )
by Abdelrahman
11:47 queued 10:02
created

src/Models/Role.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Models;
6
7
use Rinvex\Tenants\Traits\Tenantable;
8
use Cortex\Foundation\Traits\Auditable;
9
use Rinvex\Support\Traits\HashidsTrait;
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 HashidsTrait;
20
    use LogsActivity;
21
    use ValidatingTrait;
22
    use HasTranslations;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected $fillable = [
28
        'name',
29
        'title',
30
        'scope',
31
        'abilities',
32
    ];
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected $casts = [
38
        'name' => 'string',
39
        'title' => 'string',
40
        'scope' => 'integer',
41
    ];
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected $observables = [
47
        'validating',
48
        'validated',
49
    ];
50
51
    /**
52
     * The attributes that are translatable.
53
     *
54
     * @var array
55
     */
56
    public $translatable = [
57
        'title',
58
    ];
59
60
    /**
61
     * The default rules that the model will validate against.
62
     *
63
     * @var array
64
     */
65
    protected $rules = [];
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected $validationMessages = [
71
        'name.unique' => 'The combination of (name & scope) fields has already been taken.',
72
        'scope.unique' => 'The combination of (name & scope) fields has already been taken.',
73
    ];
74
75
    /**
76
     * Whether the model should throw a
77
     * ValidationException if it fails validation.
78
     *
79
     * @var bool
80
     */
81
    protected $throwValidationExceptions = true;
82
83
    /**
84
     * Indicates whether to log only dirty attributes or all.
85
     *
86
     * @var bool
87
     */
88
    protected static $logOnlyDirty = true;
89
90
    /**
91
     * The attributes that are logged on change.
92
     *
93
     * @var array
94
     */
95
    protected static $logFillable = true;
96
97
    /**
98
     * The attributes that are ignored on change.
99
     *
100
     * @var array
101
     */
102
    protected static $ignoreChangedAttributes = [
103
        'created_at',
104
        'updated_at',
105
    ];
106
107
    /**
108
     * Create a new Eloquent model instance.
109
     *
110
     * @param array $attributes
111
     */
112
    public function __construct(array $attributes = [])
113
    {
114
        parent::__construct($attributes);
115
116
        $this->setRules([
117
            'title' => 'nullable|string|max:150',
118
            'name' => 'required|string|max:150|unique:'.config('cortex.auth.tables.roles').',name,NULL,id,scope,'.($this->scope ?? 'null'),
0 ignored issues
show
The property scope does not exist on object<Cortex\Auth\Models\Role>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
119
            'scope' => 'nullable|integer|unique:'.config('cortex.auth.tables.roles').',scope,NULL,id,name,'.($this->name ?? 'null'),
0 ignored issues
show
The property name does not exist on object<Cortex\Auth\Models\Role>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120
        ]);
121
    }
122
123
    /**
124
     * Attach the given abilities to the model.
125
     *
126
     * @param mixed $abilities
127
     *
128
     * @return void
129
     */
130
    public function setAbilitiesAttribute($abilities): void
131
    {
132
        static::saved(function (self $model) use ($abilities) {
133
            $abilities = collect($abilities)->filter();
134
135
            $model->abilities->pluck('id')->similar($abilities)
136
            || activity()
137
                ->performedOn($model)
138
                ->withProperties(['attributes' => ['abilities' => $abilities], 'old' => ['abilities' => $model->abilities->pluck('id')->toArray()]])
139
                ->log('updated');
140
141
            $model->abilities()->sync($abilities, true);
142
        });
143
    }
144
}
145