<?php
namespace Prems\Plugin\PremsIndividualOffer6\Subscriber;
use Prems\Plugin\PremsIndividualOffer6\Core\Lifecycle\InstallUninstall;
use Prems\Plugin\PremsIndividualOffer6\Core\Offer\PdfService;
use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\ConfigService;
use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\OfferService;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BeforeMailValidateSubscriber implements EventSubscriberInterface
{
private PdfService $pdfService;
private AbstractSalesChannelContextFactory $salesChannelContextFactory;
private OfferService $offerService;
private ConfigService $configService;
public function __construct(
PdfService $pdfService,
AbstractSalesChannelContextFactory $salesChannelContextFactory,
OfferService $offerService,
ConfigService $configService
) {
$this->pdfService = $pdfService;
$this->salesChannelContextFactory = $salesChannelContextFactory;
$this->offerService = $offerService;
$this->configService = $configService;
}
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'beforeEmailValidate'
];
}
public function beforeEmailValidate(MailBeforeValidateEvent $event): void
{
$data = $event->getData();
if (!isset($data['offerId'], $data['technicalTemplateTypeName'])) {
return;
}
if ($data['technicalTemplateTypeName'] !== InstallUninstall::TEMPLATE_CREATED_OFFER_TECHNICAL_NAME) {
return;
}
$offer = $this->offerService->getOfferById($data['offerId'], $event->getContext());
$salesChannelContext = $this->salesChannelContextFactory->create(Uuid::randomHex(), $data['salesChannelId'], [
SalesChannelContextService::CUSTOMER_ID => $data['customerId'],
SalesChannelContextService::LANGUAGE_ID => $offer->getLanguageId(),
SalesChannelContextService::CURRENCY_ID => $offer->getCurrencyId()
]);
if (!$this->configService->getConfig($salesChannelContext)->isSendPdfWithEmail()) {
return;
}
$pdfFile = $this->pdfService->createPdfForOffer($data['offerId'], $salesChannelContext);
$binAttachments = $data['binAttachments'] ?? [];
$binAttachments[] = [
'content' => $pdfFile->getFileBlob(),
'fileName' => $pdfFile->getFileName(),
'mimeType' => $pdfFile->getContentType()
];
$event->setData(array_merge($data, ['binAttachments' => $binAttachments]));
}
}