<?php declare(strict_types=1);
namespace Samson\Subscriber;
/***
*
* This file is part of the "SAMSON Shop" project.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2021
*
***/
use GuzzleHttp\Psr7\MimeType;
use Samson\Entities\DownloadLink\DownloadLinkCollection;
use Samson\Event\TradeFairProductSelectionEvent;
use Shopware\Core\Content\MailTemplate\Event\MailSendSubscriberBridgeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
class MailSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'onMailBeforeValidate',
MailSendSubscriberBridgeEvent::class => 'onMailSendSubscriberBridgeEvent'
];
}
public function onMailSendSubscriberBridgeEvent(MailSendSubscriberBridgeEvent $event)
{
if ($event->getBusinessEvent()->getEvent() instanceof TradeFairProductSelectionEvent) {
/** @var TradeFairProductSelectionEvent $tradeFairProductSelectionEvent */
$tradeFairProductSelectionEvent = $event->getBusinessEvent()->getEvent();
$product = $tradeFairProductSelectionEvent->getProduct();
/** @var DownloadLinkCollection $downloadLinks */
$downloadLinks = $product->getExtension('downloadLinks');
$attachments = [];
foreach ($downloadLinks as $downloadLink) {
$type = pathinfo($downloadLink->getLink());
$attachments[] = [
'content' => file_get_contents($downloadLink->getLink()),
'fileName' => $type['filename'],
'mimeType' => MimeType::fromExtension($type['extension'])
];
}
if (count($attachments) > 0) {
$event->getDataBag()->set('binAttachments', $attachments);
}
}
}
public function onMailBeforeValidate(MailBeforeValidateEvent $event)
{
$replyTo = getenv('MAILER_REPLY_TO');
if (!empty($replyTo)) {
$event->addData('replyTo', $replyTo);
}
}
}