Total Complexity | 50 |
Total Lines | 452 |
Duplicated Lines | 0 % |
Changes | 13 | ||
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 |
||
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() |
||
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() |
||
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 |
||
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 |
||
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; |
||
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; |
||
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, |
||
241 | $this->middle_name, |
||
242 | $this->last_name, |
||
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; |
||
306 | |||
307 | // Return concatenated name |
||
308 | return "{$first} {$this->last_name}"; |
||
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; |
||
319 | if ($this->middle_name) { |
||
320 | $name .= ' '.$this->middle_name; |
||
321 | } |
||
322 | |||
323 | return "{$name} {$this->last_name}"; |
||
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}" : ''); |
||
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; |
||
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; |
||
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; |
||
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; |
||
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; |
||
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) : '#!'; |
||
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) : '#!'; |
||
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) : '#!'; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Retrieve the User's rate formatted as dollars. |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | public function getRateFormattedAttribute(): string |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Retrieve the raw 'text' attribute with newline chars. |
||
450 | * |
||
451 | * @return mixed |
||
452 | */ |
||
453 | public function getTextareaAttribute() |
||
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}" : ''; |
||
467 | $footer .= "\n".OrganizationService::name() ?? ''; |
||
468 | $footer .= "\n".($this->phone_work ?? OrganizationService::phone()) ?? ''; |
||
472 | } |
||
473 | } |
||
474 |