Completed
Push — master ( b6e961...8692a9 )
by Abdelrahman
15:05 queued 12:57
created

User::activate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Fort\Models;
6
7
use Rinvex\Fort\Traits\HasRoles;
8
use Illuminate\Auth\Authenticatable;
9
use Illuminate\Support\Facades\Hash;
10
use Rinvex\Fort\Traits\CanVerifyEmail;
11
use Rinvex\Fort\Traits\CanVerifyPhone;
12
use Illuminate\Database\Eloquent\Model;
13
use Rinvex\Cacheable\CacheableEloquent;
14
use Rinvex\Fort\Contracts\UserContract;
15
use Rinvex\Support\Traits\HasHashables;
16
use Illuminate\Notifications\Notifiable;
17
use Rinvex\Fort\Traits\CanResetPassword;
18
use Rinvex\Support\Traits\ValidatingTrait;
19
use Rinvex\Fort\Traits\AuthenticatableTwoFactor;
20
use Rinvex\Fort\Contracts\CanVerifyEmailContract;
21
use Rinvex\Fort\Contracts\CanVerifyPhoneContract;
22
use Illuminate\Database\Eloquent\Relations\HasMany;
23
use Illuminate\Foundation\Auth\Access\Authorizable;
24
use Rinvex\Fort\Contracts\CanResetPasswordContract;
25
use Rinvex\Fort\Contracts\AuthenticatableTwoFactorContract;
26
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
27
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
28
29
/**
30
 * Rinvex\Fort\Models\User.
31
 *
32
 * @property int                                                                                                            $id
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 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...
33
 * @property string                                                                                                         $username
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 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...
34
 * @property string                                                                                                         $password
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 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...
35
 * @property string|null                                                                                                    $remember_token
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...
36
 * @property string                                                                                                         $email
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
37
 * @property bool                                                                                                           $email_verified
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...
38
 * @property \Carbon\Carbon                                                                                                 $email_verified_at
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 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...
39
 * @property string                                                                                                         $phone
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
40
 * @property bool                                                                                                           $phone_verified
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...
41
 * @property \Carbon\Carbon                                                                                                 $phone_verified_at
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 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...
42
 * @property string                                                                                                         $name_prefix
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
43
 * @property string                                                                                                         $first_name
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
44
 * @property string                                                                                                         $middle_name
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
45
 * @property string                                                                                                         $last_name
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
46
 * @property string                                                                                                         $name_suffix
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
47
 * @property string                                                                                                         $job_title
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
48
 * @property string                                                                                                         $country_code
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 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...
49
 * @property string                                                                                                         $language_code
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
50
 * @property array                                                                                                          $two_factor
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
51
 * @property string                                                                                                         $birthday
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 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...
52
 * @property string                                                                                                         $gender
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 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...
53
 * @property bool                                                                                                           $is_active
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
54
 * @property \Carbon\Carbon                                                                                                 $last_activity
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
55
 * @property \Carbon\Carbon|null                                                                                            $created_at
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
56
 * @property \Carbon\Carbon|null                                                                                            $updated_at
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
57
 * @property \Carbon\Carbon|null                                                                                            $deleted_at
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
58
 * @property \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Ability[]                                         $abilities
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
59
 * @property-read \Illuminate\Support\Collection                                                                            $all_abilities
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
60
 * @property-read \Rinvex\Country\Country                                                                                   $country
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...
61
 * @property-read \Rinvex\Language\Language                                                                                 $language
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 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...
62
 * @property-read string                                                                                                    $name
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 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...
63
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
64
 * @property \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Role[]                                            $roles
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
65
 * @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Session[]                                    $sessions
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 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...
66
 * @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Socialite[]                                  $socialites
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
67
 *
68
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User role($roles)
69
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereBirthday($value)
70
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereCountryCode($value)
71
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereCreatedAt($value)
72
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereDeletedAt($value)
73
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereEmail($value)
74
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereEmailVerified($value)
75
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereEmailVerifiedAt($value)
76
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereFirstName($value)
77
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereGender($value)
78
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereId($value)
79
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereIsActive($value)
80
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereJobTitle($value)
81
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereLanguageCode($value)
82
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereLastActivity($value)
83
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereLastName($value)
84
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereMiddleName($value)
85
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereNamePrefix($value)
86
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereNameSuffix($value)
87
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User wherePassword($value)
88
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User wherePhone($value)
89
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User wherePhoneVerified($value)
90
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User wherePhoneVerifiedAt($value)
91
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereRememberToken($value)
92
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereTwoFactor($value)
93
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereUpdatedAt($value)
94
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\User whereUsername($value)
95
 * @mixin \Eloquent
96
 */
