Passed
Pull Request — master (#17)
by Stephen
14:26
created

User::getEmailLinkAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 1
c 2
b 1
f 0
nc 2
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
        'bio' => NewlineCast::class,
90
        'rate' => 'int',
91
        'status' => 'int',
92
    ];
93
94
    /**
95
     * Create a new factory instance for the model.
96
     *
97
     * @return UserFactory
98
     */
99
    protected static function newFactory(): UserFactory
100
    {
101
        return new UserFactory();
102
    }
103
104
    /**
105
     * Query Builder.
106
     * @param $query
107
     * @return UserBuilder
108
     */
109
    public function newEloquentBuilder($query)
110
    {
111
        return new UserBuilder($query);
112
    }
113
114
    /**
115
     * Custom User query Builder.
116
     *
117
     * @return UserBuilder|Builder
118
     */
119
    public static function query(): UserBuilder
120
    {
121
        return parent::query();
122
    }
123
124
    /**
125
     * User's 'role' relationship.
126
     *
127
     * @return BelongsTo
128
     */
129
    public function role()
130
    {
131
        return $this->belongsTo(Role::class, 'role_id', 'role_id');
132
    }
133
134
    /**
135
     * User's Notification Subscriptions.
136
     *
137
     * @return HasMany
138
     */
139
    public function notificationSubscriptions()
140
    {
141
        return $this->hasMany(UserNotification::class, 'user_id', 'id');
142
    }
143
144
    /**
145
     * User's 'team' relationship - indicates user is a member of the public team.
146
     *
147
     * @return HasOne
148
     */
149
    public function team()
150
    {
151
        return $this->hasOne(Team::class, 'user_id', 'id');
152
    }
153
154
    /**
155
     * User's address.
156
     *
157
     * @return MorphOne|Address
158
     */
159
    public function address()
160
    {
161
        return $this->morphOne(Address::class, 'addressable');
162
    }
163
164
    /**
165
     * Determine if a User has a particular 'role_id'.
166
     *
167
     * @param int $role_id
168
     * @return bool
169
     */
170
    public function isRoleId(int $role_id): bool
171
    {
172
        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...
173
    }
174
175
    /**
176
     * Determine if a User has a particular 'role name'.
177
     *
178
     * @param string $role
179
     * @return bool
180
     */
181
    public function isRole(string $role): bool
182
    {
183
        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...
184
    }
185
186
    /**
187
     * Determine if a User has an 'admin' role.
188
     *
189
     * @return bool
190
     */
191
    public function isAdmin(): bool
192
    {
193
        // User is considered an 'admin' if he/she is a 'web developer'
194
        return $this->isRoleId(3) || $this->isRoleId(4);
195
    }
196
197
    /**
198
     * Determine if a User has a 'web developer' role.
199
     *
200
     * @return bool
201
     */
202
    public function isWebDeveloper(): bool
203
    {
204
        return $this->isRoleId(4);
205
    }
206
207
    /**
208
     * Determine if a User has an 'employee' role.
209
     *
210
     * @return bool
211
     */
212
    public function isEmployee(): bool
213
    {
214
        return $this->isRoleId(1);
215
    }
216
217
    /**
218
     * Determine if a User has an 'contractor' role.
219
     *
220
     * @return bool
221
     */
222
    public function isContractor(): bool
223
    {
224
        return $this->isRoleId(2);
225
    }
226
227
    /**
228
     * Determine if a User is 'active'.
229
     *
230
     * @return bool
231
     */
232
    public function isActive(): bool
233
    {
234
        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...
235
    }
236
237
    /**
238
     * Determine if a User's $nickname is preferred over their $first_name.
239
     *
240
     * @return bool
241
     */
242
    public function isNicknamePreferred(): bool
243
    {
244
        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...
245
    }
246
247
    /**
248
     * Retrieve a User's initials.
249
     *
250
     * @return string
251
     */
252
    public function getInitialsAttribute()
253
    {
254
        return implodeFiltered('', collect([
255
            $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...
256
            $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...
257
            $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...
258
        ])->map(function ($name) {
259
            return substr($name, 0, 1);
260
        })->toArray());
261
    }
262
263
    /**
264
     * Get the AWS S3 file upload directory for an Inquiry model by retrieving the table name and primary key.
265
     *
266
     * @param string $base_dir
267
     * @return string
268
     */
269
    public function getUploadDirectory($base_dir = 'images'): string
270
    {
271
        return $base_dir.'/'.str_replace('_', '-', $this->getTable()).'/'.$this->getKey();
272
    }
273
274
    /**
275
     * Get the 'city_state' attribute.
276
     *
277
     * @return string
278
     */
279
    public function getCityStateAttribute()
280
    {
281
        return $this->address->city_state ?? null;
282
    }
