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

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\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\Form\FormEvent;
  15. use Symfony\Component\Form\FormEvents;
  16. use Symfony\Component\OptionsResolver\OptionsResolver;
  17. use Symfony\Component\Validator\Constraints\NotBlank;
  18. final class AvailabilityNotifierType extends AbstractResourceType
  19. {
  20.     private LocaleContextInterface $localeContext;
  21.     private ChannelContextInterface $channelContext;
  22.     public function __construct(
  23.         string $dataClass,
  24.         array $validationGroups,
  25.         LocaleContextInterface $localeContext,
  26.         ChannelContextInterface $channelContext,
  27.     ) {
  28.         parent::__construct($dataClass$validationGroups);
  29.         $this->localeContext $localeContext;
  30.         $this->channelContext $channelContext;
  31.     }
  32.     public function buildForm(FormBuilderInterface $builder, array $options): void
  33.     {
  34.         $builder
  35.             ->add('emailCustomer'EmailType::class, [
  36.                 'label' => 'app.ui.email_address',
  37.                 'constraints' => [
  38.                     new NotBlank([
  39.                         'message' => 'app.customer_email.not_blank',
  40.                         'groups' => 'sylius',
  41.                     ]),
  42.                 ],
  43.             ])
  44.             ->add('firstName'TextType::class, [
  45.                 'label' => 'app.ui.first_name',
  46.                 'constraints' => [
  47.                     new NotBlank([
  48.                         'message' => 'sylius.address.first_name.not_blank',
  49.                         'groups' => 'sylius',
  50.                     ]),
  51.                 ],
  52.             ])
  53.             ->add('lastName'TextType::class, [
  54.                 'label' => 'app.ui.first_name',
  55.                 'constraints' => [
  56.                     new NotBlank([
  57.                         'message' => 'sylius.address.last_name.not_blank',
  58.                         'groups' => 'sylius',
  59.                     ]),
  60.                 ],
  61.             ])
  62.         ;
  63.         if ($_ENV['APP_ENV'] != 'dev') {
  64.             $builder->add('captcha'Recaptcha3Type::class, [
  65.                 'constraints' => new Recaptcha3([
  66.                     'message' => 'karser_recaptcha3.message',
  67.                     'messageMissingValue' => 'karser_recaptcha3.message_missing_value',
  68.                     'groups' => ['sylius'],
  69.                 ]),
  70.                 'action_name' => 'availability_notifier',
  71.             ]);
  72.         }
  73.         $builder->addEventListener(FormEvents::POST_SUBMIT, [$this'onPostSubmit'])
  74.         ;
  75.     }
  76.     public function onPostSubmit(FormEvent $event): void
  77.     {
  78.         $form $event->getForm();
  79.         $productVariant $form->getConfig()->getOption('productVariant');
  80.         /** @var AvailabilityNotifier $data */
  81.         $data $event->getData();
  82.         $data->setStatus(false);
  83.         $data->setLocaleCode($this->localeContext->getLocaleCode());
  84.         $data->setChannel($this->channelContext->getChannel());
  85.         if ($productVariant === null) {
  86.             return;
  87.         }
  88.         $data->setProductVariant($productVariant);
  89.     }
  90.     public function configureOptions(OptionsResolver $resolver): void
  91.     {
  92.         parent::configureOptions($resolver);
  93.         $resolver
  94.             ->setDefined([
  95.                 'productVariant',
  96.             ])
  97.             ->setAllowedTypes('productVariant'ProductVariantInterface::class)
  98.         ;
  99.     }
  100.     public function getBlockPrefix(): string
  101.     {
  102.         return 'app_availability_notifier';
  103.     }
  104. }