<?php
namespace B2bSellersCore\Components\MailTemplate\Subscriber;
use B2bSellersCore\Components\MailTemplate\Events\MailAttachmentInterface;
use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
use Shopware\Core\Content\MailTemplate\Event\MailSendSubscriberBridgeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MailSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
FlowSendMailActionEvent::class => 'addAttachments',
MailSendSubscriberBridgeEvent::class => 'addAttachments'
];
}
/**
* @param FlowSendMailActionEvent|MailSendSubscriberBridgeEvent $event
* @return void
*/
public function addAttachments($event)
{
if ($event instanceof FlowSendMailActionEvent) {
$mailEvent = $event->getFlowEvent()->getEvent();
} else {
$mailEvent = $event->getBusinessEvent()->getEvent();
}
if (!$mailEvent instanceof MailAttachmentInterface) {
return;
}
$attachments = [];
if ($event->getDataBag()->has('binAttachments')) {
$attachments = $event->getDataBag()->get('binAttachments');
}
$attachments = array_merge($attachments, $mailEvent->getAttachments());
$event->getDataBag()->set('binAttachments', $attachments);
}
}