<?php
declare(strict_types=1);
namespace NetInventors\NetiNextEasyCouponDesigns\Subscriber;
use Doctrine\DBAL\Exception;
use NetInventors\NetiNextEasyCouponDesigns\Core\Content\Design\Aggregate\Task\TaskEntity;
use NetInventors\NetiNextEasyCouponDesigns\Events\BusinessEvent\VoucherPdfMailEvent;
use NetInventors\NetiNextEasyCouponDesigns\Events\FlowEvent\VoucherPdfMailEvent as FlowPdfEvent;
use NetInventors\NetiNextEasyCouponDesigns\Service\TaskHandler\Mailer;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Content\MailTemplate\Service\Event\MailSentEvent;
use Shopware\Core\Content\MailTemplate\Subscriber\MailSendSubscriberConfig;
use Shopware\Core\Defaults;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MailSubscriber implements EventSubscriberInterface
{
private bool $isPdfDesignsEvent = false;
private Mailer $taskMailerService;
public function __construct(Mailer $taskMailerService)
{
$this->taskMailerService = $taskMailerService;
}
public static function getSubscribedEvents(): array
{
return [
MailSentEvent::class => 'onMailSent',
VoucherPdfMailEvent::class => 'onPdfSent',
FlowPdfEvent::class => 'onPdfSent',
];
}
/**
* @throws Exception
*/
public function onMailSent(MailSentEvent $event): void
{
if (!$this->isPdfDesignsEvent) {
return;
}
$context = $event->getContext();
$extension = $context->getExtension('mail-attachments');
if (!$extension instanceof MailSendSubscriberConfig) {
return;
}
$documentsIds = $extension->getDocumentIds();
if ($documentsIds === []) {
return;
}
$orderSearchResult = $this->taskMailerService->getOrdersFromDocumentIds($documentsIds, $context);
/** @var OrderEntity $order */
foreach ($orderSearchResult->getElements() as $order) {
$this->taskMailerService->updateMailStatus($order, TaskEntity::MAIL_STATUS_SENT);
}
$this->taskMailerService->markSentDocuments($documentsIds, $context);
$this->isPdfDesignsEvent = false;
}
public function onPdfSent(VoucherPdfMailEvent $event): void
{
$this->isPdfDesignsEvent = true;
}
}