Conditions | 28 |
Paths | 13798 |
Total Lines | 155 |
Code Lines | 82 |
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 |
||
196 | private function validateForm(): void |
||
197 | { |
||
198 | // is the form submitted? |
||
199 | if ($this->form->isSubmitted()) { |
||
200 | // cleanup the submitted fields, ignore fields that were added by hackers |
||
201 | $this->form->cleanupFields(); |
||
202 | |||
203 | // get fields |
||
204 | $chkNewEmail = $this->form->getField('new_email'); |
||
205 | $txtEmail = $this->form->getField('email'); |
||
206 | $txtDisplayName = $this->form->getField('display_name'); |
||
207 | $chkNewPassword = $this->form->getField('new_password'); |
||
208 | $txtPassword = $this->form->getField('password'); |
||
209 | $txtPasswordRepeat = $this->form->getField('password_repeat'); |
||
210 | $txtFirstName = $this->form->getField('first_name'); |
||
211 | $txtLastName = $this->form->getField('last_name'); |
||
212 | $txtCity = $this->form->getField('city'); |
||
213 | $ddmGender = $this->form->getField('gender'); |
||
214 | $ddmDay = $this->form->getField('day'); |
||
215 | $ddmMonth = $this->form->getField('month'); |
||
216 | $ddmYear = $this->form->getField('year'); |
||
217 | $ddmCountry = $this->form->getField('country'); |
||
218 | $txtAbout = $this->form->getField('about'); |
||
219 | |||
220 | // email filled in? |
||
221 | if ($chkNewEmail->isChecked() && $txtEmail->isFilled(BL::getError('EmailIsRequired'))) { |
||
222 | // email must not be the same as previous one |
||
223 | if ($txtEmail->getValue() == $this->profile['email']) { |
||
224 | $txtEmail->addError(BL::getError('EmailMatchesPrevious')); |
||
225 | } |
||
226 | |||
227 | // valid email? |
||
228 | if ($txtEmail->isEmail(BL::getError('EmailIsInvalid'))) { |
||
229 | // email already exists? |
||
230 | if (BackendProfilesModel::existsByEmail($txtEmail->getValue(), $this->id)) { |
||
231 | // set error |
||
232 | $txtEmail->addError(BL::getError('EmailExists')); |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | // display name filled in? |
||
238 | if ($txtDisplayName->isFilled(BL::getError('DisplayNameIsRequired'))) { |
||
239 | // display name already exists? |
||
240 | if (BackendProfilesModel::existsDisplayName($txtDisplayName->getValue(), $this->id)) { |
||
241 | // set error |
||
242 | $txtDisplayName->addError(BL::getError('DisplayNameExists')); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | // new_password is checked, so verify new password (only if profile should not be notified) |
||
247 | // because then if the password field is empty, it will generate a new password |
||
248 | if ($chkNewPassword->isChecked() && !$this->notifyProfile) { |
||
249 | $txtPassword->isFilled(BL::err('FieldIsRequired')); |
||
250 | $txtPasswordRepeat->isFilled(BL::err('FieldIsRequired')); |
||
251 | |||
252 | // both password fields are filled in and should match |
||
253 | if ($txtPassword->isFilled() && $txtPasswordRepeat->isFilled() |
||
254 | && ($txtPassword->getValue() != $txtPasswordRepeat->getValue())) { |
||
255 | $txtPasswordRepeat->addError(BL::err('PasswordRepeatIsRequired')); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | // one of the bday fields are filled in |
||
260 | if ($ddmDay->isFilled() || $ddmMonth->isFilled() || $ddmYear->isFilled()) { |
||
261 | // valid date? |
||
262 | if (!checkdate($ddmMonth->getValue(), $ddmDay->getValue(), $ddmYear->getValue())) { |
||
263 | // set error |
||
264 | $ddmYear->addError(BL::getError('DateIsInvalid')); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | // no errors? |
||
269 | if ($this->form->isCorrect()) { |
||
270 | // build item |
||
271 | $values = ['email' => $chkNewEmail->isChecked() ? $txtEmail->getValue() : $this->profile['email']]; |
||
272 | $password = BL::lbl('YourExistingPassword'); |
||
273 | |||
274 | // only update if display name changed |
||
275 | if ($txtDisplayName->getValue() != $this->profile['display_name']) { |
||
276 | $values['display_name'] = $txtDisplayName->getValue(); |
||
277 | $values['url'] = BackendProfilesModel::getUrl( |
||
278 | $txtDisplayName->getValue(), |
||
279 | $this->id |
||
280 | ); |
||
281 | } |
||
282 | |||
283 | // new password filled in? |
||
284 | if ($chkNewPassword->isChecked()) { |
||
285 | // new password filled in? otherwise generate a password |
||
286 | $password = ($txtPassword->isFilled()) ? |
||
287 | $txtPassword->getValue() : BackendModel::generatePassword(8); |
||
288 | |||
289 | // build password |
||
290 | $values['password'] = BackendProfilesModel::encryptPassword($password); |
||
291 | } |
||
292 | |||
293 | // update values |
||
294 | BackendProfilesModel::update($this->id, $values); |
||
295 | |||
296 | // birthday is filled in |
||
297 | if ($ddmYear->isFilled()) { |
||
298 | // mysql format |
||
299 | $birthDate = $ddmYear->getValue() . '-'; |
||
300 | $birthDate .= str_pad($ddmMonth->getValue(), 2, '0', STR_PAD_LEFT) . '-'; |
||
301 | $birthDate .= str_pad($ddmDay->getValue(), 2, '0', STR_PAD_LEFT); |
||
302 | } else { |
||
303 | $birthDate = null; |
||
304 | } |
||
305 | |||
306 | // update settings |
||
307 | BackendProfilesModel::setSetting($this->id, 'first_name', $txtFirstName->getValue()); |
||
308 | BackendProfilesModel::setSetting($this->id, 'last_name', $txtLastName->getValue()); |
||
309 | BackendProfilesModel::setSetting($this->id, 'gender', $ddmGender->getValue()); |
||
310 | BackendProfilesModel::setSetting($this->id, 'birth_date', $birthDate); |
||
311 | BackendProfilesModel::setSetting($this->id, 'city', $txtCity->getValue()); |
||
312 | BackendProfilesModel::setSetting($this->id, 'country', $ddmCountry->getValue()); |
||
313 | BackendProfilesModel::setSetting($this->id, 'about', $txtAbout->getValue()); |
||
314 | |||
315 | $displayName = $values['display_name'] ?? $this->profile['display_name']; |
||
316 | |||
317 | $redirectUrl = BackendModel::createUrlForAction('Index') . |
||
318 | '&var=' . rawurlencode($values['email']) . |
||
319 | '&highlight=row-' . $this->id . |
||
320 | '&var=' . rawurlencode($displayName) . |
||
321 | '&report=' |
||
322 | ; |
||
323 | |||
324 | if ($this->notifyProfile && |
||
325 | ($chkNewEmail->isChecked() || $chkNewPassword->isChecked()) |
||
326 | ) { |
||
327 | // notify values |
||
328 | $notifyValues = array_merge( |
||
329 | $values, |
||
330 | [ |
||
331 | 'id' => $this->id, |
||
332 | 'first_name' => $txtFirstName->getValue(), |
||
333 | 'last_name' => $txtLastName->getValue(), |
||
334 | 'unencrypted_password' => $password, |
||
335 | ] |
||
336 | ); |
||
337 | |||
338 | if (!isset($notifyValues['display_name'])) { |
||
339 | $notifyValues['display_name'] = $this->profile['display_name']; |
||
340 | } |
||
341 | |||
342 | BackendProfilesModel::notifyProfile($notifyValues, true); |
||
343 | |||
344 | $redirectUrl .= 'saved-and-notified'; |
||
345 | } else { |
||
346 | $redirectUrl .= 'saved'; |
||
347 | } |
||
348 | |||
349 | // everything is saved, so redirect to the overview |
||
350 | $this->redirect($redirectUrl); |
||
351 | } |
||
365 |