custom/plugins/PremsIndividualOffer6/src/Subscriber/BeforeMailValidateSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace Prems\Plugin\PremsIndividualOffer6\Subscriber;
  3. use Prems\Plugin\PremsIndividualOffer6\Core\Lifecycle\InstallUninstall;
  4. use Prems\Plugin\PremsIndividualOffer6\Core\Offer\PdfService;
  5. use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\ConfigService;
  6. use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\OfferService;
  7. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  10. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class BeforeMailValidateSubscriber implements EventSubscriberInterface
  13. {
  14.     private PdfService $pdfService;
  15.     private AbstractSalesChannelContextFactory $salesChannelContextFactory;
  16.     private OfferService $offerService;
  17.     private ConfigService $configService;
  18.     public function __construct(
  19.         PdfService $pdfService,
  20.         AbstractSalesChannelContextFactory $salesChannelContextFactory,
  21.         OfferService $offerService,
  22.         ConfigService $configService
  23.     ) {
  24.         $this->pdfService $pdfService;
  25.         $this->salesChannelContextFactory $salesChannelContextFactory;
  26.         $this->offerService $offerService;
  27.         $this->configService $configService;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             MailBeforeValidateEvent::class => 'beforeEmailValidate'
  33.         ];
  34.     }
  35.     public function beforeEmailValidate(MailBeforeValidateEvent $event): void
  36.     {
  37.         $data $event->getData();
  38.         if (!isset($data['offerId'], $data['technicalTemplateTypeName'])) {
  39.             return;
  40.         }
  41.         if ($data['technicalTemplateTypeName'] !== InstallUninstall::TEMPLATE_CREATED_OFFER_TECHNICAL_NAME) {
  42.             return;
  43.         }
  44.         $offer $this->offerService->getOfferById($data['offerId'], $event->getContext());
  45.         $salesChannelContext $this->salesChannelContextFactory->create(Uuid::randomHex(), $data['salesChannelId'], [
  46.             SalesChannelContextService::CUSTOMER_ID => $data['customerId'],
  47.             SalesChannelContextService::LANGUAGE_ID => $offer->getLanguageId(),
  48.             SalesChannelContextService::CURRENCY_ID => $offer->getCurrencyId()
  49.         ]);
  50.         if (!$this->configService->getConfig($salesChannelContext)->isSendPdfWithEmail()) {
  51.             return;
  52.         }
  53.         $pdfFile $this->pdfService->createPdfForOffer($data['offerId'], $salesChannelContext);
  54.         $binAttachments $data['binAttachments'] ?? [];
  55.         $binAttachments[] = [
  56.             'content' => $pdfFile->getFileBlob(),
  57.             'fileName' => $pdfFile->getFileName(),
  58.             'mimeType' => $pdfFile->getContentType()
  59.         ];
  60.         $event->setData(array_merge($data, ['binAttachments' => $binAttachments]));
  61.     }
  62. }