Passed
Push — master ( b5e2c5...4306bb )
by Stephen
23:29
created

User::isWebDeveloper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Users\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Database\Eloquent\Relations\HasMany;
8
use Illuminate\Database\Eloquent\Relations\HasOne;
9
use Illuminate\Database\Eloquent\Relations\MorphOne;
10
use Sfneal\Address\Models\Address;
11
use Sfneal\Casts\NewlineCast;
12
use Sfneal\Currency\FormatDollars;
13
use Sfneal\Models\AbstractAuthenticatable;
14
use Sfneal\Scopes\OrderScope;
15
use Sfneal\Users\Builders\UserBuilder;
16
use Sfneal\Users\Scopes\UserActiveScope;
17
use Sfneal\Users\Services\OrganizationService;
18
use Vkovic\LaravelCustomCasts\HasCustomCasts;
19
20
class User extends AbstractAuthenticatable
21
{
22
    // todo: refactor status to use Status model?
23
    use HasCustomCasts;
24
25
    /**
26
     * The "booting" method of the model.
27
     *
28
     * @return void
29
     */
30
    public static function boot()
31
    {
32
        parent::boot();
33
34
        // Global scopes
35
        static::addGlobalScope(new UserActiveScope());
36
        static::addGlobalScope(new OrderScope('last_name', 'asc'));
37
    }
38
39
    protected $dates = ['deleted_at'];
40
    protected $table = 'user';
41
    protected $primaryKey = 'id';
42
    protected $connection = 'mysql';
43
44
    /**
45
     * The attributes that are mass assignable.
46
     *
47
     * @var array
48
     */
49
    protected $fillable = [
50
        'role_id',
51
        'first_name',
52
        'middle_name',
53
        'last_name',
54
        'nickname',
55
        'nickname_preferred',
56
        'title',
57
        'suffix',
58
        'email',
59
        'phone_work',
60
        'phone_mobile',
61
        'fax',
62
        'website',
63
        'bio',
64
        'username',
65
        'password',
66
        'status',
67
        'rate',
68
    ];
69
70
    /**
71
     * The attributes that should be hidden from arrays.
72
     *
73
     * @var array
74
     */
75
    protected $hidden = [
76
        'password',
77
        'remember_token',
78
    ];
79
80
    /**
81
     * The attributes that should type cast.
82
     *
83
     * @var array
84
     */
85
    protected $casts = [
86
        'bio' => NewlineCast::class,
87
    ];
88
89
    /**
90
     * Query Builder.
91
     * @param $query
92
     * @return UserBuilder
93
     */
94
    public function newEloquentBuilder($query)
95
    {
96
        return new UserBuilder($query);
97
    }
98
99
    /**
100
     * Custom User query Builder.
101
     *
102
     * @return UserBuilder|Builder
103
     */
104
    public static function query(): UserBuilder
105
    {
106
        return parent::query();
107
    }
108
109
    /**
110
     * User's 'role' relationship.
111
     *
112
     * @return BelongsTo
113
     */
114
    public function role()
115
    {
116
        return $this->belongsTo(Role::class, 'role_id', 'role_id');
117
    }
118
119
    /**
120
     * User's Notification Subscriptions.
121
     *
122
     * @return HasMany
123
     */
124
    public function notificationSubscriptions()
125
    {
126
        return $this->hasMany(UserNotification::class, 'user_id', 'id');
127
    }
128
129
    /**
130
     * User's 'team' relationship - indicates user is a member of the public team.
131
     *
132
     * @return HasOne
133
     */
134
    public function team()
135
    {
136
        return $this->hasOne(Team::class, 'user_id', 'id');
137
    }
138
139
    /**
140
     * User's address.
141
     *
142
     * @return MorphOne|Address
143
     */
144
    public function address()
145
    {
146
        return $this->morphOne(Address::class, 'addressable');
147
    }
148
149
    /**
150
     * Determine if a User has a particular 'role_id'.
151
     *
152
     * @param int $role_id
153
     * @return bool
154
     */
155
    public function isRoleId(int $role_id): bool
156
    {
157
        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...
158
    }
159
160
    /**
161
     * Determine if a User has a particular 'role name'.
162
     *
163
     * @param string $role
164
     * @return bool
165
     */
166
    public function isRole(string $role): bool
167
    {
168
        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...
169
    }
170
171
    /**
172
     * Determine if a User has an 'admin' role.
173
     *
174
     * @return bool
175
     */
176
    public function isAdmin(): bool
177
    {
178
        // User is considered an 'admin' if he/she is a 'web developer'
179
        return $this->isRoleId(3) || $this->isRoleId(4);
180
    }
181
182
    /**
183
     * Determine if a User has a 'web developer' role.
184
     *
185
     * @return bool
186
     */
187
    public function isWebDeveloper(): bool
188
    {
189
        return $this->isRoleId(4);
190
    }
191
192
    /**
193
     * Determine if a User has an 'employee' role.
194
     *
195
     * @return bool
196
     */
197
    public function isEmployee(): bool
198
    {
199
        return $this->isRoleId(1);
200
    }
201
202
    /**
203
     * Determine if a User has an 'contractor' role.
204
     *
205
     * @return bool
206
     */
207
    public function isContractor(): bool
208
    {
209
        return $this->isRoleId(2);
210
    }
211
212
    /**
213
     * Determine if a User is 'active'.
214
     *
215
     * @return bool
216
     */
217
    public function isActive(): bool
218
    {
219
        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...
220
    }
221
222
    /**
223
     * Determine if a User's $nickname is preferred over their $first_name.
224
     *
225
     * @return bool
226
     */
227
    public function isNicknamePreferred(): bool
228
    {
229
        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...
230
    }
231
232
    /**
233
     * Retrieve a User's initials.
234
     *
235
     * @return string
236
     */
237
    public function getInitialsAttribute()
238
    {
239
        return implodeFiltered('', collect([
240
            $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...
241
            $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...
242
            $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...
243
        ])->map(function ($name) {
244
            return substr($name, 0, 1);
245
        })->toArray());
246
    }
247
248
    /**
249
     * Get the AWS S3 file upload directory for an Inquiry model by retrieving the table name and primary key.
250
     *
251
     * @param string $base_dir
252
     * @return string
253
     */
254
    public function getUploadDirectory($base_dir = 'images'): string
255
    {
256
        return $base_dir.'/'.str_replace('_', '-', $this->getTable()).'/'.$this->getKey();
257
    }
258
259
    /**
260
     * Get the 'city_state' attribute.
261
     *
262
     * @return string
263
     */
264
    public function getCityStateAttribute()
265
    {
266
        return $this->address->city_state ?? null;
267
    }
268
269
    /**
270
     * Set the 'password' attribute.
271
     *
272
     * @param $value
273
     */
274
    public function setPasswordAttribute($value)
275
    {
276
        if (! empty($value)) {
277
            $this->attributes['password'] = bcrypt($value);
278
        }
279
    }
280
281
    /**
282
     * Mutate the 'middle_name' attribute.
283
     *
284
     * @param string|null $value
285
     */
286
    public function setMiddleNameAttribute(string $value = null)
287
    {
288
        if (! is_null($value)) {
289
            // Remove leading & trailing whitespace
290
            $middle_name = trim($value);
291
292
            // Append '.' to the middle name if's a single letter
293
            $this->attributes['middle_name'] = strlen($middle_name) == 1 ? "{$middle_name}." : $middle_name;
294
        }
295
    }
296
297
    /**
298
     * Retrieve the User's name (first & last).
299
     *
300
     * @return string
301
     */
302
    public function getNameAttribute(): string
303
    {
304
        // Use nickname instead of first if it is set & preferred
305
        $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...
306
307
        // Return concatenated name
308
        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...
309
    }
310
311
    /**
312
     * Retrieve the User's full name with middle initial.
313
     *
314
     * @return string
315
     */
316
    public function getNameFullAttribute(): string
317
    {
318
        $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...
319
        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...
320
            $name .= ' '.$this->middle_name;
321
        }
322
323
        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...
324
    }
