src/Form/EventListener/Franchise/ValidateFranchiseFieldsSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Franchise;
  4. use App\Entity\Franchise\Franchise;
  5. use App\Entity\Franchise\Parameter;
  6. use App\Repository\Admin\LocaleRepository;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. class ValidateFranchiseFieldsSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private LocaleRepository $localeRepository,
  14.     ) {
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [FormEvents::POST_SUBMIT => 'postSubmit'];
  19.     }
  20.     public function postSubmit(FormEvent $event): void
  21.     {
  22.         /** @var Franchise $franchise */
  23.         $franchise $event->getData();
  24.         if (null !== $franchise && null !== $franchise->getId()) {
  25.             return;
  26.         }
  27.         if (null === $franchise->getParameter()) {
  28.             $parameter = new Parameter();
  29.             $parameter->setGoogleAuthEnabled($event->getForm()->get('googleAuthEnabled')->getData());
  30.             $parameter->setAppleAuthEnabled($event->getForm()->get('appleAuthEnabled')->getData());
  31.             $locale $this->localeRepository->findOneBy([], ['id' => 'ASC']);
  32.             if (null !== $locale) {
  33.                 $parameter->setLocale($locale);
  34.             }
  35.             $franchise->setParameter($parameter);
  36.         }
  37.     }
  38. }