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

User::getNameFullAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 4
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 8
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,
0 ignored issues
show
Bug introduced by
The property first_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...
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,
0 ignored issues
show
Bug introduced by
The property last_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...
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
     * Retrieve the User's name (first & last).
322
     *
323
     * @return string
324
     */
325
    public function getNameAttribute(): string
326
    {
327
        // Use nickname instead of first if it is set & preferred
328
        $first = (isset($this->nickname) && $this->isNicknamePreferred()) ? $this->nickname : $this->first_name;
0 ignored issues
show
Bug introduced by
The property first_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...
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...
329
330
        // Return concatenated name
331
        return "{$first} {$this->last_name}";
0 ignored issues
show
Bug introduced by
The property last_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...
332
    }
333
334
    /**
335
     * Retrieve the User's full name with middle initial.
336
     *
337
     * @return string
338
     */
339
    public function getNameFullAttribute(): string
340
    {
341
        $name = $this->first_name;
0 ignored issues
show
Bug introduced by
The property first_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...
342
        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...
343
            $name .= ' '.$this->middle_name;
344
        }
345
346
        return "{$name} {$this->last_name}";
0 ignored issues
show
Bug introduced by
The property last_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...
347
    }
348
349
    /**
350
     * Retrieve the User's name with their suffix.
351
     *
352
     * @return string
353
     */
354
    public function getNameSuffixAttribute(): string
355
    {
356
        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...
357
    }
358
359
    /**
360
     * Get the 'list_name' attribute.
361
     *
362
     * @return string
363
     */
364
    public function getListNameAttribute()
365
    {
366
        return implode(', ', array_reverse(explode(' ', $this->name)));
367
    }
368
369
    /**
370
     * Get the 'name_link' attribute that returns a url to the User's team.show page.
371
     *
372
     * @return string
373
     */
374
    public function getNameLinkAttribute()
375
    {
376
        return '<a href="'.route('user.show', ['user'=>$this->id]).'">'.$this->name.'</a>';
377
    }
378
379
    // todo: add address accessor method to a sfneal/address trait
380
381
    /**
382
     * Retrieve the User's 'address1' attribute.
383
     *
384
     * @return mixed
385
     */
386
    public function getAddress1Attribute()
387
    {
388
        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...
389
    }
390
391
    /**
392
     * Retrieve the User's 'address2' attribute.
393
     *
394
     * @return mixed
395
     */
396
    public function getAddress2Attribute()
397
    {
398
        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...
399
    }
400
401
    /**
402
     * Retrieve the User's 'city' attribute.
403
     *
404
     * @return mixed
405
     */
406
    public function getCityAttribute()
407
    {
408
        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...
409
    }
410
411
    /**
412
     * Retrieve the User's 'state' attribute.
413
     *
414
     * @return mixed
415
     */
416
    public function getStateAttribute()
417
    {
418
        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...
419
    }
420
421
    /**
422
     * Retrieve the User's 'zip' attribute.
423
     *
424
     * @return mixed
425
     */
426
    public function getZipAttribute()
427
    {
428
        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...
429
    }
430
431
    /**
432
     * Retrieve an email link.
433
     *
434
     * @return string
435
     */
436
    public function getEmailLinkAttribute(): string
437
    {
438
        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...
439
    }
440
441
    /**
442
     * Retrieve a work phone link.
443
     *
444
     * @return string
445
     */
446
    public function getPhoneWorkLinkAttribute(): string
447
    {
448
        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...
449
    }
450
451
    /**
452
     * Retrieve a mobile phone link.
453
     *
454
     * @return string
455
     */
456
    public function getPhoneMobileLinkAttribute(): string
457
    {
458
        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...
459
    }
460
461
    /**
462
     * Retrieve the User's rate formatted as dollars.
463
     *
464
     * @return string
465
     */
466
    public function getRateFormattedAttribute(): string
467
    {
468
        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...
469
    }
470
471
    /**
472
     * Retrieve the raw 'text' attribute with newline chars.
473
     *
474
     * @return mixed
475
     */
476
    public function getTextareaAttribute()
477
    {
478
        return $this->attributes['bio'];
479
    }
480
481
    /**
482
     * Retrieve a User's custom email footer.
483
     *
484
     * @return string
485
     */
486
    public function getEmailFooterAttribute(): string
487
    {
488
        $footer = "{$this->name}";
489
        $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...
490
        $footer .= "\n".OrganizationService::name() ?? '';
491
        $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...
492
        $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...
493
494
        return $footer;
495
    }
496
}
497