custom/plugins/NetiNextEasyCouponDesigns/src/Subscriber/MailSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCouponDesigns\Subscriber;
  4. use Doctrine\DBAL\Exception;
  5. use NetInventors\NetiNextEasyCouponDesigns\Core\Content\Design\Aggregate\Task\TaskEntity;
  6. use NetInventors\NetiNextEasyCouponDesigns\Events\BusinessEvent\VoucherPdfMailEvent;
  7. use NetInventors\NetiNextEasyCouponDesigns\Events\FlowEvent\VoucherPdfMailEvent as FlowPdfEvent;
  8. use NetInventors\NetiNextEasyCouponDesigns\Service\TaskHandler\Mailer;
  9. use Shopware\Core\Checkout\Order\OrderEntity;
  10. use Shopware\Core\Content\MailTemplate\Service\Event\MailSentEvent;
  11. use Shopware\Core\Content\MailTemplate\Subscriber\MailSendSubscriberConfig;
  12. use Shopware\Core\Defaults;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class MailSubscriber implements EventSubscriberInterface
  15. {
  16.     private bool   $isPdfDesignsEvent false;
  17.     private Mailer $taskMailerService;
  18.     public function __construct(Mailer $taskMailerService)
  19.     {
  20.         $this->taskMailerService $taskMailerService;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             MailSentEvent::class       => 'onMailSent',
  26.             VoucherPdfMailEvent::class => 'onPdfSent',
  27.             FlowPdfEvent::class        => 'onPdfSent',
  28.         ];
  29.     }
  30.     /**
  31.      * @throws Exception
  32.      */
  33.     public function onMailSent(MailSentEvent $event): void
  34.     {
  35.         if (!$this->isPdfDesignsEvent) {
  36.             return;
  37.         }
  38.         $context   $event->getContext();
  39.         $extension $context->getExtension('mail-attachments');
  40.         if (!$extension instanceof MailSendSubscriberConfig) {
  41.             return;
  42.         }
  43.         $documentsIds $extension->getDocumentIds();
  44.         if ($documentsIds === []) {
  45.             return;
  46.         }
  47.         $orderSearchResult $this->taskMailerService->getOrdersFromDocumentIds($documentsIds$context);
  48.         /** @var OrderEntity $order */
  49.         foreach ($orderSearchResult->getElements() as $order) {
  50.             $this->taskMailerService->updateMailStatus($orderTaskEntity::MAIL_STATUS_SENT);
  51.         }
  52.         $this->taskMailerService->markSentDocuments($documentsIds$context);
  53.         $this->isPdfDesignsEvent false;
  54.     }
  55.     public function onPdfSent(VoucherPdfMailEvent $event): void
  56.     {
  57.         $this->isPdfDesignsEvent true;
  58.     }
  59. }