Total Complexity | 75 |
Total Lines | 314 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ProfileController 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 ProfileController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ProfileController extends BasePageController |
||
21 | { |
||
22 | /** |
||
23 | * @throws \Throwable |
||
24 | */ |
||
25 | public function show(Request $request) |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return Factory|\Illuminate\View\View|View |
||
85 | * |
||
86 | * @throws \Exception |
||
87 | */ |
||
88 | public function edit(Request $request) |
||
89 | { |
||
90 | |||
91 | $action = $request->input('action') ?? 'view'; |
||
92 | |||
93 | $userid = $this->userdata->id; |
||
94 | if (! $this->userdata) { |
||
95 | $this->show404('No such user!'); |
||
96 | } |
||
97 | |||
98 | $errorStr = ''; |
||
99 | $success_2fa = $request->session()->get('success'); |
||
100 | $error_2fa = $request->session()->get('error'); |
||
101 | |||
102 | // Generate 2FA QR code URL if 2FA is set up but not enabled |
||
103 | $google2fa_url = ''; |
||
104 | if ($this->userdata->passwordSecurity()->exists() && ! $this->userdata->passwordSecurity->google2fa_enable) { |
||
105 | $google2fa_url = \Google2FA::getQRCodeInline( |
||
106 | config('app.name'), |
||
107 | $this->userdata->email, |
||
108 | $this->userdata->passwordSecurity->google2fa_secret |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | switch ($action) { |
||
113 | case 'newapikey': |
||
114 | User::updateRssKey($userid); |
||
115 | |||
116 | return redirect()->to('profile'); |
||
117 | case 'clearcookies': |
||
118 | return redirect()->to('profileedit'); |
||
119 | case 'submit': |
||
120 | $validator = Validator::make($request->all(), [ |
||
121 | 'email' => ['nullable', 'string', 'email', 'max:255', 'unique:users,email,'.$userid, 'indisposable'], |
||
122 | 'password' => ['nullable', 'string', 'min:8', 'confirmed', 'regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/'], |
||
123 | ]); |
||
124 | |||
125 | if ($validator->fails()) { |
||
126 | $errorStr = implode('', Arr::collapse($validator->errors()->toArray())); |
||
127 | } else { |
||
128 | User::updateUser( |
||
129 | $userid, |
||
130 | $this->userdata->username, |
||
131 | $request->input('email'), |
||
132 | $this->userdata->grabs, |
||
133 | $this->userdata->roles_id, |
||
134 | $this->userdata->notes, |
||
135 | $this->userdata->invites, |
||
136 | $request->has('viewmovies') ? 1 : 0, |
||
137 | $request->has('viewaudio') ? 1 : 0, |
||
138 | $request->has('viewpc') ? 1 : 0, |
||
139 | $request->has('viewadult') ? 1 : 0, |
||
140 | $request->has('viewconsole') ? 1 : 0, |
||
141 | $request->has('viewbooks') ? 1 : 0, |
||
142 | 'None', |
||
143 | ); |
||
144 | |||
145 | // Update theme preference |
||
146 | if ($request->has('theme_preference')) { |
||
147 | $themeValue = $request->input('theme_preference'); |
||
148 | if (in_array($themeValue, ['light', 'dark', 'system'])) { |
||
149 | User::where('id', $userid)->update(['theme_preference' => $themeValue]); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // Handle Console permission |
||
154 | if ($request->has('viewconsole')) { |
||
155 | if (! $this->userdata->hasDirectPermission('view console')) { |
||
156 | $this->userdata->givePermissionTo('view console'); |
||
157 | } |
||
158 | } else { |
||
159 | if ($this->userdata->hasPermissionTo('view console')) { |
||
160 | $this->userdata->revokePermissionTo('view console'); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // Handle Movies permission |
||
165 | if ($request->has('viewmovies')) { |
||
166 | if (! $this->userdata->hasDirectPermission('view movies')) { |
||
167 | $this->userdata->givePermissionTo('view movies'); |
||
168 | } |
||
169 | } else { |
||
170 | if ($this->userdata->hasPermissionTo('view movies')) { |
||
171 | $this->userdata->revokePermissionTo('view movies'); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | // Handle Audio permission |
||
176 | if ($request->has('viewaudio')) { |
||
177 | if (! $this->userdata->hasDirectPermission('view audio')) { |
||
178 | $this->userdata->givePermissionTo('view audio'); |
||
179 | } |
||
180 | } else { |
||
181 | if ($this->userdata->hasPermissionTo('view audio')) { |
||
182 | $this->userdata->revokePermissionTo('view audio'); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | // Handle PC/Games permission |
||
187 | if ($request->has('viewpc')) { |
||
188 | if (! $this->userdata->hasDirectPermission('view pc')) { |
||
189 | $this->userdata->givePermissionTo('view pc'); |
||
190 | } |
||
191 | } else { |
||
192 | if ($this->userdata->hasPermissionTo('view pc')) { |
||
193 | $this->userdata->revokePermissionTo('view pc'); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | // Handle TV permission |
||
198 | if ($request->has('viewtv')) { |
||
199 | if (! $this->userdata->hasDirectPermission('view tv')) { |
||
200 | $this->userdata->givePermissionTo('view tv'); |
||
201 | } |
||
202 | } else { |
||
203 | if ($this->userdata->hasPermissionTo('view tv')) { |
||
204 | $this->userdata->revokePermissionTo('view tv'); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | // Handle Adult permission |
||
209 | if ($request->has('viewadult')) { |
||
210 | if (! $this->userdata->hasDirectPermission('view adult')) { |
||
211 | $this->userdata->givePermissionTo('view adult'); |
||
212 | } |
||
213 | } else { |
||
214 | if ($this->userdata->hasPermissionTo('view adult')) { |
||
215 | $this->userdata->revokePermissionTo('view adult'); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | // Handle Books permission |
||
220 | if ($request->has('viewbooks')) { |
||
221 | if (! $this->userdata->hasDirectPermission('view books')) { |
||
222 | $this->userdata->givePermissionTo('view books'); |
||
223 | } |
||
224 | } else { |
||
225 | if ($this->userdata->hasPermissionTo('view books')) { |
||
226 | $this->userdata->revokePermissionTo('view books'); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | // Handle Other permission |
||
231 | if ($request->has('viewother')) { |
||
232 | if (! $this->userdata->hasDirectPermission('view other')) { |
||
233 | $this->userdata->givePermissionTo('view other'); |
||
234 | } |
||
235 | } else { |
||
236 | if ($this->userdata->hasPermissionTo('view other')) { |
||
237 | $this->userdata->revokePermissionTo('view other'); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | if ($request->has('password') && ! empty($request->input('password'))) { |
||
242 | User::updatePassword($userid, $request->input('password')); |
||
243 | } |
||
244 | |||
245 | if (! $this->userdata->hasRole('Admin')) { |
||
246 | if (! empty($request->input('email')) && $this->userdata->email !== $request->input('email')) { |
||
247 | $this->userdata->email = $request->input('email'); |
||
248 | |||
249 | $verify_user = $this->userdata; |
||
250 | |||
251 | UserVerification::generate($verify_user); |
||
252 | |||
253 | UserVerification::send($verify_user, 'User email verification required'); |
||
254 | |||
255 | Auth::logout(); |
||
256 | |||
257 | return redirect()->to('login')->with('info', 'You will be able to login after you verify your new email address'); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | return redirect()->to('profile')->with('success', 'Profile changes saved'); |
||
262 | } |
||
263 | break; |
||
264 | |||
265 | case 'view': |
||
266 | default: |
||
267 | break; |
||
268 | } |
||
269 | |||
270 | $this->viewData = array_merge($this->viewData, [ |
||
271 | 'error' => $errorStr, |
||
272 | 'user' => $this->userdata, |
||
273 | 'userexccat' => User::getCategoryExclusionById($userid), |
||
274 | 'success_2fa' => $success_2fa, |
||
275 | 'error_2fa' => $error_2fa, |
||
276 | 'google2fa_url' => $google2fa_url, |
||
277 | 'yesno_ids' => [1, 0], |
||
278 | 'yesno_names' => ['Yes', 'No'], |
||
279 | 'publicview' => false, |
||
280 | 'privileged' => $this->userdata->hasRole('Admin') || $this->userdata->hasRole('Moderator'), |
||
281 | 'userinvitedby' => ($this->userdata->invitedby && $this->userdata->invitedby !== '') ? User::find($this->userdata->invitedby) : null, |
||
282 | 'meta_title' => 'Edit User Profile', |
||
283 | 'meta_keywords' => 'edit,profile,user,details', |
||
284 | 'meta_description' => 'Edit User Profile for '.$this->userdata->username, |
||
285 | ]); |
||
286 | |||
287 | return view('profile.edit', $this->viewData); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @throws \Exception |
||
292 | */ |
||
293 | public function destroy(Request $request): Application|View|Factory|RedirectResponse|\Illuminate\Contracts\Foundation\Application |
||
294 | { |
||
295 | $userId = $request->input('id'); |
||
296 | |||
297 | if ($userId !== null && (int) $userId === $this->userdata->id && ! $this->userdata->hasRole('Admin')) { |
||
298 | $user = User::find($userId); |
||
299 | SendAccountDeletedEmail::dispatch($user); |
||
300 | Auth::logout(); |
||
301 | $user->delete(); |
||
302 | } |
||
303 | |||
304 | if ($this->userdata->hasRole('Admin')) { |
||
305 | return redirect()->to('profile'); |
||
306 | } |
||
307 | |||
308 | return view('errors.503')->with('warning', 'Dont try to delete another user account!'); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Update user's dark mode preference |
||
313 | */ |
||
314 | public function updateTheme(Request $request) |
||
334 | } |
||
335 | } |
||
336 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: