| Total Complexity | 57 |
| Total Lines | 519 |
| Duplicated Lines | 0 % |
| Changes | 19 | ||
| Bugs | 3 | Features | 0 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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() |
||
| 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; |
||
|
|
|||
| 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); |
||
| 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 |
||
| 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; |
||
| 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 |
||
| 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, |
||
| 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) |
||
| 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); |
||
| 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); |
||
| 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; |
||
| 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) { |
||
| 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}" : ''); |
||
| 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; |
||
| 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; |
||
| 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; |
||
| 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; |
||
| 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; |
||
| 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) : '#!'; |
||
| 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) : '#!'; |
||
| 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) : '#!'; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Retrieve the User's rate formatted as dollars. |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public function getRateFormattedAttribute(): string |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Retrieve the raw 'text' attribute with newline chars. |
||
| 519 | * |
||
| 520 | * @return mixed |
||
| 521 | */ |
||
| 522 | public function getTextareaAttribute() |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Retrieve a User's custom email footer. |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | public function getEmailFooterAttribute(): string |
||
| 533 | { |
||
| 541 | } |
||
| 542 | } |
||
| 543 |