283
284
    /**
285
     * Set the 'password' attribute.
286
     *
287
     * @param $value
288
     */
289
    public function setPasswordAttribute($value)
290
    {
291
        if (! empty($value)) {
292
            $this->attributes['password'] = bcrypt($value);
293
        }
294
    }
295
296
    /**
297
     * Mutate the 'middle_name' attribute.
298
     *
299
     * @param string|null $value
300
     */
301
    public function setMiddleNameAttribute(string $value = null)
302
    {
303
        if (! is_null($value)) {
304
            // Remove leading & trailing whitespace
305
            $middle_name = trim($value);
306
307
            // Append '.' to the middle name if's a single letter
308
            $this->attributes['middle_name'] = strlen($middle_name) == 1 ? "{$middle_name}." : $middle_name;
309
        }
310
    }
311
312
    /**
313
     * Retrieve the User's name (first & last).
314
     *
315
     * @return string
316
     */
317
    public function getNameAttribute(): string
318
    {
319
        // Use nickname instead of first if it is set & preferred
320
        $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...
321
322
        // Return concatenated name
323
        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...
324
    }
325
326
    /**
327
     * Retrieve the User's full name with middle initial.
328
     *
329
     * @return string
330
     */
331
    public function getNameFullAttribute(): string
332
    {
333
        $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...
334
        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...
335
            $name .= ' '.$this->middle_name;
336
        }
337
338
        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...
339
    }
340
341
    /**
342
     * Retrieve the User's name with their suffix.
343
     *
344
     * @return string
345
     */
346
    public function getNameSuffixAttribute(): string
347
    {
348
        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...
349
    }
350
351
    /**
352
     * Get the 'list_name' attribute.
353
     *
354
     * @return string
355
     */
356
    public function getListNameAttribute()
357
    {
358
        return implode(', ', array_reverse(explode(' ', $this->name)));
359
    }
360
361
    /**
362
     * Get the 'name_link' attribute that returns a url to the User's team.show page.
363
     *
364
     * @return string
365
     */
366
    public function getNameLinkAttribute()
367
    {
368
        return '<a href="'.route('user.show', ['user'=>$this->id]).'">'.$this->name.'</a>';
369
    }
370
371
    // todo: add address accessor method to a sfneal/address trait
372
373
    /**
374
     * Retrieve the User's 'address1' attribute.
375
     *
376
     * @return mixed
377
     */
378
    public function getAddress1Attribute()
379
    {
380
        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...
381
    }
382
383
    /**
384
     * Retrieve the User's 'address2' attribute.
385
     *
386
     * @return mixed
387
     */
388
    public function getAddress2Attribute()
389
    {
390
        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...
391
    }
392
393
    /**
394
     * Retrieve the User's 'city' attribute.
395
     *
396
     * @return mixed
397
     */
398
    public function getCityAttribute()
399
    {
400
        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...
401
    }
402
403
    /**
404
     * Retrieve the User's 'state' attribute.
405
     *
406
     * @return mixed
407
     */
408
    public function getStateAttribute()
409
    {
410
        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...
411
    }
412
413
    /**
414
     * Retrieve the User's 'zip' attribute.
415
     *
416
     * @return mixed
417
     */
418
    public function getZipAttribute()
419
    {
420
        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...
421
    }
422
423
    /**
424
     * Retrieve an email link.
425
     *
426
     * @return string
427
     */
428
    public function getEmailLinkAttribute(): string
429
    {
430
        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...
431
    }
432
433
    /**
434
     * Retrieve a work phone link.
435
     *
436
     * @return string
437
     */
438
    public function getPhoneWorkLinkAttribute(): string
439
    {
440
        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...
441
    }
442
443
    /**
444
     * Retrieve a mobile phone link.
445
     *
446
     * @return string
447
     */
448
    public function getPhoneMobileLinkAttribute(): string
449
    {
450
        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...
451
    }
452
453
    /**
454
     * Retrieve the User's rate formatted as dollars.
455
     *
456
     * @return string
457
     */
458
    public function getRateFormattedAttribute(): string
459
    {
460
        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...
461
    }
462
463
    /**
464
     * Retrieve the raw 'text' attribute with newline chars.
465
     *
466
     * @return mixed
467
     */
468
    public function getTextareaAttribute()
469
    {
470
        return $this->attributes['bio'];
471
    }
472
473
    /**
474
     * Retrieve a User's custom email footer.
475
     *
476
     * @return string
477
     */
478
    public function getEmailFooterAttribute(): string
479
    {
480
        $footer = "{$this->name}";
481
        $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...
482
        $footer .= "\n".OrganizationService::name() ?? '';
483
        $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...
484
        $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...
485
486
        return $footer;
487
    }
488
}
489