| Total Complexity | 45 |
| Total Lines | 526 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | 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 |
||
| 19 | class User extends AbstractAuthenticatable |
||
| 20 | { |
||
| 21 | // todo: refactor status to use Status model? |
||
| 22 | use HasCustomCasts; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The "booting" method of the model. |
||
| 26 | * |
||
| 27 | * @return void |
||
| 28 | */ |
||
| 29 | public static function boot() |
||
| 30 | { |
||
| 31 | parent::boot(); |
||
| 32 | |||
| 33 | // Global scopes |
||
| 34 | static::addGlobalScope(new UserActiveScope()); |
||
| 35 | static::addGlobalScope(new OrderScope('last_name', 'asc')); |
||
| 36 | } |
||
| 37 | |||
| 38 | protected $dates = ['deleted_at']; |
||
| 39 | protected $table = 'user'; |
||
| 40 | protected $primaryKey = 'id'; |
||
| 41 | protected $connection = 'mysql'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The attributes that are mass assignable. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $fillable = [ |
||
| 49 | 'role_id', |
||
| 50 | 'first_name', |
||
| 51 | 'middle_name', |
||
| 52 | 'last_name', |
||
| 53 | 'nickname', |
||
| 54 | 'nickname_preferred', |
||
| 55 | 'title', |
||
| 56 | 'suffix', |
||
| 57 | 'email', |
||
| 58 | 'phone_work', |
||
| 59 | 'phone_mobile', |
||
| 60 | 'fax', |
||
| 61 | 'website', |
||
| 62 | 'bio', |
||
| 63 | 'username', |
||
| 64 | 'password', |
||
| 65 | 'status', |
||
| 66 | 'rate', |
||
| 67 | 'plan_management_buckets', |
||
| 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 | // * @var array Events to be dispatched after certain events |
||
| 91 | // */ |
||
| 92 | // protected $dispatchesEvents = [ |
||
| 93 | // 'updated' => UserUpdatedEvent::class, |
||
| 94 | // 'deleted' => DeletedFileModelsParentEvent::class, |
||
| 95 | // ]; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Query Builder. |
||
| 99 | * @param $query |
||
| 100 | * @return UserBuilder |
||
| 101 | */ |
||
| 102 | public function newEloquentBuilder($query) |
||
| 103 | { |
||
| 104 | return new UserBuilder($query); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Custom User query Builder. |
||
| 109 | * |
||
| 110 | * @return UserBuilder|Builder |
||
| 111 | */ |
||
| 112 | public static function query(): UserBuilder |
||
| 113 | { |
||
| 114 | return parent::query(); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * User's 'role' relationship. |
||
| 119 | * |
||
| 120 | * @return BelongsTo |
||
| 121 | */ |
||
| 122 | public function role() |
||
| 123 | { |
||
| 124 | return $this->belongsTo(Role::class, 'role_id', 'role_id'); |
||
| 125 | } |
||
| 126 | |||
| 127 | // /** |
||
| 128 | // * Tasks belonging to the User. |
||
| 129 | // * |
||
| 130 | // * @return HasMany |
||
| 131 | // */ |
||
| 132 | // public function tasks() |
||
| 133 | // { |
||
| 134 | // return $this->hasMany(Task::class, 'user_id', 'id')->orderBy('updated_at', 'desc'); |
||
| 135 | // } |
||
| 136 | // |
||
| 137 | // /** |
||
| 138 | // * Projects that the User is a team member of. |
||
| 139 | // * |
||
| 140 | // * @return BelongsToMany |
||
| 141 | // */ |
||
| 142 | // public function projects() |
||
| 143 | // { |
||
| 144 | // return $this->belongsToMany(Project::class, 'project_user', 'user_id', 'project_id')->with(['status']); |
||
| 145 | // } |
||
| 146 | // |
||
| 147 | // /** |
||
| 148 | // * Comment created by the user. |
||
| 149 | // * |
||
| 150 | // * @return HasMany |
||
| 151 | // */ |
||
| 152 | // public function comments() |
||
| 153 | // { |
||
| 154 | // return $this->hasMany(ProjectComment::class, 'user_id', 'id')->orderBy('created_at', 'desc'); |
||
| 155 | // } |
||
| 156 | // |
||
| 157 | // /** |
||
| 158 | // * Documents uploaded by the User. |
||
| 159 | // * |
||
| 160 | // * @return HasMany |
||
| 161 | // */ |
||
| 162 | // public function docs() |
||
| 163 | // { |
||
| 164 | // return $this->hasMany(ProjectDocument::class, 'user_id', 'id')->orderBy('created_at', 'desc'); |
||
| 165 | // } |
||
| 166 | // |
||
| 167 | // /** |
||
| 168 | // * Traffic tracking for a User. |
||
| 169 | // * |
||
| 170 | // * @return HasMany |
||
| 171 | // */ |
||
| 172 | // public function traffic() |
||
| 173 | // { |
||
| 174 | // return $this->hasMany(TrackTraffic::class, 'user_id', 'id'); |
||
| 175 | // } |
||
| 176 | // |
||
| 177 | // /** |
||
| 178 | // * Activity tracking for a User. |
||
| 179 | // * |
||
| 180 | // * @return HasMany |
||
| 181 | // */ |
||
| 182 | // public function activity() |
||
| 183 | // { |
||
| 184 | // return $this->hasMany(TrackActivity::class, 'user_id', 'id'); |
||
| 185 | // } |
||
| 186 | // |
||
| 187 | // /** |
||
| 188 | // * Task Rate History for a User. |
||
| 189 | // * |
||
| 190 | // * @return HasMany |
||
| 191 | // */ |
||
| 192 | // public function rateHistory() |
||
| 193 | // { |
||
| 194 | // return $this->hasMany(RateHistory::class, 'user_id', 'id'); |
||
| 195 | // } |
||
| 196 | // |
||
| 197 | // /** |
||
| 198 | // * The image file. |
||
| 199 | // * |
||
| 200 | // * @return MorphOne|File |
||
| 201 | // */ |
||
| 202 | // public function file() |
||
| 203 | // { |
||
| 204 | // return $this->morphOne(File::class, 'fileable'); |
||
| 205 | // } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * User's Notification Subscriptions. |
||
| 209 | * |
||
| 210 | * @return HasMany |
||
| 211 | */ |
||
| 212 | public function notificationSubscriptions() |
||
| 213 | { |
||
| 214 | return $this->hasMany(UserNotification::class, 'user_id', 'id'); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * User's 'team' relationship - indicates user is a member of the public team. |
||
| 219 | * |
||
| 220 | * @return HasOne |
||
| 221 | */ |
||
| 222 | public function team() |
||
| 223 | { |
||
| 224 | return $this->hasOne(Team::class, 'user_id', 'id'); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * User's address. |
||
| 229 | * |
||
| 230 | * @return MorphOne|Address |
||
| 231 | */ |
||
| 232 | public function address() |
||
| 233 | { |
||
| 234 | return $this->morphOne(Address::class, 'addressable'); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Determine if a User has a particular 'role_id'. |
||
| 239 | * |
||
| 240 | * @param int $role_id |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function isRoleId(int $role_id): bool |
||
| 244 | { |
||
| 245 | return $this->role_id == $role_id; |
||
|
|
|||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Determine if a User has a particular 'role name'. |
||
| 250 | * |
||
| 251 | * @param string $role |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public function isRole(string $role): bool |
||
| 255 | { |
||
| 256 | return strtolower($this->role->name) == strtolower($role); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Determine if a User has an 'admin' role. |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | public function isAdmin(): bool |
||
| 265 | { |
||
| 266 | return $this->isRoleId(3); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Determine if a User has an 'employee' role. |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function isEmployee(): bool |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Determine if a User has an 'contractor' role. |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function isContractor(): bool |
||
| 285 | { |
||
| 286 | return $this->isRoleId(2); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Determine if a User is 'active'. |
||
| 291 | * |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function isActive(): bool |
||
| 295 | { |
||
| 296 | return $this->status == 1; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Determine if a User's $nickname is preferred over their $first_name. |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function isNicknamePreferred(): bool |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Retrieve a User's initials. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getInitialsAttribute() |
||
| 315 | { |
||
| 316 | return implodeFiltered('', collect([ |
||
| 317 | $this->first_name, |
||
| 318 | $this->middle_name, |
||
| 319 | $this->last_name, |
||
| 320 | ])->map(function ($name) { |
||
| 321 | return substr($name, 0, 1); |
||
| 322 | })->toArray()); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get the AWS S3 file upload directory for an Inquiry model by retrieving the table name and primary key. |
||
| 327 | * |
||
| 328 | * @param string $base_dir |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function getUploadDirectory($base_dir = 'images'): string |
||
| 332 | { |
||
| 333 | return $base_dir.'/'.str_replace('_', '-', $this->getTable()).'/'.$this->getKey(); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get the 'city_state' attribute. |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getCityStateAttribute() |
||
| 342 | { |
||
| 343 | return $this->address->city_state ?? null; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Set the 'password' attribute. |
||
| 348 | * |
||
| 349 | * @param $value |
||
| 350 | */ |
||
| 351 | public function setPasswordAttribute($value) |
||
| 352 | { |
||
| 353 | if (! empty($value)) { |
||
| 354 | $this->attributes['password'] = bcrypt($value); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | // /** |
||
| 359 | // * Get a User's permitted plan management buckets. |
||
| 360 | // * |
||
| 361 | // * @return array|mixed |
||
| 362 | // */ |
||
| 363 | // public function getPermittedBucketsAttribute() |
||
| 364 | // { |
||
| 365 | // if (isset($this->plan_management_buckets)) { |
||
| 366 | // return $this->plan_management_buckets; |
||
| 367 | // } else { |
||
| 368 | // return array_keys(PlanManagement::buckets()); |
||
| 369 | // } |
||
| 370 | // } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Mutate the 'middle_name' attribute. |
||
| 374 | * |
||
| 375 | * @param string|null $value |
||
| 376 | */ |
||
| 377 | public function setMiddleNameAttribute(string $value = null) |
||
| 378 | { |
||
| 379 | if (! is_null($value)) { |
||
| 380 | // Remove leading & trailing whitespace |
||
| 381 | $middle_name = trim($value); |
||
| 382 | |||
| 383 | // Append '.' to the middle name if's a single letter |
||
| 384 | $this->attributes['middle_name'] = strlen($middle_name) == 1 ? "{$middle_name}." : $middle_name; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Retrieve the User's name (first & last). |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | public function getNameAttribute(): string |
||
| 394 | { |
||
| 395 | // Use nickname instead of first if it is set & preferred |
||
| 396 | $first = (isset($this->nickname) && $this->isNicknamePreferred()) ? $this->nickname : $this->first_name; |
||
| 397 | |||
| 398 | // Return concatenated name |
||
| 399 | return "{$first} {$this->last_name}"; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Retrieve the User's full name with middle initial. |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function getNameFullAttribute(): string |
||
| 408 | { |
||
| 409 | $name = $this->first_name; |
||
| 410 | if ($this->middle_name) { |
||
| 411 | $name .= ' '.$this->middle_name; |
||
| 412 | } |
||
| 413 | |||
| 414 | return "{$name} {$this->last_name}"; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Retrieve the User's name with their suffix. |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | public function getNameSuffixAttribute(): string |
||
| 423 | { |
||
| 424 | return $this->name_full.($this->suffix ? ", {$this->suffix}" : ''); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get the 'list_name' attribute. |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function getListNameAttribute() |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get the 'name_link' attribute that returns a url to the User's team.show page. |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getNameLinkAttribute() |
||
| 443 | { |
||
| 444 | return '<a href="'.route('user.show', ['user'=>$this->id]).'">'.$this->name.'</a>'; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Retrieve the User's 'address1' attribute. |
||
| 449 | * |
||
| 450 | * @return mixed |
||
| 451 | */ |
||
| 452 | public function getAddress1Attribute() |
||
| 453 | { |
||
| 454 | return $this->address->address_1 ?? null; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Retrieve the User's 'address2' attribute. |
||
| 459 | * |
||
| 460 | * @return mixed |
||
| 461 | */ |
||
| 462 | public function getAddress2Attribute() |
||
| 463 | { |
||
| 464 | return $this->address->address_2 ?? null; |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Retrieve the User's 'city' attribute. |
||
| 469 | * |
||
| 470 | * @return mixed |
||
| 471 | */ |
||
| 472 | public function getCityAttribute() |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Retrieve the User's 'state' attribute. |
||
| 479 | * |
||
| 480 | * @return mixed |
||
| 481 | */ |
||
| 482 | public function getStateAttribute() |
||
| 483 | { |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Retrieve the User's 'zip' attribute. |
||
| 489 | * |
||
| 490 | * @return mixed |
||
| 491 | */ |
||
| 492 | public function getZipAttribute() |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Retrieve an email link. |
||
| 499 | * |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | public function getEmailLinkAttribute(): string |
||
| 503 | { |
||
| 504 | return $this->email ? ('mailto:'.$this->email) : '#!'; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Retrieve a work phone link. |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public function getPhoneWorkLinkAttribute(): string |
||
| 513 | { |
||
| 514 | return $this->phone_work ? ('tel:'.$this->phone_work) : '#!'; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Retrieve a mobile phone link. |
||
| 519 | * |
||
| 520 | * @return string |
||
| 521 | */ |
||
| 522 | public function getPhoneMobileLinkAttribute(): string |
||
| 523 | { |
||
| 524 | return $this->phone_mobile ? ('tel:'.$this->phone_mobile) : '#!'; |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Retrieve the User's rate formatted as dollars. |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | public function getRateFormattedAttribute(): string |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Retrieve the raw 'text' attribute with newline chars. |
||
| 539 | * |
||
| 540 | * @return mixed |
||
| 541 | */ |
||
| 542 | public function getTextareaAttribute() |
||
| 547 |