Completed
Push — develop ( 1e6c4d...2ee822 )
by Abdelrahman
02:34
created

Manager::getRouteKeyName()   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 Rinvex\Tenants\Traits\Tenantable;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\MorphToMany;
10
use Cortex\Auth\Notifications\PhoneVerificationNotification;
11
use Cortex\Auth\Notifications\ManagerPasswordResetNotification;
12
use Cortex\Auth\Notifications\ManagerEmailVerificationNotification;
13
14
class Manager extends User
15
{
16
    use Tenantable;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $fillable = [
22
        'username',
23
        'password',
24
        'two_factor',
25
        'email',
26
        'email_verified',
27
        'email_verified_at',
28
        'phone',
29
        'phone_verified',
30
        'phone_verified_at',
31
        'full_name',
32
        'title',
33
        'country_code',
34
        'language_code',
35
        'birthday',
36
        'gender',
37
        'is_active',
38
        'last_activity',
39
        'abilities',
40
        'roles',
41
        'tenants',
42
    ];
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected $passwordResetNotificationClass = ManagerPasswordResetNotification::class;
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected $emailVerificationNotificationClass = ManagerEmailVerificationNotification::class;
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected $phoneVerificationNotificationClass = PhoneVerificationNotification::class;
58
59
    /**
60
     * Create a new Eloquent model instance.
61
     *
62
     * @param array $attributes
63
     */
64
    public function __construct(array $attributes = [])
65
    {
66
        parent::__construct($attributes);
67
68
        $this->setTable(config('cortex.auth.tables.managers'));
69
        $this->setRules([
70
            'username' => 'required|alpha_dash|min:3|max:150|unique:'.config('cortex.auth.tables.managers').',username',
71
            'password' => 'sometimes|required|min:'.config('cortex.auth.password_min_chars'),
72
            'two_factor' => 'nullable|array',
73
            'email' => 'required|email|min:3|max:150|unique:'.config('cortex.auth.tables.managers').',email',
74
            'email_verified' => 'sometimes|boolean',
75
            'email_verified_at' => 'nullable|date',
76
            'phone' => 'nullable|phone:AUTO',
77
            'phone_verified' => 'sometimes|boolean',
78
            'phone_verified_at' => 'nullable|date',
79
            'full_name' => 'required|string|max:150',
80
            'title' => 'nullable|string|max:150',
81
            'country_code' => 'nullable|alpha|size:2|country',
82
            'language_code' => 'nullable|alpha|size:2|language',
83
            'birthday' => 'nullable|date_format:Y-m-d',
84
            'gender' => 'nullable|in:male,female',
85
            'is_active' => 'sometimes|boolean',
86
            'last_activity' => 'nullable|date',
87
            'tags' => 'nullable|array',
88
        ]);
89
    }
90
91
    /**
92
     * Attach the given tenants to the model.
93
     *
94
     * @param mixed $tenants
95
     *
96
     * @return void
97
     */
98
    public function setTenantsAttribute($tenants): void
99
    {
100
        static::saved(function (self $model) use ($tenants) {
101
            $tenants = collect($tenants)->filter();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $tenants, 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...
102
103
            $model->tenants->pluck('id')->similar($tenants)
104
            || activity()
105
                ->performedOn($model)
106
                ->withProperties(['attributes' => ['tenants' => $tenants], 'old' => ['tenants' => $model->tenants->pluck('id')->toArray()]])
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 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...
107
                ->log('updated');
108
109
            $model->syncTenants($tenants);
110
        });
111
    }
112
113
    /**
114
     * Get all attached tenants to the manager.
115
     *
116
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
117
     */
118
    public function tenants(): MorphToMany
119
    {
120
        return $this->morphToMany(config('rinvex.tenants.models.tenant'), 'tenantable', config('rinvex.tenants.tables.tenantables'), 'tenantable_id', 'tenant_id')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 162 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...
121
                    ->withTimestamps();
122
    }
123
124
    /**
125
     * Determine if manager is owner of the given tenant.
126
     *
127
     * @param \Illuminate\Database\Eloquent\Model $tenant
128
     *
129
     * @return bool
130
     */
131
    public function isOwner(Model $tenant): bool
132
    {
133
        return $this->getKey() === $tenant->owner->getKey();
134
    }
135
136
    /**
137
     * Determine if manager is staff of the given tenant.
138
     *
139
     * @param \Illuminate\Database\Eloquent\Model $tenant
140
     *
141
     * @return bool
142
     */
143
    public function isStaff(Model $tenant): bool
144
    {
145
        return $this->tenants->contains($tenant);
146
    }
147
148
    /**
149
     * Get the route key for the model.
150
     *
151
     * @return string
152
     */
153
    public function getRouteKeyName()
154
    {
155
        return 'username';
156
    }
157
}
158