Conditions | 91 |
Paths | > 20000 |
Total Lines | 169 |
Code Lines | 119 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
108 | public function edit(Request $request) |
||
109 | { |
||
110 | $this->setPreferences(); |
||
111 | |||
112 | $action = $request->input('action') ?? 'view'; |
||
113 | |||
114 | $userid = $this->userdata->id; |
||
115 | if (! $this->userdata) { |
||
116 | $this->show404('No such user!'); |
||
117 | } |
||
118 | |||
119 | $errorStr = ''; |
||
120 | $success_2fa = $request->session()->get('success'); |
||
121 | $error_2fa = $request->session()->get('error'); |
||
122 | |||
123 | // Generate 2FA QR code URL if 2FA is set up but not enabled |
||
124 | $google2fa_url = ''; |
||
125 | if ($this->userdata->passwordSecurity()->exists() && ! $this->userdata->passwordSecurity->google2fa_enable) { |
||
126 | $google2fa_url = \Google2FA::getQRCodeInline( |
||
127 | config('app.name'), |
||
128 | $this->userdata->email, |
||
129 | $this->userdata->passwordSecurity->google2fa_secret |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | switch ($action) { |
||
134 | case 'newapikey': |
||
135 | User::updateRssKey($userid); |
||
136 | |||
137 | return redirect()->to('profile'); |
||
138 | case 'clearcookies': |
||
139 | return redirect()->to('profileedit'); |
||
140 | case 'submit': |
||
141 | $validator = Validator::make($request->all(), [ |
||
142 | 'email' => ['nullable', 'string', 'email', 'max:255', 'unique:users', 'indisposable'], |
||
143 | 'password' => ['nullable', 'string', 'min:8', 'confirmed', 'regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/'], |
||
144 | ]); |
||
145 | |||
146 | if ($validator->fails()) { |
||
147 | $errorStr = implode('', Arr::collapse($validator->errors()->toArray())); |
||
148 | } else { |
||
149 | User::updateUser( |
||
150 | $userid, |
||
151 | $this->userdata->username, |
||
152 | $request->input('email'), |
||
153 | $this->userdata->grabs, |
||
154 | $this->userdata->roles_id, |
||
155 | $this->userdata->notes, |
||
156 | $this->userdata->invites, |
||
157 | $request->has('movieview') ? 1 : 0, |
||
158 | $request->has('musicview') ? 1 : 0, |
||
159 | $request->has('gameview') ? 1 : 0, |
||
160 | $request->has('xxxview') ? 1 : 0, |
||
161 | $request->has('consoleview') ? 1 : 0, |
||
162 | $request->has('bookview') ? 1 : 0, |
||
163 | 'None', |
||
164 | ); |
||
165 | |||
166 | if ((int) $request->input('viewconsole') === 1 && $this->userdata->can('view console') && ! $this->userdata->hasDirectPermission('view console')) { |
||
167 | $this->userdata->givePermissionTo('view console'); |
||
168 | } elseif ((int) $request->input('viewconsole') === 0 && $this->userdata->can('view console') && $this->userdata->hasDirectPermission('view console')) { |
||
169 | $this->userdata->revokePermissionTo('view console'); |
||
170 | } elseif ($this->userdata->cant('view console') && \in_array((int) $request->input('viewconsole'), [0, 1], true)) { |
||
171 | $this->userdata->revokePermissionTo('view console'); |
||
172 | } |
||
173 | |||
174 | if ((int) $request->input('viewmovies') === 1 && $this->userdata->can('view movies') && ! $this->userdata->hasDirectPermission('view movies')) { |
||
175 | $this->userdata->givePermissionTo('view movies'); |
||
176 | } elseif ((int) $request->input('viewmovies') === 0 && $this->userdata->can('view movies') && $this->userdata->hasDirectPermission('view movies')) { |
||
177 | $this->userdata->revokePermissionTo('view movies'); |
||
178 | } elseif ($this->userdata->cant('view movies') && $this->userdata->hasDirectPermission('view movies') && \in_array((int) $request->input('viewmovies'), [0, 1], true)) { |
||
179 | $this->userdata->revokePermissionTo('view movies'); |
||
180 | } |
||
181 | |||
182 | if ((int) $request->input('viewaudio') === 1 && $this->userdata->can('view audio') && ! $this->userdata->hasDirectPermission('view audio')) { |
||
183 | $this->userdata->givePermissionTo('view audio'); |
||
184 | } elseif ((int) $request->input('viewaudio') === 0 && $this->userdata->can('view audio') && $this->userdata->hasDirectPermission('view audio')) { |
||
185 | $this->userdata->revokePermissionTo('view audio'); |
||
186 | } elseif ($this->userdata->cant('view audio') && $this->userdata->hasDirectPermission('view audio') && \in_array((int) $request->input('viewaudio'), [0, 1], true)) { |
||
187 | $this->userdata->revokePermissionTo('view audio'); |
||
188 | } |
||
189 | |||
190 | if ((int) $request->input('viewpc') === 1 && $this->userdata->can('view pc') && ! $this->userdata->hasDirectPermission('view pc')) { |
||
191 | $this->userdata->givePermissionTo('view pc'); |
||
192 | } elseif ((int) $request->input('viewpc') === 0 && $this->userdata->can('view pc') && $this->userdata->hasDirectPermission('view pc')) { |
||
193 | $this->userdata->revokePermissionTo('view pc'); |
||
194 | } elseif ($this->userdata->cant('view pc') && $this->userdata->hasDirectPermission('view pc') && \in_array((int) $request->input('viewpc'), [0, 1], true)) { |
||
195 | $this->userdata->revokePermissionTo('view pc'); |
||
196 | } |
||
197 | |||
198 | if ((int) $request->input('viewtv') === 1 && $this->userdata->can('view tv') && ! $this->userdata->hasDirectPermission('view tv')) { |
||
199 | $this->userdata->givePermissionTo('view tv'); |
||
200 | } elseif ((int) $request->input('viewtv') === 0 && $this->userdata->can('view tv') && $this->userdata->hasDirectPermission('view tv')) { |
||
201 | $this->userdata->revokePermissionTo('view tv'); |
||
202 | } elseif ($this->userdata->cant('view tv') && $this->userdata->hasDirectPermission('view tv') && \in_array((int) $request->input('viewtv'), [0, 1], true)) { |
||
203 | $this->userdata->revokePermissionTo('view tv'); |
||
204 | } |
||
205 | |||
206 | if ((int) $request->input('viewadult') === 1 && $this->userdata->can('view adult') && ! $this->userdata->hasDirectPermission('view adult')) { |
||
207 | $this->userdata->givePermissionTo('view adult'); |
||
208 | } elseif ((int) $request->input('viewadult') === 0 && $this->userdata->can('view adult') && $this->userdata->hasDirectPermission('view adult')) { |
||
209 | $this->userdata->revokePermissionTo('view adult'); |
||
210 | } elseif ($this->userdata->cant('view adult') && $this->userdata->hasDirectPermission('view adult') && \in_array((int) $request->input('viewadult'), [0, 1], true)) { |
||
211 | $this->userdata->revokePermissionTo('view adult'); |
||
212 | } |
||
213 | |||
214 | if ((int) $request->input('viewbooks') === 1 && $this->userdata->can('view books') && ! $this->userdata->hasDirectPermission('view books')) { |
||
215 | $this->userdata->givePermissionTo('view books'); |
||
216 | } elseif ((int) $request->input('viewbooks') === 0 && $this->userdata->can('view books') && $this->userdata->hasDirectPermission('view books')) { |
||
217 | $this->userdata->revokePermissionTo('view books'); |
||
218 | } elseif ($this->userdata->cant('view books') && $this->userdata->hasDirectPermission('view books') && \in_array((int) $request->input('viewbooks'), [0, 1], true)) { |
||
219 | $this->userdata->revokePermissionTo('view books'); |
||
220 | } |
||
221 | |||
222 | if ((int) $request->input('viewother') === 1 && $this->userdata->can('view other') && ! $this->userdata->hasDirectPermission('view other')) { |
||
223 | $this->userdata->givePermissionTo('view other'); |
||
224 | } elseif ((int) $request->input('viewother') === 0 && $this->userdata->can('view other') && $this->userdata->hasDirectPermission('view other')) { |
||
225 | $this->userdata->revokePermissionTo('view other'); |
||
226 | } elseif ($this->userdata->cant('view other') && $this->userdata->hasDirectPermission('view other') && \in_array((int) $request->input('viewother'), [0, 1], true)) { |
||
227 | $this->userdata->revokePermissionTo('view other'); |
||
228 | } |
||
229 | |||
230 | if ($request->has('password') && ! empty($request->input('password'))) { |
||
231 | User::updatePassword($userid, $request->input('password')); |
||
232 | } |
||
233 | |||
234 | if (! $this->userdata->hasRole('Admin')) { |
||
235 | if (! empty($request->input('email')) && $this->userdata->email !== $request->input('email')) { |
||
236 | $this->userdata->email = $request->input('email'); |
||
237 | |||
238 | $verify_user = $this->userdata; |
||
239 | |||
240 | UserVerification::generate($verify_user); |
||
241 | |||
242 | UserVerification::send($verify_user, 'User email verification required'); |
||
243 | |||
244 | Auth::logout(); |
||
245 | |||
246 | return redirect()->to('login')->with('info', 'You will be able to login after you verify your new email address'); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | return redirect()->to('profile')->with('success', 'Profile changes saved'); |
||
251 | } |
||
252 | break; |
||
253 | |||
254 | case 'view': |
||
255 | default: |
||
256 | break; |
||
257 | } |
||
258 | |||
259 | $this->smarty->assign('error', $errorStr); |
||
260 | $this->smarty->assign('user', $this->userdata); |
||
261 | $this->smarty->assign('userexccat', User::getCategoryExclusionById($userid)); |
||
262 | $this->smarty->assign('success_2fa', $success_2fa); |
||
263 | $this->smarty->assign('error_2fa', $error_2fa); |
||
264 | $this->smarty->assign('google2fa_url', $google2fa_url); |
||
265 | |||
266 | $meta_title = 'Edit User Profile'; |
||
267 | $meta_keywords = 'edit,profile,user,details'; |
||
268 | $meta_description = 'Edit User Profile for '.$this->userdata->username; |
||
269 | |||
270 | $this->smarty->assign('yesno_ids', [1, 0]); |
||
271 | $this->smarty->assign('yesno_names', ['Yes', 'No']); |
||
272 | |||
273 | $content = $this->smarty->fetch('profileedit.tpl'); |
||
274 | |||
275 | $this->smarty->assign(compact('content', 'meta_title', 'meta_keywords', 'meta_description')); |
||
276 | $this->pagerender(); |
||
277 | } |
||
301 |