src/Form/EventListener/Franchise/Parameter/AddGoogleAuthEnabledFieldSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Franchise\Parameter;
  4. use App\Entity\Franchise\Franchise;
  5. use App\Entity\Franchise\Parameter;
  6. use App\Entity\Security\RoleInterface;
  7. use App\Form\FormHelper;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. use Symfony\Component\Security\Core\Security;
  13. class AddGoogleAuthEnabledFieldSubscriber extends FormHelper implements EventSubscriberInterface
  14. {
  15.     private $authorizedRoles = [RoleInterface::ROLE_ADMINRoleInterface::ROLE_PREVIOUS_ADMINRoleInterface::ROLE_SUPPORT];
  16.     public function __construct(
  17.         private Security $security,
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  23.     }
  24.     public function preSetData(FormEvent $event): void
  25.     {
  26.         if (
  27.             === \count(array_intersect($this->authorizedRoles$this->security->getToken()?->getRoleNames() ?? []))
  28.             || ($event->getData() instanceof Franchise && null !== $event->getData()->getId())
  29.         ) {
  30.             return;
  31.         }
  32.         $event->getForm()->add('googleAuthEnabled'CheckboxType::class, $this->getCheckParameters(
  33.             'app.manager.pages.settings.form.google_auth_enabled',
  34.             self::PLACEHOLDER_ENABLE_ASK,
  35.             null,
  36.             ['mapped' => $event->getData() instanceof Parameter],
  37.         ));
  38.     }
  39. }