97
class User extends Model implements UserContract, AuthenticatableContract, AuthenticatableTwoFactorContract, AuthorizableContract, CanResetPasswordContract, CanVerifyEmailContract, CanVerifyPhoneContract
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 203 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...
98
{
99
    use HasRoles;
100
    use Notifiable;
101
    use Authorizable;
102
    use HasHashables;
103
    use CanVerifyEmail;
104
    use CanVerifyPhone;
105
    use Authenticatable;
106
    use ValidatingTrait;
107
    use CanResetPassword;
108
    use CacheableEloquent;
109
    use AuthenticatableTwoFactor;
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    protected $fillable = [
115
        'username',
116
        'password',
117
        'two_factor',
118
        'email',
119
        'email_verified',
120
        'email_verified_at',
121
        'phone',
122
        'phone_verified',
123
        'phone_verified_at',
124
        'name_prefix',
125
        'first_name',
126
        'middle_name',
127
        'last_name',
128
        'name_suffix',
129
        'job_title',
130
        'country_code',
131
        'language_code',
132
        'birthday',
133
        'gender',
134
        'is_active',
135
        'last_activity',
136
        'abilities',
137
        'roles',
138
    ];
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    protected $casts = [
144
        'username' => 'string',
145
        'password' => 'string',
146
        'two_factor' => 'json',
147
        'email' => 'string',
148
        'email_verified' => 'boolean',
149
        'email_verified_at' => 'datetime',
150
        'phone' => 'string',
151
        'phone_verified' => 'boolean',
152
        'phone_verified_at' => 'datetime',
153
        'name_prefix' => 'string',
154
        'first_name' => 'string',
155
        'middle_name' => 'string',
156
        'last_name' => 'string',
157
        'name_suffix' => 'string',
158
        'job_title' => 'string',
159
        'country_code' => 'string',
160
        'language_code' => 'string',
161
        'birthday' => 'string',
162
        'gender' => 'string',
163
        'is_active' => 'boolean',
164
        'last_activity' => 'datetime',
165
        'deleted_at' => 'datetime',
166
    ];
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    protected $hidden = [
172
        'password',
173
        'two_factor',
174
        'remember_token',
175
    ];
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    protected $with = [
181
        'abilities',
182
        'roles',
183
    ];
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    protected $observables = [
189
        'validating',
190
        'validated',
191
    ];
192
193
    /**
194
     * The attributes to be encrypted before saving.
195
     *
196
     * @var array
197
     */
198
    protected $hashables = [
199
        'password',
200
    ];
201
202
    /**
203
     * The default rules that the model will validate against.
204
     *
205
     * @var array
206
     */
207
    protected $rules = [];
208
209
    /**
210
     * Whether the model should throw a
211
     * ValidationException if it fails validation.
212
     *
213
     * @var bool
214
     */
215
    protected $throwValidationExceptions = true;
216
217
    /**
218
     * Create a new Eloquent model instance.
219
     *
220
     * @param array $attributes
221
     */
222
    public function __construct(array $attributes = [])
223
    {
224
        parent::__construct($attributes);
225
226
        $this->setTable(config('rinvex.fort.tables.users'));
227
        $this->setRules([
228
            'username' => 'required|alpha_dash|min:3|max:150|unique:'.config('rinvex.fort.tables.users').',username',
229
            'password' => 'sometimes|required|min:'.config('rinvex.fort.password_min_chars'),
230
            'two_factor' => 'nullable|array',
231
            'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.fort.tables.users').',email',
232
            'email_verified' => 'sometimes|boolean',
233
            'email_verified_at' => 'nullable|date',
234
            'phone' => 'nullable|numeric|min:4',
235
            'phone_verified' => 'sometimes|boolean',
236
            'phone_verified_at' => 'nullable|date',
237
            'name_prefix' => 'nullable|string|max:150',
238
            'first_name' => 'nullable|string|max:150',
239
            'middle_name' => 'nullable|string|max:150',
240
            'last_name' => 'nullable|string|max:150',
241
            'name_suffix' => 'nullable|string|max:150',
242
            'job_title' => 'nullable|string|max:150',
243
            'country_code' => 'nullable|alpha|size:2|country',
244
            'language_code' => 'nullable|alpha|size:2|language',
245
            'birthday' => 'nullable|date_format:Y-m-d',
246
            'gender' => 'nullable|string|in:m,f',
247
            'is_active' => 'sometimes|boolean',
248
            'last_activity' => 'nullable|date',
249
        ]);
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    protected static function boot()
256
    {
257
        parent::boot();
258
259
        static::saving(function (self $user) {
260
            foreach (array_intersect($user->getHashables(), array_keys($user->getAttributes())) as $hashable) {
261
                if ($user->isDirty($hashable) && Hash::needsRehash($user->$hashable)) {
262
                    $user->$hashable = Hash::make($user->$hashable);
263
                }
264
            }
265
        });
266
    }
267
268
    /**
269
     * A user may have multiple direct abilities.
270
     *
271
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
272
     */
273
    public function abilities()
274
    {
275
        return $this->belongsToMany(config('rinvex.fort.models.ability'), config('rinvex.fort.tables.ability_user'), 'user_id', 'ability_id')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 141 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...
276
                    ->withTimestamps();
277
    }
278
279
    /**
280
     * A user may have multiple roles.
281
     *
282
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
283
     */
284
    public function roles()
285
    {
286
        return $this->belongsToMany(config('rinvex.fort.models.role'), config('rinvex.fort.tables.role_user'), 'user_id', 'role_id')
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...
287
                    ->withTimestamps();
288
    }
289
290
    /**
291
     * A user may have many sessions.
292
     *
293
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
294
     */
295
    public function sessions(): HasMany
296
    {
297
        return $this->hasMany(config('rinvex.fort.models.session'), 'user_id', 'id');
298
    }
299
300
    /**
301
     * A user may have multiple socialites.
302
     *
303
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
304
     */
305
    public function socialites(): HasMany
306
    {
307
        return $this->hasMany(config('rinvex.fort.models.socialite'), 'user_id', 'id');
308
    }
309
310
    /**
311
     * Get name attribute.
312
     *
313
     * @return string
314
     */
315
    public function getNameAttribute()
316
    {
317
        $name = trim(implode(' ', [$this->name_prefix, $this->first_name, $this->middle_name, $this->last_name, $this->name_suffix]));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
318
319
        return $name ?: $this->username;
320
    }
321
322
    /**
323
     * Get all abilities of the user.
324
     *
325
     * @return \Illuminate\Support\Collection
326
     */
327
    public function getAllAbilitiesAttribute()
328
    {
329
        return $this->abilities->merge($this->roles->pluck('abilities')->collapse());
330
    }
331
332
    /**
333
     * Determine if the user is super admin.
334
     *
335
     * @return bool
336
     */
337
    public function isSuperadmin()
338
    {
339
        return $this->getAllAbilitiesAttribute()->where('resource', 'global')->where('policy', null)->contains('action', 'superadmin');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
340
    }
341
342
    /**
343
     * Determine if the user is protected.
344
     *
345
     * @return bool
346
     */
347
    public function isProtected()
348
    {
349
        return in_array($this->id, config('rinvex.fort.protected.users'));
350
    }
351
352
    /**
353
     * Route notifications for the authy channel.
354
     *
355
     * @return int
356
     */
357
    public function routeNotificationForAuthy()
358
    {
359
        if (! ($authyId = array_get($this->getTwoFactor(), 'phone.authy_id')) && $this->getEmailForVerification() && $this->getPhoneForVerification() && $this->getCountryForVerification()) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 190 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...
360
            $result = app('rinvex.authy.user')->register($this->getEmailForVerification(), preg_replace('/[^0-9]/', '', $this->getPhoneForVerification()), $this->getCountryForVerification());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 191 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...
361
            $authyId = $result->get('user')['id'];
362
363
            // Prepare required variables
364
            $twoFactor = $this->getTwoFactor();
365
366
            // Update user account
367
            array_set($twoFactor, 'phone.authy_id', $authyId);
368
369
            $this->fill(['two_factor' => $twoFactor])->forceSave();
370
        }
371
372
        return $authyId;
373
    }
