custom/plugins/fgitsAutoinvoiceSW6/src/Subscriber/MailSubscriber.php line 93

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Fgits\AutoInvoice\Subscriber;
  3. use Fgits\AutoInvoice\Service\ConditionChecker;
  4. use Fgits\AutoInvoice\Service\CustomFields\OrderCustomFields;
  5. use Fgits\AutoInvoice\Service\DB\Document as DBDocument;
  6. use Fgits\AutoInvoice\Service\DB\Order as DBOrder;
  7. use Fgits\AutoInvoice\Service\Document;
  8. use Fgits\AutoInvoice\Service\DocumentCreator;
  9. use Fgits\AutoInvoice\Service\FgitsLibrary\Mailer as FgitsMailer;
  10. use Fgits\AutoInvoice\Service\FgitsLibrary\Utility;
  11. use Psr\Log\LoggerInterface;
  12. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. /**
  17.  * Copyright (c) 2021. GOLLE IT.
  18.  *
  19.  * @author Andrey Grigorkin <andrey@golle-it.de>
  20.  */
  21. class MailSubscriber implements EventSubscriberInterface
  22. {
  23.     private OrderCustomFields $orderCustomFields;
  24.     private DBDocument $dbDocument;
  25.     private DBOrder $dbOrder;
  26.     private ConditionChecker $conditionChecker;
  27.     private Document $document;
  28.     private DocumentCreator $documentCreator;
  29.     private FgitsMailer $mailer;
  30.     private Utility $utility;
  31.     private SessionInterface $session;
  32.     private SystemConfigService $systemConfigService;
  33.     private LoggerInterface $logger;
  34.     /**
  35.      * MailSubscriber constructor.
  36.      *
  37.      * @param OrderCustomFields $orderCustomFields
  38.      * @param DBDocument $dbDocument
  39.      * @param DBOrder $dbOrder
  40.      * @param ConditionChecker $conditionChecker
  41.      * @param Document $document
  42.      * @param DocumentCreator $documentCreator
  43.      * @param FgitsMailer $mailer
  44.      * @param Utility $utility
  45.      * @param SessionInterface $session
  46.      * @param SystemConfigService $systemConfigService
  47.      * @param LoggerInterface $logger
  48.      */
  49.     public function __construct(
  50.         OrderCustomFields $orderCustomFields,
  51.         DBDocument $dbDocument,
  52.         DBOrder $dbOrder,
  53.         ConditionChecker $conditionChecker,
  54.         Document $document,
  55.         DocumentCreator $documentCreator,
  56.         FgitsMailer $mailer,
  57.         Utility $utility,
  58.         SessionInterface $session,
  59.         SystemConfigService $systemConfigService,
  60.         LoggerInterface $logger
  61.     ) {
  62.         $this->orderCustomFields   $orderCustomFields;
  63.         $this->dbDocument          $dbDocument;
  64.         $this->dbOrder             $dbOrder;
  65.         $this->conditionChecker    $conditionChecker;
  66.         $this->document            $document;
  67.         $this->documentCreator     $documentCreator;
  68.         $this->mailer              $mailer;
  69.         $this->utility             $utility;
  70.         $this->session             $session;
  71.         $this->systemConfigService $systemConfigService;
  72.         $this->logger              $logger;
  73.     }
  74.     public static function getSubscribedEvents(): array
  75.     {
  76.         return[
  77.             MailBeforeSentEvent::class => 'onAfterCreateMessage'
  78.         ];
  79.     }
  80.     /**
  81.      * Gets invoked when an email is ready to be sent.
  82.      *
  83.      * @param MailBeforeSentEvent $event
  84.      *
  85.      * @throws \Exception
  86.      */
  87.     public function onAfterCreateMessage(MailBeforeSentEvent $event): void
  88.     {
  89.         $data $event->getData();
  90.         $config $this->systemConfigService->get('fgitsAutoinvoiceSW6.config'$data['salesChannelId']);
  91.         if ($config['active'] && !empty($config['sendCustomerEmail']) && !empty($config['attachEmail']) && isset($data['templateId'])) {
  92.             $mailTemplate $this->mailer->getMailTemplateById($data['templateId'], ['mailTemplateType'], $event->getContext());
  93.             $mailTemplateType $mailTemplate->getMailTemplateType();
  94.             if ($mailTemplateType->getTechnicalName() == $config['attachEmail']) {
  95.                 $order $this->dbOrder->getOrderById($this->session->get('fgits_autoinvoice_latest_order_id'), $this->utility->getOrderAssociations(), $event->getContext());
  96.                 if (!$this->orderCustomFields->isCustomerNoInvoice($order) && $this->conditionChecker->shouldSendInvoice($order)) {
  97.                     $this->documentCreator->createInvoice($order);
  98.                     $invoice $this->document->getInvoice($order$event->getContext());
  99.                     $event->getMessage()->attach($invoice['content'], $invoice['fileName'], $invoice['mimeType']);
  100.                     $this->dbDocument->setSent($invoice['id'], $invoice['orderVersionId']);
  101.                     $this->orderCustomFields->setOrderProcessed($order);
  102.                 }
  103.                 $this->orderCustomFields->setOrderConfirmationSent($order);
  104.             }
  105.         }
  106.     }
  107. }