<?php
declare(strict_types=1);
namespace App\Form\EventListener\Franchise;
use App\Entity\Franchise\Franchise;
use App\Entity\Franchise\Parameter;
use App\Repository\Admin\LocaleRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class ValidateFranchiseFieldsSubscriber implements EventSubscriberInterface
{
public function __construct(
private LocaleRepository $localeRepository,
) {
}
public static function getSubscribedEvents(): array
{
return [FormEvents::POST_SUBMIT => 'postSubmit'];
}
public function postSubmit(FormEvent $event): void
{
/** @var Franchise $franchise */
$franchise = $event->getData();
if (null !== $franchise && null !== $franchise->getId()) {
return;
}
if (null === $franchise->getParameter()) {
$parameter = new Parameter();
$parameter->setGoogleAuthEnabled($event->getForm()->get('googleAuthEnabled')->getData());
$parameter->setAppleAuthEnabled($event->getForm()->get('appleAuthEnabled')->getData());
$locale = $this->localeRepository->findOneBy([], ['id' => 'ASC']);
if (null !== $locale) {
$parameter->setLocale($locale);
}
$franchise->setParameter($parameter);
}
}
}