custom/plugins/BucsItTimeline/src/Subscriber/AddCustomTimelineEntriesSubscriber.php line 105

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (C) web fabric gmbh - All Rights Reserved
  5.  * Unauthorized copying of this file, via any medium is strictly prohibited
  6.  * Proprietary and confidential
  7.  * Written by Dominik Mank <entwicklung@web-fabric.de>, December 2020
  8.  */
  9. namespace BucsItTimeline\Subscriber;
  10. use BucsItTimeline\Entity\BucsTimelineCustomDefinition;
  11. use Shopware\Core\Checkout\Customer\CustomerEntity;
  12. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  13. use Shopware\Core\Checkout\Order\OrderEntity;
  14. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  15. use Shopware\Core\Content\MailTemplate\Service\Event\MailSentEvent;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class AddCustomTimelineEntriesSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var EntityRepositoryInterface
  22.      */
  23.     private $customTimelineEntriesRepository;
  24.     private $preparedData = [];
  25.     public function __construct(EntityRepositoryInterface $customTimelineEntriesRepository)
  26.     {
  27.         $this->customTimelineEntriesRepository $customTimelineEntriesRepository;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             MailBeforeValidateEvent::class => 'onMailBeforeValidate',
  33.             MailSentEvent::class => 'onMailSent'
  34.         ];
  35.     }
  36.     public function onMailBeforeValidate(MailBeforeValidateEvent $event)
  37.     {
  38.         $templateData $event->getTemplateData();
  39.         $data $event->getData();
  40.         $order $templateData['order'] ?? null;
  41.         $customer $templateData['customer'] ?? null;
  42.         $comment $templateData['BucsTimelineComment'] ?? null;
  43.         if ($order instanceof OrderEntity) {
  44.             $this->preparedData = [
  45.                 [
  46.                     'timelineType' => BucsTimelineCustomDefinition::TIMELINE_TYPE_ORDER,
  47.                     'title' => 'bucs-timeline.order.mailSentTitle',
  48.                     'message' => 'bucs-timeline.order.mailSentMessage',
  49.                     'type' => 'order',
  50.                     'entityId' => $order->getId(),
  51.                     'entityVersionId' => $order->getVersionId(),
  52.                     'referenceData' => [
  53.                         'order_id' => $order->getId(),
  54.                         'order_version_id' => $order->getVersionId(),
  55.                         'email_template_id' => $data['templateId'],
  56.                     ],
  57.                     'userId' => null
  58.                 ]
  59.             ];
  60.             $orderCustomer $order->getOrderCustomer();
  61.             if ($orderCustomer instanceof OrderCustomerEntity && $orderCustomer->getCustomer() instanceof CustomerEntity) {
  62.                 $this->preparedData[] = [
  63.                     'timelineType' => BucsTimelineCustomDefinition::TIMELINE_TYPE_CUSTOMER,
  64.                     'title' => 'bucs-timeline.order.mailSentTitle',
  65.                     'message' => 'bucs-timeline.order.mailSentMessage',
  66.                     'type' => 'customer',
  67.                     'entityId' => $orderCustomer->getCustomer()->getId(),
  68.                     'entityVersionId' => $orderCustomer->getCustomer()->getVersionId(),
  69.                     'referenceData' => [
  70.                         'email_template_id' => $data['templateId'],
  71.                         'order_id' => $order->getId(),
  72.                         'customer_id' => $orderCustomer->getCustomer()->getId(),
  73.                         'order_version_id' => $order->getVersionId(),
  74.                         'order_number' => $order->getOrderNumber()
  75.                     ],
  76.                     'userId' => null
  77.                 ];
  78.             }
  79.         }
  80.         if ($customer && $comment) {
  81.             $this->preparedData[] = [
  82.                 'timelineType' => BucsTimelineCustomDefinition::TIMELINE_TYPE_CUSTOMER,
  83.                 'title' => 'bucs-timeline.order.mailSentTitle',
  84.                 'message' => 'bucs-timeline.order.mailSentMessage',
  85.                 'type' => 'customer',
  86.                 'entityId' => $customer->getId(),
  87.                 'entityVersionId' => $customer->getVersionId(),
  88.                 'referenceData' => [
  89.                     'customer_id' => $customer->getId(),
  90.                     'email_template_id' => $data['templateId'],
  91.                 ],
  92.                 'userId' => null
  93.             ];
  94.         }
  95.     }
  96.     public function onMailSent(MailSentEvent $event)
  97.     {
  98.         foreach ($this->preparedData as &$data) {
  99.             $data['referenceData']['email_subject'] = $event->getSubject();
  100.             $data['referenceData']['email_recipient'] = array_key_first($event->getRecipients());
  101.             $emailHtml $event->getContents();
  102.             $data['referenceData']['email_html'] = \array_shift($emailHtml);
  103.         }
  104.         unset($data);
  105.         $this->customTimelineEntriesRepository->create(
  106.             $this->preparedData,
  107.             $event->getContext()
  108.         );
  109.         $this->preparedData = [];
  110.     }
  111. }