Total Complexity | 45 |
Total Lines | 280 |
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 |
||
8 | trait UserAttribute |
||
9 | { |
||
10 | /** |
||
11 | * @return mixed |
||
12 | */ |
||
13 | public function canChangeEmail() |
||
14 | { |
||
15 | return config('access.users.change_email'); |
||
16 | } |
||
17 | |||
18 | /** |
||
19 | * @return bool |
||
20 | */ |
||
21 | public function canChangePassword() |
||
22 | { |
||
23 | return ! app('session')->has(config('access.socialite_session_name')); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @return string |
||
28 | */ |
||
29 | public function getStatusLabelAttribute() |
||
30 | { |
||
31 | if ($this->isActive()) { |
||
32 | return "<label class='label label-success'>" . trans('labels.general.active') . '</label>'; |
||
|
|||
33 | } |
||
34 | |||
35 | return "<label class='label label-danger'>" . trans('labels.general.inactive') . '</label>'; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @return string |
||
40 | */ |
||
41 | public function getConfirmedLabelAttribute() |
||
42 | { |
||
43 | if ($this->isConfirmed()) { |
||
44 | if ($this->id != 1 && $this->id != access()->id()) { |
||
45 | return '<a href="' . route('admin.access.user.unconfirm', |
||
46 | $this) . '" data-toggle="tooltip" data-placement="top" title="' . trans('buttons.backend.access.users.unconfirm') . '" name="confirm_item"><label class="label label-success" style="cursor:pointer">' . trans('labels.general.yes') . '</label></a>'; |
||
47 | } else { |
||
48 | return '<label class="label label-success">' . trans('labels.general.yes') . '</label>'; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | return '<a href="' . route('admin.access.user.confirm', $this) . '" data-toggle="tooltip" data-placement="top" title="' . trans('buttons.backend.access.users.confirm') . '" name="confirm_item"><label class="label label-danger" style="cursor:pointer">' . trans('labels.general.no') . '</label></a>'; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return mixed |
||
57 | */ |
||
58 | public function getPictureAttribute() |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param bool $size |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | public function getPicture($size = false) |
||
69 | { |
||
70 | if (! $size) { |
||
71 | $size = config('gravatar.default.size'); |
||
72 | } |
||
73 | |||
74 | return gravatar()->get($this->email, ['size' => $size]); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param $provider |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | public function hasProvider($provider) |
||
83 | { |
||
84 | foreach ($this->providers as $p) { |
||
85 | if ($p->provider == $provider) { |
||
86 | return true; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return false; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function isActive() |
||
97 | { |
||
98 | return $this->status == 1; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return bool |
||
103 | */ |
||
104 | public function isConfirmed() |
||
105 | { |
||
106 | return $this->confirmed == 1; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function isPending() |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getFullNameAttribute() |
||
121 | { |
||
122 | return $this->last_name |
||
123 | ? $this->first_name . ' ' . $this->last_name |
||
124 | : $this->first_name; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getNameAttribute() |
||
131 | { |
||
132 | return $this->full_name; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | public function getShowButtonAttribute() |
||
139 | { |
||
140 | return '<a href="' . route('admin.access.user.show', $this) . '" class="btn btn-xs btn-info"><i class="fa fa-search" data-toggle="tooltip" data-placement="top" title="' . trans('buttons.general.crud.view') . '"></i></a> '; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | public function getEditButtonAttribute() |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getChangePasswordButtonAttribute() |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return string |
||
161 | */ |
||
162 | public function getStatusButtonAttribute() |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @return string |
||
191 | */ |
||
192 | public function getConfirmedButtonAttribute() |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | public function getDeleteButtonAttribute() |
||
205 | { |
||
206 | if ($this->id != access()->id() && $this->id != 1) { |
||
207 | return '<a href="' . route('admin.access.user.destroy', $this) . '" |
||
208 | data-method="delete" |
||
209 | data-trans-button-cancel="' . trans('buttons.general.cancel') . '" |
||
210 | data-trans-button-confirm="' . trans('buttons.general.crud.delete') . '" |
||
211 | data-trans-title="' . trans('strings.backend.general.are_you_sure') . '" |
||
212 | class="btn btn-xs btn-danger"><i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="' . trans('buttons.general.crud.delete') . '"></i></a> '; |
||
213 | } |
||
214 | |||
215 | return ''; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return string |
||
220 | */ |
||
221 | public function getRestoreButtonAttribute() |
||
222 | { |
||
223 | return '<a href="' . route('admin.access.user.restore', $this) . '" name="restore_user" class="btn btn-xs btn-info"><i class="fa fa-refresh" data-toggle="tooltip" data-placement="top" title="' . trans('buttons.backend.access.users.restore_user') . '"></i></a> '; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getDeletePermanentlyButtonAttribute() |
||
230 | { |
||
231 | return '<a href="' . route('admin.access.user.delete-permanently', $this) . '" name="delete_user_perm" class="btn btn-xs btn-danger"><i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="' . trans('buttons.backend.access.users.delete_permanently') . '"></i></a> '; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getLoginAsButtonAttribute() |
||
238 | { |
||
239 | /* |
||
240 | * If the admin is currently NOT spoofing a user |
||
241 | */ |
||
242 | if (! session()->has('admin_user_id') || ! session()->has('temp_user_id')) { |
||
243 | //Won't break, but don't let them "Login As" themselves |
||
244 | if ($this->id != access()->id()) { |
||
245 | return '<a href="' . route('admin.access.user.login-as', |
||
246 | $this) . '" class="btn btn-xs btn-success"><i class="fa fa-lock" data-toggle="tooltip" data-placement="top" title="' . trans('buttons.backend.access.users.login_as', |
||
247 | ['user' => $this->full_name]) . '"></i></a> '; |
||
248 | } |
||
249 | } |
||
250 | |||
251 | return ''; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getClearSessionButtonAttribute() |
||
258 | { |
||
259 | if ($this->id != access()->id() && config('session.driver') == 'database') { |
||
260 | return '<a href="' . route('admin.access.user.clear-session', $this) . '" |
||
261 | data-trans-button-cancel="' . trans('buttons.general.cancel') . '" |
||
262 | data-trans-button-confirm="' . trans('buttons.general.continue') . '" |
||
263 | data-trans-title="' . trans('strings.backend.general.are_you_sure') . '" |
||
264 | class="btn btn-xs btn-warning" name="confirm_item"><i class="fa fa-times" data-toggle="tooltip" data-placement="top" title="' . trans('buttons.backend.access.users.clear_session') . '"></i></a> '; |
||
265 | } |
||
266 | |||
267 | return ''; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @return string |
||
272 | */ |
||
273 | public function getActionButtonsAttribute() |
||
288 | } |
||
289 | } |
||
290 |