Total Complexity | 51 |
Total Lines | 465 |
Duplicated Lines | 0 % |
Changes | 15 | ||
Bugs | 2 | 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() |
||
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; |
||
|
|||
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); |
||
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; |
||
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; |
||
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, |
||
256 | $this->middle_name, |
||
257 | $this->last_name, |
||
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; |
||
321 | |||
322 | // Return concatenated name |
||
323 | return "{$first} {$this->last_name}"; |
||
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; |
||
334 | if ($this->middle_name) { |
||
335 | $name .= ' '.$this->middle_name; |
||
336 | } |
||
337 | |||
338 | return "{$name} {$this->last_name}"; |
||
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}" : ''); |
||
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; |
||
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; |
||
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; |
||
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; |
||
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; |
||
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) : '#!'; |
||
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) : '#!'; |
||
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) : '#!'; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Retrieve the User's rate formatted as dollars. |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | public function getRateFormattedAttribute(): string |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Retrieve the raw 'text' attribute with newline chars. |
||
465 | * |
||
466 | * @return mixed |
||
467 | */ |
||
468 | public function getTextareaAttribute() |
||
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}" : ''; |
||
482 | $footer .= "\n".OrganizationService::name() ?? ''; |
||
483 | $footer .= "\n".($this->phone_work ?? OrganizationService::phone()) ?? ''; |
||
484 | $footer .= $this->email ? "\n{$this->email}" : ''; |
||
485 | |||
486 | return $footer; |
||
487 | } |
||
488 | } |
||
489 |