Conditions | 33 |
Paths | > 20000 |
Total Lines | 321 |
Code Lines | 194 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
357 | private function validateForm(): void |
||
358 | { |
||
359 | // is the form submitted? |
||
360 | if ($this->form->isSubmitted()) { |
||
361 | // validate required fields |
||
362 | $this->form->getField('site_title')->isFilled(BL::err('FieldIsRequired')); |
||
363 | |||
364 | // Google Tracking options |
||
365 | if ($this->form->getField('google_tracking_google_analytics_tracking_id_enabled')->getChecked()) { |
||
366 | $this->form->getField('google_tracking_google_analytics_tracking_id')->isFilled( |
||
367 | BL::err('FieldIsRequired') |
||
368 | ); |
||
369 | } |
||
370 | if ($this->form->getField('google_tracking_google_tag_manager_container_id_enabled')->getChecked()) { |
||
371 | $this->form->getField('google_tracking_google_tag_manager_container_id')->isFilled( |
||
372 | BL::err('FieldIsRequired') |
||
373 | ); |
||
374 | } |
||
375 | |||
376 | // date & time |
||
377 | $this->form->getField('time_format')->isFilled(BL::err('FieldIsRequired')); |
||
378 | $this->form->getField('date_format_short')->isFilled(BL::err('FieldIsRequired')); |
||
379 | $this->form->getField('date_format_long')->isFilled(BL::err('FieldIsRequired')); |
||
380 | |||
381 | // number |
||
382 | $this->form->getField('number_format')->isFilled(BL::err('FieldIsRequired')); |
||
383 | |||
384 | // akismet key may be filled in |
||
385 | if ($this->needsAkismet && $this->form->getField('akismet_key')->isFilled()) { |
||
386 | // key has changed |
||
387 | if ($this->form->getField('akismet_key')->getValue() != $this->get('fork.settings')->get('Core', 'akismet_key', null)) { |
||
388 | // create instance |
||
389 | $akismet = new Akismet($this->form->getField('akismet_key')->getValue(), SITE_URL); |
||
390 | |||
391 | // invalid key |
||
392 | if (!$akismet->verifyKey()) { |
||
393 | $this->form->getField('akismet_key')->setError(BL::err('InvalidAPIKey')); |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | |||
398 | // domains filled in |
||
399 | if ($this->form->getField('site_domains')->isFilled()) { |
||
400 | // split on newlines |
||
401 | $domains = explode("\n", trim($this->form->getField('site_domains')->getValue())); |
||
402 | |||
403 | // loop domains |
||
404 | foreach ($domains as $domain) { |
||
405 | // strip funky stuff |
||
406 | $domain = trim(str_replace(['www.', 'http://', 'https://'], '', $domain)); |
||
407 | |||
408 | // invalid URL |
||
409 | if (!\SpoonFilter::isURL('http://' . $domain)) { |
||
410 | // set error |
||
411 | $this->form->getField('site_domains')->setError(BL::err('InvalidDomain')); |
||
412 | |||
413 | // stop looping domains |
||
414 | break; |
||
415 | } |
||
416 | } |
||
417 | } |
||
418 | |||
419 | if ($this->form->getField('ckfinder_image_max_width')->isFilled()) { |
||
420 | $this->form->getField( |
||
421 | 'ckfinder_image_max_width' |
||
422 | )->isInteger(BL::err('InvalidInteger')); |
||
423 | } |
||
424 | if ($this->form->getField('ckfinder_image_max_height')->isFilled()) { |
||
425 | $this->form->getField( |
||
426 | 'ckfinder_image_max_height' |
||
427 | )->isInteger(BL::err('InvalidInteger')); |
||
428 | } |
||
429 | |||
430 | $privacyConsentLevelsField = $this->form->getField('privacy_consent_levels'); |
||
431 | if ($privacyConsentLevelsField->isFilled()) { |
||
432 | $levels = explode(',', $privacyConsentLevelsField->getValue()); |
||
433 | foreach ($levels as $level) { |
||
434 | if (!preg_match('/^[a-z_\x7f-\xff][a-z0-9_\x7f-\xff]*$/i', $level)) { |
||
435 | $privacyConsentLevelsField->setError(sprintf(BL::err('InvalidVariableName'), $level)); |
||
436 | break; |
||
437 | } |
||
438 | } |
||
439 | } |
||
440 | |||
441 | |||
442 | // no errors ? |
||
443 | if ($this->form->isCorrect()) { |
||
444 | // general settings |
||
445 | $this->get('fork.settings')->set( |
||
446 | 'Core', |
||
447 | 'site_title_' . BL::getWorkingLanguage(), |
||
448 | $this->form->getField('site_title')->getValue() |
||
449 | ); |
||
450 | |||
451 | if ($this->form->getField('google_tracking_google_analytics_tracking_id_enabled')->isChecked()) { |
||
452 | $googleTrackingAnalyticsTrackingId = $this->form->getField('google_tracking_google_analytics_tracking_id')->getValue(); |
||
453 | } else { |
||
454 | $googleTrackingAnalyticsTrackingId = ''; |
||
455 | } |
||
456 | $this->get('fork.settings')->set( |
||
457 | 'Core', |
||
458 | 'google_tracking_google_analytics_tracking_id', |
||
459 | $googleTrackingAnalyticsTrackingId |
||
460 | ); |
||
461 | |||
462 | if ($this->form->getField('google_tracking_google_tag_manager_container_id_enabled')->isChecked()) { |
||
463 | $googleTrackingTagManagerContainerId = $this->form->getField('google_tracking_google_tag_manager_container_id')->getValue(); |
||
464 | } else { |
||
465 | $googleTrackingTagManagerContainerId = ''; |
||
466 | } |
||
467 | $this->get('fork.settings')->set( |
||
468 | 'Core', |
||
469 | 'google_tracking_google_tag_manager_container_id', |
||
470 | $googleTrackingTagManagerContainerId |
||
471 | ); |
||
472 | |||
473 | $this->get('fork.settings')->set( |
||
474 | 'Core', |
||
475 | 'site_html_head', |
||
476 | $this->form->getField('site_html_head')->getValue() |
||
477 | ); |
||
478 | $this->get('fork.settings')->set( |
||
479 | 'Core', |
||
480 | 'site_html_start_of_body', |
||
481 | $this->form->getField('site_html_start_of_body')->getValue() |
||
482 | ); |
||
483 | // @deprecated remove this in Fork 6, use site_html_start_of_body |
||
484 | $this->get('fork.settings')->set( |
||
485 | 'Core', |
||
486 | 'site_start_of_body_scripts', |
||
487 | $this->form->getField('site_html_start_of_body')->getValue() |
||
488 | ); |
||
489 | $this->get('fork.settings')->set( |
||
490 | 'Core', |
||
491 | 'site_html_end_of_body', |
||
492 | $this->form->getField('site_html_end_of_body')->getValue() |
||
493 | ); |
||
494 | // @deprecated remove this in Fork 6, use site_html_end_of_body |
||
495 | $this->get('fork.settings')->set( |
||
496 | 'Core', |
||
497 | 'site_html_footer', |
||
498 | $this->form->getField('site_html_end_of_body')->getValue() |
||
499 | ); |
||
500 | |||
501 | // facebook settings |
||
502 | $this->get('fork.settings')->set( |
||
503 | 'Core', |
||
504 | 'facebook_admin_ids', |
||
505 | ($this->form->getField('facebook_admin_ids')->isFilled()) ? $this->form->getField( |
||
506 | 'facebook_admin_ids' |
||
507 | )->getValue() : null |
||
508 | ); |
||
509 | $this->get('fork.settings')->set( |
||
510 | 'Core', |
||
511 | 'facebook_app_id', |
||
512 | ($this->form->getField('facebook_application_id')->isFilled()) ? $this->form->getField( |
||
513 | 'facebook_application_id' |
||
514 | )->getValue() : null |
||
515 | ); |
||
516 | $this->get('fork.settings')->set( |
||
517 | 'Core', |
||
518 | 'facebook_app_secret', |
||
519 | ($this->form->getField('facebook_application_secret')->isFilled()) ? $this->form->getField( |
||
520 | 'facebook_application_secret' |
||
521 | )->getValue() : null |
||
522 | ); |
||
523 | |||
524 | // twitter settings |
||
525 | /** @var \SpoonFormText $txtTwitterSiteName */ |
||
526 | $txtTwitterSiteName = $this->form->getField('twitter_site_name'); |
||
527 | if ($txtTwitterSiteName->isFilled()) { |
||
528 | $this->get('fork.settings')->set( |
||
529 | 'Core', |
||
530 | 'twitter_site_name', |
||
531 | '@' . ltrim($txtTwitterSiteName->getValue(), '@') |
||
532 | ); |
||
533 | } |
||
534 | |||
535 | // ckfinder settings |
||
536 | $this->get('fork.settings')->set( |
||
537 | 'Core', |
||
538 | 'ckfinder_license_name', |
||
539 | ($this->form->getField('ckfinder_license_name')->isFilled()) ? $this->form->getField( |
||
540 | 'ckfinder_license_name' |
||
541 | )->getValue() : null |
||
542 | ); |
||
543 | $this->get('fork.settings')->set( |
||
544 | 'Core', |
||
545 | 'ckfinder_license_key', |
||
546 | ($this->form->getField('ckfinder_license_key')->isFilled()) ? $this->form->getField( |
||
547 | 'ckfinder_license_key' |
||
548 | )->getValue() : null |
||
549 | ); |
||
550 | $this->get('fork.settings')->set( |
||
551 | 'Core', |
||
552 | 'ckfinder_image_max_width', |
||
553 | ($this->form->getField('ckfinder_image_max_width')->isFilled()) ? $this->form->getField( |
||
554 | 'ckfinder_image_max_width' |
||
555 | )->getValue() : 1600 |
||
556 | ); |
||
557 | $this->get('fork.settings')->set( |
||
558 | 'Core', |
||
559 | 'ckfinder_image_max_height', |
||
560 | ($this->form->getField('ckfinder_image_max_height')->isFilled()) ? $this->form->getField( |
||
561 | 'ckfinder_image_max_height' |
||
562 | )->getValue() : 1200 |
||
563 | ); |
||
564 | |||
565 | // api keys |
||
566 | if ($this->needsAkismet) { |
||
567 | $this->get('fork.settings')->set( |
||
568 | 'Core', |
||
569 | 'akismet_key', |
||
570 | $this->form->getField('akismet_key')->getValue() |
||
571 | ); |
||
572 | } |
||
573 | if ($this->needsGoogleMaps) { |
||
574 | $this->get('fork.settings')->set( |
||
575 | 'Core', |
||
576 | 'google_maps_key', |
||
577 | $this->form->getField('google_maps_key')->getValue() |
||
578 | ); |
||
579 | } |
||
580 | if ($this->needsGoogleRecaptcha) { |
||
581 | $this->get('fork.settings')->set( |
||
582 | 'Core', |
||
583 | 'google_recaptcha_site_key', |
||
584 | $this->form->getField('google_recaptcha_site_key')->getValue() |
||
585 | ); |
||
586 | $this->get('fork.settings')->set( |
||
587 | 'Core', |
||
588 | 'google_recaptcha_secret_key', |
||
589 | $this->form->getField('google_recaptcha_secret_key')->getValue() |
||
590 | ); |
||
591 | } |
||
592 | |||
593 | // date & time formats |
||
594 | $this->get('fork.settings')->set( |
||
595 | 'Core', |
||
596 | 'time_format', |
||
597 | $this->form->getField('time_format')->getValue() |
||
598 | ); |
||
599 | $this->get('fork.settings')->set( |
||
600 | 'Core', |
||
601 | 'date_format_short', |
||
602 | $this->form->getField('date_format_short')->getValue() |
||
603 | ); |
||
604 | $this->get('fork.settings')->set( |
||
605 | 'Core', |
||
606 | 'date_format_long', |
||
607 | $this->form->getField('date_format_long')->getValue() |
||
608 | ); |
||
609 | |||
610 | // date & time formats |
||
611 | $this->get('fork.settings')->set( |
||
612 | 'Core', |
||
613 | 'number_format', |
||
614 | $this->form->getField('number_format')->getValue() |
||
615 | ); |
||
616 | |||
617 | // before we save the languages, we need to ensure that each language actually exists and may be chosen. |
||
618 | $languages = [SITE_DEFAULT_LANGUAGE]; |
||
619 | $activeLanguages = array_unique( |
||
620 | array_merge($languages, $this->form->getField('active_languages')->getValue()) |
||
621 | ); |
||
622 | $redirectLanguages = array_unique( |
||
623 | array_merge($languages, $this->form->getField('redirect_languages')->getValue()) |
||
624 | ); |
||
625 | |||
626 | // cleanup redirect-languages, by removing the values that aren't present in the active languages |
||
627 | $redirectLanguages = array_intersect($redirectLanguages, $activeLanguages); |
||
628 | |||
629 | // save active languages |
||
630 | $this->get('fork.settings')->set('Core', 'active_languages', $activeLanguages); |
||
631 | $this->get('fork.settings')->set('Core', 'redirect_languages', $redirectLanguages); |
||
632 | |||
633 | // domains may not contain www, http or https. Therefor we must loop and create the list of domains. |
||
634 | $siteDomains = []; |
||
635 | |||
636 | // domains filled in |
||
637 | if ($this->form->getField('site_domains')->isFilled()) { |
||
638 | // split on newlines |
||
639 | $domains = explode("\n", trim($this->form->getField('site_domains')->getValue())); |
||
640 | |||
641 | // loop domains |
||
642 | foreach ($domains as $domain) { |
||
643 | // strip funky stuff |
||
644 | $siteDomains[] = trim(str_replace(['www.', 'http://', 'https://'], '', $domain)); |
||
645 | } |
||
646 | } |
||
647 | |||
648 | // save domains |
||
649 | $this->get('fork.settings')->set('Core', 'site_domains', $siteDomains); |
||
650 | |||
651 | // cookies |
||
652 | // @deprecated remove this in Fork 6, the privacy consent dialog should be used |
||
653 | $this->get('fork.settings')->set( |
||
654 | 'Core', |
||
655 | 'show_cookie_bar', |
||
656 | $this->form->getField('show_cookie_bar')->getChecked() |
||
657 | ); |
||
658 | |||
659 | // privacy |
||
660 | $this->get('fork.settings')->set( |
||
661 | 'Core', |
||
662 | 'show_consent_dialog', |
||
663 | $this->form->getField('show_consent_dialog')->getChecked() |
||
664 | ); |
||
665 | $privacyConsentLevels = []; |
||
666 | if ($privacyConsentLevelsField->isFilled()) { |
||
667 | $privacyConsentLevels = explode(',', $privacyConsentLevelsField->getValue()); |
||
668 | } |
||
669 | $this->get('fork.settings')->set( |
||
670 | 'Core', |
||
671 | 'privacy_consent_levels', |
||
672 | $privacyConsentLevels |
||
673 | ); |
||
674 | |||
675 | // assign report |
||
676 | $this->template->assign('report', true); |
||
677 | $this->template->assign('reportMessage', BL::msg('Saved')); |
||
678 | } |
||
682 |