custom/plugins/WbfkTimelineExtension/src/Subscriber/AddCustomTimelineEntriesSubscriber.php line 136

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\TimelineExtension\Subscriber;
  4. use BucsItTimeline\Entity\BucsTimelineCustomDefinition;
  5. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  6. use Shopware\Core\Content\MailTemplate\Service\Event\MailSentEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Wbfk\Offer\Core\Entity\Offer\OfferEntity;
  11. use Wbfk\Offer\Event\Flow\OfferEventApproved;
  12. use Wbfk\Offer\Event\Flow\OfferEventApprovedInternal;
  13. use Wbfk\Offer\Event\Flow\OfferEventOrdered;
  14. use function array_shift;
  15. /*
  16.  * Based on \BucsItTimeline\Subscriber\AddCustomTimelineEntriesSubscriber, for OfferEntity
  17.  */
  18. class AddCustomTimelineEntriesSubscriber implements EventSubscriberInterface
  19. {
  20.     public const TIMELINE_TYPE_OFFER 'wbfk_offer';
  21.     private array $preparedData = [];
  22.     public function __construct(
  23.         private readonly EntityRepository $customTimelineEntriesRepository,
  24.         private readonly RequestStack $requestStack
  25.     ) {
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             MailBeforeValidateEvent::class => 'onMailBeforeValidate',
  31.             MailSentEvent::class => 'onMailSent',
  32.             OfferEventApproved::class => 'onOfferEventApproved',
  33.             OfferEventApprovedInternal::class => 'onOfferEventApprovedInternal',
  34.             OfferEventOrdered::class => 'onOfferEventOrdered',
  35.         ];
  36.     }
  37.     public function onMailBeforeValidate(MailBeforeValidateEvent $event): void
  38.     {
  39.         $templateData $event->getTemplateData();
  40.         $data $event->getData();
  41.         $offer $templateData['offer'] ?? null;
  42.         if ($offer instanceof OfferEntity) {
  43.             $this->preparedData = [
  44.                 [
  45.                     'timelineType' => self::TIMELINE_TYPE_OFFER,
  46.                     'title' => 'wbfk-timeline-extension.offer.mailSentTitle',
  47.                     'message' => 'wbfk-timeline-extension.offer.mailSentMessage',
  48.                     'type' => 'offer',
  49.                     'entityId' => $offer->getId(),
  50.                     'entityVersionId' => $offer->getVersionId(),
  51.                     'referenceData' => [
  52.                         'offer_id' => $offer->getId(),
  53.                         'offer_version_id' => $offer->getVersionId(),
  54.                         'email_template_id' => $data['templateId'],
  55.                     ],
  56.                     'userId' => null,
  57.                 ],
  58.             ];
  59.             $offerCustomer $offer->getCustomer();
  60.             if ($offerCustomer) {
  61.                 $this->preparedData[] = [
  62.                     'timelineType' => BucsTimelineCustomDefinition::TIMELINE_TYPE_CUSTOMER,
  63.                     'title' => 'wbfk-timeline-extension.offer.mailSentTitle',
  64.                     'message' => 'wbfk-timeline-extension.offer.mailSentMessage',
  65.                     'type' => 'customer',
  66.                     'entityId' => $offerCustomer->getId(),
  67.                     'entityVersionId' => $offerCustomer->getVersionId(),
  68.                     'referenceData' => [
  69.                         'email_template_id' => $data['templateId'],
  70.                         'offer_id' => $offer->getId(),
  71.                         'customer_id' => $offerCustomer->getId(),
  72.                         'offer_version_id' => $offer->getVersionId(),
  73.                         'offer_number' => $offer->getOfferNumber(),
  74.                     ],
  75.                     'userId' => null,
  76.                 ];
  77.             }
  78.         }
  79.         // Customer and comment entry is already added from this listener: \BucsItTimeline\Subscriber\AddCustomTimelineEntriesSubscriber
  80.     }
  81.     public function onMailSent(MailSentEvent $event): void
  82.     {
  83.         /** @noinspection DuplicatedCode */
  84.         foreach ($this->preparedData as &$data) {
  85.             $data['referenceData']['email_subject'] = $event->getSubject();
  86.             $data['referenceData']['email_recipient'] = array_key_first($event->getRecipients());
  87.             $emailHtml $event->getContents();
  88.             $data['referenceData']['email_html'] = array_shift($emailHtml);
  89.         }
  90.         unset($data);
  91.         $this->customTimelineEntriesRepository->create(
  92.             $this->preparedData,
  93.             $event->getContext()
  94.         );
  95.         $this->preparedData = [];
  96.     }
  97.     public function onOfferEventApproved(OfferEventApproved $approved): void
  98.     {
  99.         $offer $approved->getOffer();
  100.         $this->preparedData[] = [
  101.             'timelineType' => self::TIMELINE_TYPE_OFFER,
  102.             'title' => 'wbfk-timeline-extension.offer.approvedTitle',
  103.             'message' => 'wbfk-timeline-extension.offer.approvedMessage',
  104.             'type' => 'offer',
  105.             'entityId' => $offer->getId(),
  106.             'entityVersionId' => $offer->getVersionId(),
  107.             'referenceData' => [
  108.                 'offer_id' => $offer->getId(),
  109.                 'customer_id' => $offer->getCustomerId(),
  110.                 'offer_version_id' => $offer->getVersionId(),
  111.                 'offer_number' => $offer->getOfferNumber(),
  112.             ],
  113.             'userId' => $this->requestStack->getMainRequest()?->attributes->get('oauth_user_id'),
  114.         ];
  115.         $this->customTimelineEntriesRepository->create(
  116.             $this->preparedData,
  117.             $approved->getContext()
  118.         );
  119.         $this->preparedData = [];
  120.     }
  121.     public function onOfferEventApprovedInternal(OfferEventApprovedInternal $approved): void
  122.     {
  123.         $offer $approved->getOffer();
  124.         $this->preparedData[] = [
  125.             'timelineType' => self::TIMELINE_TYPE_OFFER,
  126.             'title' => 'wbfk-timeline-extension.offer.approvedInternalTitle',
  127.             'message' => 'wbfk-timeline-extension.offer.approvedInternalMessage',
  128.             'type' => 'offer',
  129.             'entityId' => $offer->getId(),
  130.             'entityVersionId' => $offer->getVersionId(),
  131.             'referenceData' => [
  132.                 'offer_id' => $offer->getId(),
  133.                 'customer_id' => $offer->getCustomerId(),
  134.                 'offer_version_id' => $offer->getVersionId(),
  135.                 'offer_number' => $offer->getOfferNumber(),
  136.             ],
  137.             'userId' => $this->requestStack->getMainRequest()?->attributes->get('oauth_user_id'),
  138.         ];
  139.         $this->customTimelineEntriesRepository->create(
  140.             $this->preparedData,
  141.             $approved->getContext()
  142.         );
  143.         $this->preparedData = [];
  144.     }
  145.     public function onOfferEventOrdered(OfferEventOrdered $ordered): void
  146.     {
  147.         $offer $ordered->getOffer();
  148.         $this->preparedData[] = [
  149.             'timelineType' => self::TIMELINE_TYPE_OFFER,
  150.             'title' => 'wbfk-timeline-extension.offer.orderedTitle',
  151.             'message' => 'wbfk-timeline-extension.offer.orderedMessage',
  152.             'type' => 'offer',
  153.             'entityId' => $offer->getId(),
  154.             'entityVersionId' => $offer->getVersionId(),
  155.             'referenceData' => [
  156.                 'offer_id' => $offer->getId(),
  157.                 'customer_id' => $offer->getCustomerId(),
  158.                 'offer_version_id' => $offer->getVersionId(),
  159.                 'offer_number' => $offer->getOfferNumber(),
  160.             ],
  161.             'userId' => null,
  162.         ];
  163.         $this->customTimelineEntriesRepository->create(
  164.             $this->preparedData,
  165.             $ordered->getContext()
  166.         );
  167.         $this->preparedData = [];
  168.     }
  169. }