Completed
Push — master ( 9e59ff...11ca4d )
by Abdelrahman
66:39 queued 62:06
created

Manager::isSupermanager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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_at',
27
        'phone',
28
        'phone_verified_at',
29
        'given_name',
30
        'family_name',
31
        'title',
32
        'organization',
33
        'country_code',
34
        'language_code',
35
        'birthday',
36
        'gender',
37
        'social',
38
        'is_active',
39
        'last_activity',
40
        'abilities',
41
        'roles',
42
        'tags',
43
        'tenants',
44
    ];
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected $passwordResetNotificationClass = ManagerPasswordResetNotification::class;
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected $emailVerificationNotificationClass = ManagerEmailVerificationNotification::class;
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected $phoneVerificationNotificationClass = PhoneVerificationNotification::class;
60
61
    /**
62
     * Create a new Eloquent model instance.
63
     *
64
     * @param array $attributes
65
     */
66
    public function __construct(array $attributes = [])
67
    {
68
        parent::__construct($attributes);
69
70
        $this->setTable(config('cortex.auth.tables.managers'));
71
        $this->setRules([
72
            'username' => 'required|alpha_dash|min:3|max:150|unique:'.config('cortex.auth.tables.managers').',username',
73
            'password' => 'sometimes|required|min:'.config('cortex.auth.password_min_chars'),
74
            'two_factor' => 'nullable|array',
75
            'email' => 'required|email|min:3|max:150|unique:'.config('cortex.auth.tables.managers').',email',
76
            'email_verified_at' => 'nullable|date',
77
            'phone' => 'nullable|phone:AUTO',
78
            'phone_verified_at' => 'nullable|date',
79
            'given_name' => 'required|string|max:150',
80
            'family_name' => 'nullable|string|max:150',
81
            'title' => 'nullable|string|max:150',
82
            'organization' => 'nullable|string|max:150',
83
            'country_code' => 'nullable|alpha|size:2|country',
84
            'language_code' => 'nullable|alpha|size:2|language',
85
            'birthday' => 'nullable|date_format:Y-m-d',
86
            'gender' => 'nullable|in:male,female',
87
            'social' => 'nullable',
88
            'is_active' => 'sometimes|boolean',
89
            'last_activity' => 'nullable|date',
90
            'tags' => 'nullable|array',
91
            'tenants' => 'nullable|array',
92
        ]);
93
    }
94
95
    /**
96
     * Attach the given tenants to the model.
97
     *
98
     * @param mixed $tenants
99
     *
100
     * @return void
101
     */
102
    public function setTenantsAttribute($tenants): void
103
    {
104
        static::saved(function (self $model) use ($tenants) {
105
            $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...
106
107
            $model->tenants->pluck('id')->similar($tenants)
108
            || activity()
109
                ->performedOn($model)
110
                ->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...
111
                ->log('updated');
112
113
            $model->syncTenants($tenants);
114
        });
115
    }
116
117
    /**
118
     * Get all attached tenants to the manager.
119
     *
120
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
121
     */
122
    public function tenants(): MorphToMany
123
    {
124
        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...
125
                    ->withTimestamps();
126
    }
127
128
    /**
129
     * Determine if this is supermanager of the given tenant.
130
     *
131
     * @param \Illuminate\Database\Eloquent\Model $tenant
132
     *
133
     * @return bool
134
     */
135
    public function isSupermanager(Model $tenant): bool
136
    {
137
        return $this->isManager($tenant) && $this->isA('supermanager');
138
    }
139
140
    /**
141
     * Determine if this is manager of the given tenant.
142
     *
143
     * @param \Illuminate\Database\Eloquent\Model $tenant
144
     *
145
     * @return bool
146
     */
147
    public function isManager(Model $tenant): bool
148
    {
149
        return $this->tenants->contains($tenant);
150
    }
151
152
    /**
153
     * Get the route key for the model.
154
     *
155
     * @return string
156
     */
157
    public function getRouteKeyName()
158
    {
159
        return 'username';
160
    }
161
}
162