374
375
    /**
376
     * Get the user's country.
377
     *
378
     * @return \Rinvex\Country\Country
379
     */
380
    public function getCountryAttribute()
381
    {
382
        return country($this->country_code);
383
    }
384
385
    /**
386
     * Get the user's language.
387
     *
388
     * @return \Rinvex\Language\Language
389
     */
390
    public function getLanguageAttribute()
391
    {
392
        return language($this->language_code);
393
    }
394
395
    /**
396
     * Attach the user roles.
397
     *
398
     * @param mixed $roles
399
     *
400
     * @return void
401
     */
402
    public function setRolesAttribute($roles)
403
    {
404
        static::saved(function (self $model) use ($roles) {
405
            $model->roles()->sync($roles);
406
        });
407
    }
408
409
    /**
410
     * Attach the user abilities.
411
     *
412
     * @param mixed $abilities
413
     *
414
     * @return void
415
     */
416
    public function setAbilitiesAttribute($abilities)
417
    {
418
        static::saved(function (self $model) use ($abilities) {
419
            $model->abilities()->sync($abilities);
420
        });
421
    }
422
423
    /**
424
     * Activate the user.
425
     *
426
     * @return $this
427
     */
428
    public function activate()
429
    {
430
        $this->update(['is_active' => true]);
431
432
        return $this;
433
    }
434
435
    /**
436
     * Deactivate the user.
437
     *
438
     * @return $this
439
     */
440
    public function deactivate()
441
    {
442
        $this->update(['is_active' => false]);
443
444
        return $this;
445
    }
446
}
447