Passed
Pull Request — master (#17)
by Stephen
04:31
created

User::getAddress1Attribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Users\Models;
4
5
use Database\Factories\UserFactory;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Factories\HasFactory;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
use Illuminate\Database\Eloquent\Relations\HasOne;
11
use Illuminate\Database\Eloquent\Relations\MorphOne;
12
use Sfneal\Address\Models\Address;
13
use Sfneal\Casts\NewlineCast;
14
use Sfneal\Currency\Currency;
15
use Sfneal\Models\AuthModel;
16
use Sfneal\Scopes\OrderScope;
17
use Sfneal\Users\Builders\UserBuilder;
18
use Sfneal\Users\Scopes\UserActiveScope;
19
use Sfneal\Users\Services\OrganizationService;
20
use Vkovic\LaravelCustomCasts\HasCustomCasts;
21
22
class User extends AuthModel
23
{
24
    // todo: refactor status to use Status model?
25
    use HasCustomCasts;
26
    use HasFactory;
27
28
    /**
29
     * The "booting" method of the model.
30
     *
31
     * @return void
32
     */
33
    public static function boot()
34
    {
35
        parent::boot();
36
37
        // Global scopes
38
        static::addGlobalScope(new UserActiveScope());
39
        static::addGlobalScope(new OrderScope('last_name', 'asc'));
40
    }
41
42
    protected $dates = ['deleted_at'];
43
    protected $table = 'user';
44
    protected $primaryKey = 'id';
45
46
    /**
47
     * The attributes that are mass assignable.
48
     *
49
     * @var array
50
     */
51
    protected $fillable = [
52
        'role_id',
53
        'first_name',
54
        'middle_name',
55
        'last_name',
56
        'nickname',
57
        'nickname_preferred',
58
        'title',
59
        'suffix',
60
        'email',
61
        'phone_work',
62
        'phone_mobile',
63
        'fax',
64
        'website',
65
        'bio',
66
        'username',
67
        'password',
68
        'status',
69
        'rate',
70
    ];
71
72
    /**
73
     * The attributes that should be hidden from arrays.
74
     *
75
     * @var array
76
     */
77
    protected $hidden = [
78
        'password',
79
        'remember_token',
80
    ];
81
82
    /**
83
     * The attributes that should type cast.
84
     *
85
     * @var array
86
     */
87
    protected $casts = [
88
        'role_id' => 'int',
89
        'nickname_preferred' => 'int',
90
        'bio' => NewlineCast::class,
91
        'status' => 'int',
92
        'rate' => 'int',
93
    ];
94
95
    /**
96
     * @var array Attributes that should be appended to collections
97
     */
98
    protected $appends = [
99
        'name',
100
    ];
101
102
    /**
103
     * Create a new factory instance for the model.
104
     *
105
     * @return UserFactory
106
     */
107
    protected static function newFactory(): UserFactory
108
    {
109
        return new UserFactory();
110
    }
111
112
    /**
113
     * Query Builder.
114
     * @param $query
115
     * @return UserBuilder
116
     */
117
    public function newEloquentBuilder($query)
118
    {
119
        return new UserBuilder($query);
120
    }
121
122
    /**
123
     * Custom User query Builder.
124
     *
125
     * @return UserBuilder|Builder
126
     */
127
    public static function query(): UserBuilder
128
    {
129
        return parent::query();
130
    }
131
132
    /**
133
     * User's 'role' relationship.
134
     *
135
     * @return BelongsTo
136
     */
137
    public function role()
138
    {
139
        return $this->belongsTo(Role::class, 'role_id', 'role_id');
140
    }
141
142
    /**
143
     * User's Notification Subscriptions.
144
     *
145
     * @return HasMany
146
     */
147
    public function notificationSubscriptions()
148
    {
149
        return $this->hasMany(UserNotification::class, 'user_id', 'id');
150
    }
151
152
    /**
153
     * User's 'team' relationship - indicates user is a member of the public team.
154
     *
155
     * @return HasOne
156
     */
157
    public function team()
158
    {
159
        return $this->hasOne(Team::class, 'user_id', 'id');
160
    }
161
162
    /**
163
     * User's address.
164
     *
165
     * @return MorphOne|Address
166
     */
167
    public function address()
168
    {
169
        return $this->morphOne(Address::class, 'addressable');
170
    }
171
172
    /**
173
     * Determine if a User has a particular 'role_id'.
174
     *
175
     * @param int $role_id
176
     * @return bool
177
     */
178
    public function isRoleId(int $role_id): bool
179
    {
180
        return $this->role_id == $role_id;
0 ignored issues
show
Bug introduced by
The property role_id does not exist on Sfneal\Users\Models\User. Did you mean role?
Loading history...
181
    }
182
183
    /**
184
     * Determine if a User has a particular 'role name'.
185
     *
186
     * @param string $role
187
     * @return bool
188
     */
189
    public function isRole(string $role): bool
190
    {
191
        return strtolower($this->role->name) == strtolower($role);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Sfneal\Users\Models\Role. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
192
    }
193
194
    /**
195
     * Determine if a User has an 'admin' role.
196
     *
197
     * @return bool
198
     */
199
    public function isAdmin(): bool
200
    {
201
        // User is considered an 'admin' if user is a 'web developer'
202
        return $this->isRoleId(3) || $this->isRoleId(4);
203
    }
204
205
    /**
206
     * Determine if a User has a 'web developer' role.
207
     *
208
     * @return bool
209
     */
210
    public function isWebDeveloper(): bool
211
    {
212
        return $this->isRoleId(4);
213
    }
214
215
    /**
216
     * Determine if a User has an 'employee' role.
217
     *
218
     * @return bool
219
     */
220
    public function isEmployee(): bool
221
    {
222
        return $this->isRoleId(1);
223
    }
224
225
    /**
226
     * Determine if a User has an 'contractor' role.
227
     *
228
     * @return bool
229
     */
230
    public function isContractor(): bool
231
    {
232
        return $this->isRoleId(2);
233
    }
234
235
    /**
236
     * Determine if a User is 'active'.
237
     *
238
     * @return bool
239
     */
240
    public function isActive(): bool
241
    {
242
        return $this->status == 1;
0 ignored issues
show
Bug introduced by
The property status does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
243
    }
244
245
    /**
246
     * Determine if a User's $nickname is preferred over their $first_name.
247
     *
248
     * @return bool
249
     */
250
    public function isNicknamePreferred(): bool
251
    {
252
        return $this->nickname_preferred == 1;
0 ignored issues
show
Bug introduced by
The property nickname_preferred does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
253
    }
254
255
    /**
256
     * Retrieve a User's initials.
257
     *
258
     * @return string
259
     */
260
    public function getInitialsAttribute()
261
    {
262
        return implodeFiltered('', collect([
263
            $this->first_name,
264
            $this->middle_name,
0 ignored issues
show
Bug introduced by
The property middle_name does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
265
            $this->last_name,
266
        ])->map(function ($name) {
267
            return substr($name, 0, 1);
268
        })->toArray());
269
    }
270
271
    /**
272
     * Get the AWS S3 file upload directory for an Inquiry model by retrieving the table name and primary key.
273
     *
274
     * @param string $base_dir
275
     * @return string
276
     */
277
    public function getUploadDirectory($base_dir = 'images'): string
278
    {
279
        return $base_dir.'/'.str_replace('_', '-', $this->getTable()).'/'.$this->getKey();
280
    }
281
282
    /**
283
     * Get the 'city_state' attribute.
284
     *
285
     * @return string
286
     */
287
    public function getCityStateAttribute()
288
    {
289
        return $this->address->city_state ?? null;
290
    }
291
292
    /**
293
     * Set the 'password' attribute.
294
     *
295
     * @param $value
296
     */
297
    public function setPasswordAttribute($value)
298
    {
299
        if (! empty($value)) {
300
            $this->attributes['password'] = bcrypt($value);
301
        }
302
    }
303
304
    /**
305
     * Mutate the 'middle_name' attribute.
306
     *
307
     * @param string|null $value
308
     */
309
    public function setMiddleNameAttribute(string $value = null)
310
    {
311
        if (! is_null($value)) {
312
            // Remove leading & trailing whitespace
313
            $middle_name = trim($value);
314
315
            // Append '.' to the middle name if's a single letter
316
            $this->attributes['middle_name'] = strlen($middle_name) == 1 ? "{$middle_name}." : $middle_name;
317
        }
318
    }
319
320
    /**
321
     * Mutate the 'first_name' attribute.
322
     *
323
     * @param string|null $value
324
     */
325
    public function setFirstNameAttribute(string $value = null)
326
    {
327
        if (! is_null($value)) {
328
            $this->attributes['first_name'] = trim($value);
329
        }
330
    }
331
332
    /**
333
     * Mutate the 'last_name' attribute.
334
     *
335
     * @param string|null $value
336
     */
337
    public function setLastNameAttribute(string $value = null)
338
    {
339
        if (! is_null($value)) {
340
            $this->attributes['last_name'] = trim($value);
341
        }
342
    }
343
344
    /**
345
     * Access the 'first_name' attribute.
346
     *
347
     * @param string|null $value
348
     * @return string
349
     */
350
    public function getFirstNameAttribute(string $value = null): string
351
    {
352
        return trim($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

352
        return trim(/** @scrutinizer ignore-type */ $value);
Loading history...
353
    }
354
355
    /**
356
     * Access the 'last_name' attribute.
357
     *
358
     * @param string|null $value
359
     * @return string
360
     */
361
    public function getLastNameAttribute(string $value = null): string
362
    {
363
        return trim($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

363
        return trim(/** @scrutinizer ignore-type */ $value);
Loading history...
364
    }
365
366
    /**
367
     * Retrieve the User's name (first & last).
368
     *
369
     * @return string
370
     */
371
    public function getNameAttribute(): string
372
    {
373
        // Use nickname instead of first if it is set & preferred
374
        $first = (isset($this->nickname) && $this->isNicknamePreferred()) ? $this->nickname : $this->first_name;
0 ignored issues
show
Bug introduced by
The property nickname does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
375
376
        // Return concatenated name
377
        return "{$first} {$this->last_name}";
378
    }
379
380
    /**
381
     * Retrieve the User's full name with middle initial.
382
     *
383
     * @return string
384
     */
385
    public function getNameFullAttribute(): string
386
    {
387
        $name = $this->first_name;
388
        if ($this->middle_name) {
0 ignored issues
show
Bug introduced by
The property middle_name does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
389
            $name .= ' '.$this->middle_name;
390
        }
391
392
        return "{$name} {$this->last_name}";
393
    }
394
395
    /**
396
     * Retrieve the User's name with their suffix.
397
     *
398
     * @return string
399
     */
400
    public function getNameSuffixAttribute(): string
401
    {
402
        return $this->name_full.($this->suffix ? ", {$this->suffix}" : '');
0 ignored issues
show
Bug introduced by
The property suffix does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
403
    }
404
405
    /**
406
     * Get the 'list_name' attribute.
407
     *
408
     * @return string
409
     */
410
    public function getListNameAttribute()
411
    {
412
        return implode(', ', array_reverse(explode(' ', $this->name)));
413
    }
414
415
    /**
416
     * Get the 'name_link' attribute that returns a url to the User's team.show page.
417
     *
418
     * @return string
419
     */
420
    public function getNameLinkAttribute()
421
    {
422
        return '<a href="'.route('user.show', ['user'=>$this->id]).'">'.$this->name.'</a>';
423
    }
424
425
    // todo: add address accessor method to a sfneal/address trait
426
427
    /**
428
     * Retrieve the User's 'address1' attribute.
429
     *
430
     * @return mixed
431
     */
432
    public function getAddress1Attribute()
433
    {
434
        return $this->address->address_1 ?? null;
0 ignored issues
show
Bug introduced by
The property address_1 does not exist on Sfneal\Address\Models\Address. Did you mean addressable?
Loading history...
435
    }
436
437
    /**
438
     * Retrieve the User's 'address2' attribute.
439
     *
440
     * @return mixed
441
     */
442
    public function getAddress2Attribute()
443
    {
444
        return $this->address->address_2 ?? null;
0 ignored issues
show
Bug introduced by
The property address_2 does not exist on Sfneal\Address\Models\Address. Did you mean addressable?
Loading history...
445
    }
446
447
    /**
448
     * Retrieve the User's 'city' attribute.
449
     *
450
     * @return mixed
451
     */
452
    public function getCityAttribute()
453
    {
454
        return $this->address->city ?? null;
0 ignored issues
show
Bug introduced by
The property city does not seem to exist on Sfneal\Address\Models\Address. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
455
    }
456
457
    /**
458
     * Retrieve the User's 'state' attribute.
459
     *
460
     * @return mixed
461
     */
462
    public function getStateAttribute()
463
    {
464
        return $this->address->state ?? null;
0 ignored issues
show
Bug introduced by
The property state does not seem to exist on Sfneal\Address\Models\Address. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
465
    }
466
467
    /**
468
     * Retrieve the User's 'zip' attribute.
469
     *
470
     * @return mixed
471
     */
472
    public function getZipAttribute()
473
    {
474
        return $this->address->zip ?? null;
0 ignored issues
show
Bug introduced by
The property zip does not seem to exist on Sfneal\Address\Models\Address. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
475
    }
476
477
    /**
478
     * Retrieve an email link.
479
     *
480
     * @return string
481
     */
482
    public function getEmailLinkAttribute(): string
483
    {
484
        return $this->email ? ('mailto:'.$this->email) : '#!';
0 ignored issues
show
Bug introduced by
The property email does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
485
    }
486
487
    /**
488
     * Retrieve a work phone link.
489
     *
490
     * @return string
491
     */
492
    public function getPhoneWorkLinkAttribute(): string
493
    {
494
        return $this->phone_work ? ('tel:'.$this->phone_work) : '#!';
0 ignored issues
show
Bug introduced by
The property phone_work does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
495
    }
496
497
    /**
498
     * Retrieve a mobile phone link.
499
     *
500
     * @return string
501
     */
502
    public function getPhoneMobileLinkAttribute(): string
503
    {
504
        return $this->phone_mobile ? ('tel:'.$this->phone_mobile) : '#!';
0 ignored issues
show
Bug introduced by
The property phone_mobile does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
505
    }
506
507
    /**
508
     * Retrieve the User's rate formatted as dollars.
509
     *
510
     * @return string
511
     */
512
    public function getRateFormattedAttribute(): string
513
    {
514
        return (! empty($this->rate)) ? Currency::dollars($this->rate) : '-';
0 ignored issues
show
Bug introduced by
The property rate does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
515
    }
516
517
    /**
518
     * Retrieve the raw 'text' attribute with newline chars.
519
     *
520
     * @return mixed
521
     */
522
    public function getTextareaAttribute()
523
    {
524
        return $this->attributes['bio'];
525
    }
526
527
    /**
528
     * Retrieve a User's custom email footer.
529
     *
530
     * @return string
531
     */
532
    public function getEmailFooterAttribute(): string
533
    {
534
        $footer = "{$this->name}";
535
        $footer .= $this->title ? "\n{$this->title}" : '';
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
536
        $footer .= "\n".OrganizationService::name() ?? '';
537
        $footer .= "\n".($this->phone_work ?? OrganizationService::phone()) ?? '';
0 ignored issues
show
Bug introduced by
The property phone_work does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
538
        $footer .= $this->email ? "\n{$this->email}" : '';
0 ignored issues
show
Bug introduced by
The property email does not seem to exist on Sfneal\Users\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
539
540
        return $footer;
541
    }
542
}
543