custom/plugins/CompraInvoiceDispatchSW6/src/Storefront/Subscriber/FrontendSubscriber.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\InvoiceDispatchSW6\Storefront\Subscriber;
  3. use Compra\InvoiceDispatchSW6\Core\System\InvoiceDispatchService;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  5. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Validator\Constraints\NotBlank;
  10. class FrontendSubscriber implements EventSubscriberInterface
  11. {
  12.     /** @var InvoiceDispatchService  */
  13.     protected $invoiceDispatchService;
  14.     /** @var Request */
  15.     protected $request;
  16.     public function __construct(
  17.         InvoiceDispatchService $invoiceDispatchService,
  18.         RequestStack $requestStack
  19.     )
  20.     {
  21.         $this->invoiceDispatchService $invoiceDispatchService;
  22.         $this->request $requestStack->getCurrentRequest();
  23.     }
  24.     /**
  25.      * @return string[]
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             CustomerRegisterEvent::class => 'onCustomerRegister',
  31.             'framework.validation.customer.create' => 'onBuildValidation'
  32.         ];
  33.     }
  34.     /**
  35.      * @param BuildValidationEvent $event
  36.      */
  37.     public function onBuildValidation(BuildValidationEvent $event): void
  38.     {
  39.         if ($event->getDefinition()->getName() !== 'customer.create') {
  40.             return;
  41.         }
  42.         // add constraint notBlank on invoice mail address if 'mail' is selected
  43.         if ($this->request->get('invoice_dispatch') === InvoiceDispatchService::INVOICE_DISPATCH_MAIL) {
  44.             $event->getDefinition()->add('invoice_mail_address', new NotBlank());
  45.         }
  46.     }
  47.     /**
  48.      * @param CustomerRegisterEvent $event
  49.      */
  50.     public function onCustomerRegister(CustomerRegisterEvent $event): void
  51.     {
  52.         $invoiceDispatchMode $this->request->get('invoice_dispatch');
  53.         $invoiceMailAddress $this->request->get('invoice_mail_address');
  54.         $this->invoiceDispatchService->saveInvoiceDispatch($invoiceDispatchMode$invoiceMailAddress$event->getCustomer(), $event->getSalesChannelContext()->getContext());
  55.     }
  56. }