Total Complexity | 42 |
Total Lines | 257 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like UserAttribute 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 UserAttribute, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | trait UserAttribute |
||
11 | { |
||
12 | /** |
||
13 | * @param $password |
||
14 | */ |
||
15 | public function setPasswordAttribute($password) : void |
||
16 | { |
||
17 | // If password was accidentally passed in already hashed, try not to double hash it |
||
18 | if ( |
||
19 | (\strlen($password) === 60 && preg_match('/^\$2y\$/', $password)) || |
||
20 | (\strlen($password) === 95 && preg_match('/^\$argon2i\$/', $password)) |
||
21 | ) { |
||
22 | $hash = $password; |
||
23 | } else { |
||
24 | $hash = Hash::make($password); |
||
25 | } |
||
26 | |||
27 | // Note: Password Histories are logged from the \App\Observer\User\UserObserver class |
||
28 | $this->attributes['password'] = $hash; |
||
|
|||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @return string |
||
33 | */ |
||
34 | public function getStatusLabelAttribute() |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @return string |
||
45 | */ |
||
46 | public function getConfirmedLabelAttribute() |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | public function getFullNameAttribute() |
||
66 | { |
||
67 | return $this->last_name |
||
68 | ? $this->first_name.' '.$this->last_name |
||
69 | : $this->first_name; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getNameAttribute() |
||
76 | { |
||
77 | return $this->full_name; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return mixed |
||
82 | */ |
||
83 | public function getPictureAttribute() |
||
84 | { |
||
85 | return $this->getPicture(); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getSocialButtonsAttribute() |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getLoginAsButtonAttribute() |
||
109 | { |
||
110 | /* |
||
111 | * If the admin is currently NOT spoofing a user |
||
112 | */ |
||
113 | if (! session()->has('admin_user_id') || ! session()->has('temp_user_id')) { |
||
114 | //Won't break, but don't let them "Login As" themselves |
||
115 | if ($this->id != auth()->id()) { |
||
116 | return '<a href="'.route( |
||
117 | 'admin.auth.user.login-as', |
||
118 | $this |
||
119 | ).'" class="dropdown-item">'.__('buttons.backend.access.users.login_as', ['user' => e($this->full_name)]).'</a> '; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | return ''; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getClearSessionButtonAttribute() |
||
130 | { |
||
131 | if ($this->id != auth()->id() && config('session.driver') == 'database') { |
||
132 | return '<a href="'.route('admin.auth.user.clear-session', $this).'" |
||
133 | data-trans-button-cancel="'.__('buttons.general.cancel').'" |
||
134 | data-trans-button-confirm="'.__('buttons.general.continue').'" |
||
135 | data-trans-title="'.__('strings.backend.general.are_you_sure').'" |
||
136 | class="dropdown-item" name="confirm_item">'.__('buttons.backend.access.users.clear_session').'</a> '; |
||
137 | } |
||
138 | |||
139 | return ''; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getShowButtonAttribute() |
||
146 | { |
||
147 | return '<a href="'.route('admin.auth.user.show', $this).'" data-toggle="tooltip" data-placement="top" title="'.__('buttons.general.crud.view').'" class="btn btn-info"><i class="fas fa-eye"></i></a>'; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getEditButtonAttribute() |
||
154 | { |
||
155 | return '<a href="'.route('admin.auth.user.edit', $this).'" data-toggle="tooltip" data-placement="top" title="'.__('buttons.general.crud.edit').'" class="btn btn-primary"><i class="fas fa-edit"></i></a>'; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getChangePasswordButtonAttribute() |
||
162 | { |
||
163 | return '<a href="'.route('admin.auth.user.change-password', $this).'" class="dropdown-item">'.__('buttons.backend.access.users.change_password').'</a> '; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @return string |
||
168 | */ |
||
169 | public function getStatusButtonAttribute() |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getConfirmedButtonAttribute() |
||
197 | { |
||
198 | if (! $this->isConfirmed() && ! config('access.users.requires_approval')) { |
||
199 | return '<a href="'.route('admin.auth.user.account.confirm.resend', $this).'" class="dropdown-item">'.__('buttons.backend.access.users.resend_email').'</a> '; |
||
200 | } |
||
201 | |||
202 | return ''; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getDeleteButtonAttribute() |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getDeletePermanentlyButtonAttribute() |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | public function getRestoreButtonAttribute() |
||
234 | { |
||
235 | return '<a href="'.route('admin.auth.user.restore', $this).'" name="confirm_item" class="btn btn-info"><i class="fas fa-refresh" data-toggle="tooltip" data-placement="top" title="'.__('buttons.backend.access.users.restore_user').'"></i></a> '; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @return string |
||
240 | */ |
||
241 | public function getActionButtonsAttribute() |
||
267 | </div> |
||
268 | </div> |
||
269 | </div>'; |
||
270 | } |
||
271 | } |
||
272 |