custom/static-plugins/SamsonCustomer/src/Subscriber/MailSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2021
  11.  *
  12.  ***/
  13. use GuzzleHttp\Psr7\MimeType;
  14. use Samson\Entities\DownloadLink\DownloadLinkCollection;
  15. use Samson\Event\TradeFairProductSelectionEvent;
  16. use Shopware\Core\Content\MailTemplate\Event\MailSendSubscriberBridgeEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  19. class MailSubscriber implements EventSubscriberInterface
  20. {
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             MailBeforeValidateEvent::class => 'onMailBeforeValidate',
  25.             MailSendSubscriberBridgeEvent::class => 'onMailSendSubscriberBridgeEvent'
  26.         ];
  27.     }
  28.     public function onMailSendSubscriberBridgeEvent(MailSendSubscriberBridgeEvent $event)
  29.     {
  30.         if ($event->getBusinessEvent()->getEvent() instanceof TradeFairProductSelectionEvent) {
  31.             /** @var TradeFairProductSelectionEvent $tradeFairProductSelectionEvent */
  32.             $tradeFairProductSelectionEvent $event->getBusinessEvent()->getEvent();
  33.             $product $tradeFairProductSelectionEvent->getProduct();
  34.             /** @var DownloadLinkCollection $downloadLinks */
  35.             $downloadLinks $product->getExtension('downloadLinks');
  36.             $attachments = [];
  37.             foreach ($downloadLinks as $downloadLink) {
  38.                 $type pathinfo($downloadLink->getLink());
  39.                 $attachments[] = [
  40.                     'content' => file_get_contents($downloadLink->getLink()),
  41.                     'fileName' => $type['filename'],
  42.                     'mimeType' => MimeType::fromExtension($type['extension'])
  43.                 ];
  44.             }
  45.             if (count($attachments) > 0) {
  46.                 $event->getDataBag()->set('binAttachments'$attachments);
  47.             }
  48.         }
  49.     }
  50.     public function onMailBeforeValidate(MailBeforeValidateEvent $event)
  51.     {
  52.         $replyTo getenv('MAILER_REPLY_TO');
  53.         if (!empty($replyTo)) {
  54.             $event->addData('replyTo'$replyTo);
  55.         }
  56.     }
  57. }