src/Form/Type/UserAlertStock/AvailabilityNotifierType.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\Type\UserAlertStock;
  4. use App\Entity\UserAlertStock\AvailabilityNotifier;
  5. use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
  6. use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;
  7. use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
  8. use Sylius\Component\Channel\Context\ChannelContextInterface;
  9. use Sylius\Component\Core\Model\ProductVariantInterface;
  10. use Sylius\Component\Locale\Context\LocaleContextInterface;
  11. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. final class AvailabilityNotifierType extends AbstractResourceType
  18. {
  19.     private LocaleContextInterface $localeContext;
  20.     private ChannelContextInterface $channelContext;
  21.     public function __construct(
  22.         string $dataClass,
  23.         array $validationGroups,
  24.         LocaleContextInterface $localeContext,
  25.         ChannelContextInterface $channelContext
  26.     ) {
  27.         parent::__construct($dataClass$validationGroups);
  28.         $this->localeContext $localeContext;
  29.         $this->channelContext $channelContext;
  30.     }
  31.     public function buildForm(FormBuilderInterface $builder, array $options): void
  32.     {
  33.         $builder
  34.             ->add('emailCustomer'EmailType::class, [
  35.                 'label' => 'app.ui.email_address',
  36.                 'constraints' => [
  37.                     new NotBlank([
  38.                         'message' => 'app.customer_email.not_blank',
  39.                     ]),
  40.                 ]
  41.             ])
  42.             ->add('captcha'Recaptcha3Type::class, [
  43.                 'constraints' => new Recaptcha3([
  44.                     'message'             => 'karser_recaptcha3.message',
  45.                     'messageMissingValue' => 'karser_recaptcha3.message_missing_value',
  46.                     'groups' => ['sylius']
  47.                 ]),
  48.                 'action_name' => 'availability_notifier',
  49.             ])
  50.             ->addEventListener(FormEvents::POST_SUBMIT, [$this'onPostSubmit']);
  51.     }
  52.     public function onPostSubmit(FormEvent $event): void
  53.     {
  54.         $form $event->getForm();
  55.         $productVariant $form->getConfig()->getOption('productVariant');
  56.         /** @var AvailabilityNotifier $data */
  57.         $data $event->getData();
  58.         $data->setStatus(false);
  59.         $data->setLocaleCode($this->localeContext->getLocaleCode());
  60.         $data->setChannel($this->channelContext->getChannel());
  61.         if($productVariant === null){
  62.             return;
  63.         }
  64.         $data->setProductVariant($productVariant);
  65.     }
  66.     public function configureOptions(OptionsResolver $resolver): void
  67.     {
  68.         parent::configureOptions($resolver);
  69.         $resolver
  70.             ->setDefined([
  71.                 'productVariant',
  72.             ])
  73.             ->setAllowedTypes('productVariant'ProductVariantInterface::class)
  74.         ;
  75.     }
  76.     public function getBlockPrefix(): string
  77.     {
  78.         return 'app_availability_notifier';
  79.     }
  80. }