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

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 Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
  6. use Sylius\Component\Channel\Context\ChannelContextInterface;
  7. use Sylius\Component\Core\Model\ProductVariantInterface;
  8. use Sylius\Component\Locale\Context\LocaleContextInterface;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\Form\FormEvent;
  12. use Symfony\Component\Form\FormEvents;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Validator\Constraints\NotBlank;
  15. final class AvailabilityNotifierType extends AbstractResourceType
  16. {
  17.     private LocaleContextInterface $localeContext;
  18.     private ChannelContextInterface $channelContext;
  19.     public function __construct(
  20.         string $dataClass,
  21.         array $validationGroups,
  22.         LocaleContextInterface $localeContext,
  23.         ChannelContextInterface $channelContext
  24.     ) {
  25.         parent::__construct($dataClass$validationGroups);
  26.         $this->localeContext $localeContext;
  27.         $this->channelContext $channelContext;
  28.     }
  29.     public function buildForm(FormBuilderInterface $builder, array $options): void
  30.     {
  31.         $builder
  32.             ->add('emailCustomer'EmailType::class, [
  33.                 'label' => 'app.ui.email_address',
  34.                 'constraints' => [
  35.                     new NotBlank([
  36.                         'message' => 'app.customer_email.not_blank',
  37.                     ]),
  38.                 ]
  39.             ])
  40.             ->addEventListener(FormEvents::POST_SUBMIT, [$this'onPostSubmit']);
  41.     }
  42.     public function onPostSubmit(FormEvent $event): void
  43.     {
  44.         $form $event->getForm();
  45.         $productVariant $form->getConfig()->getOption('productVariant');
  46.         /** @var AvailabilityNotifier $data */
  47.         $data $event->getData();
  48.         $data->setStatus(false);
  49.         $data->setLocaleCode($this->localeContext->getLocaleCode());
  50.         $data->setChannel($this->channelContext->getChannel());
  51.         if($productVariant === null){
  52.             return;
  53.         }
  54.         $data->setProductVariant($productVariant);
  55.     }
  56.     public function configureOptions(OptionsResolver $resolver): void
  57.     {
  58.         parent::configureOptions($resolver);
  59.         $resolver
  60.             ->setDefined([
  61.                 'productVariant',
  62.             ])
  63.             ->setAllowedTypes('productVariant'ProductVariantInterface::class)
  64.         ;
  65.     }
  66.     public function getBlockPrefix(): string
  67.     {
  68.         return 'app_availability_notifier';
  69.     }
  70. }