325
326
    /**
327
     * Retrieve the User's name with their suffix.
328
     *
329
     * @return string
330
     */
331
    public function getNameSuffixAttribute(): string
332
    {
333
        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...
334
    }
335
336
    /**
337
     * Get the 'list_name' attribute.
338
     *
339
     * @return string
340
     */
341
    public function getListNameAttribute()
342
    {
343
        return implode(', ', array_reverse(explode(' ', $this->name)));
344
    }
345
346
    /**
347
     * Get the 'name_link' attribute that returns a url to the User's team.show page.
348
     *
349
     * @return string
350
     */
351
    public function getNameLinkAttribute()
352
    {
353
        return '<a href="'.route('user.show', ['user'=>$this->id]).'">'.$this->name.'</a>';
354
    }
355
356
    // todo: add address accessor method to a sfneal/address trait
357
358
    /**
359
     * Retrieve the User's 'address1' attribute.
360
     *
361
     * @return mixed
362
     */
363
    public function getAddress1Attribute()
364
    {
365
        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...
366
    }
367
368
    /**
369
     * Retrieve the User's 'address2' attribute.
370
     *
371
     * @return mixed
372
     */
373
    public function getAddress2Attribute()
374
    {
375
        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...
376
    }
377
378
    /**
379
     * Retrieve the User's 'city' attribute.
380
     *
381
     * @return mixed
382
     */
383
    public function getCityAttribute()
384
    {
385
        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...
386
    }
387
388
    /**
389
     * Retrieve the User's 'state' attribute.
390
     *
391
     * @return mixed
392
     */
393
    public function getStateAttribute()
394
    {
395
        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...
396
    }
397
398
    /**
399
     * Retrieve the User's 'zip' attribute.
400
     *
401
     * @return mixed
402
     */
403
    public function getZipAttribute()
404
    {
405
        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...
406
    }
407
408
    /**
409
     * Retrieve an email link.
410
     *
411
     * @return string
412
     */
413
    public function getEmailLinkAttribute(): string
414
    {
415
        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...
416
    }
417
418
    /**
419
     * Retrieve a work phone link.
420
     *
421
     * @return string
422
     */
423
    public function getPhoneWorkLinkAttribute(): string
424
    {
425
        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...
426
    }
427
428
    /**
429
     * Retrieve a mobile phone link.
430
     *
431
     * @return string
432
     */
433
    public function getPhoneMobileLinkAttribute(): string
434
    {
435
        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...
436
    }
437
438
    /**
439
     * Retrieve the User's rate formatted as dollars.
440
     *
441
     * @return string
442
     */
443
    public function getRateFormattedAttribute(): string
444
    {
445
        return (! empty($this->rate)) ? '$'.FormatDollars::execute($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...
446
    }
447
448
    /**
449
     * Retrieve the raw 'text' attribute with newline chars.
450
     *
451
     * @return mixed
452
     */
453
    public function getTextareaAttribute()
454
    {
455
        return $this->attributes['bio'];
456
    }
457
458
    /**
459
     * Retrieve a User's custom email footer.
460
     *
461
     * @return string
462
     */
463
    public function getEmailFooterAttribute(): string
464
    {
465
        $footer = "{$this->name}";
466
        $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...
467
        $footer .= "\n".OrganizationService::name() ?? '';
468
        $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...
469
        $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...
470
471
        return $footer;
472
    }
473
}
474