<?php
declare(strict_types=1);
/*
* Copyright (C) web fabric gmbh - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Dominik Mank <entwicklung@web-fabric.de>, December 2020
*/
namespace BucsItTimeline\Subscriber;
use BucsItTimeline\Entity\BucsTimelineCustomDefinition;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
use Shopware\Core\Content\MailTemplate\Service\Event\MailSentEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AddCustomTimelineEntriesSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $customTimelineEntriesRepository;
private $preparedData = [];
public function __construct(EntityRepositoryInterface $customTimelineEntriesRepository)
{
$this->customTimelineEntriesRepository = $customTimelineEntriesRepository;
}
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'onMailBeforeValidate',
MailSentEvent::class => 'onMailSent'
];
}
public function onMailBeforeValidate(MailBeforeValidateEvent $event)
{
$templateData = $event->getTemplateData();
$data = $event->getData();
$order = $templateData['order'] ?? null;
$customer = $templateData['customer'] ?? null;
$comment = $templateData['BucsTimelineComment'] ?? null;
if ($order instanceof OrderEntity) {
$this->preparedData = [
[
'timelineType' => BucsTimelineCustomDefinition::TIMELINE_TYPE_ORDER,
'title' => 'bucs-timeline.order.mailSentTitle',
'message' => 'bucs-timeline.order.mailSentMessage',
'type' => 'order',
'entityId' => $order->getId(),
'entityVersionId' => $order->getVersionId(),
'referenceData' => [
'order_id' => $order->getId(),
'order_version_id' => $order->getVersionId(),
'email_template_id' => $data['templateId'],
],
'userId' => null
]
];
$orderCustomer = $order->getOrderCustomer();
if ($orderCustomer instanceof OrderCustomerEntity && $orderCustomer->getCustomer() instanceof CustomerEntity) {
$this->preparedData[] = [
'timelineType' => BucsTimelineCustomDefinition::TIMELINE_TYPE_CUSTOMER,
'title' => 'bucs-timeline.order.mailSentTitle',
'message' => 'bucs-timeline.order.mailSentMessage',
'type' => 'customer',
'entityId' => $orderCustomer->getCustomer()->getId(),
'entityVersionId' => $orderCustomer->getCustomer()->getVersionId(),
'referenceData' => [
'email_template_id' => $data['templateId'],
'order_id' => $order->getId(),
'customer_id' => $orderCustomer->getCustomer()->getId(),
'order_version_id' => $order->getVersionId(),
'order_number' => $order->getOrderNumber()
],
'userId' => null
];
}
}
if ($customer && $comment) {
$this->preparedData[] = [
'timelineType' => BucsTimelineCustomDefinition::TIMELINE_TYPE_CUSTOMER,
'title' => 'bucs-timeline.order.mailSentTitle',
'message' => 'bucs-timeline.order.mailSentMessage',
'type' => 'customer',
'entityId' => $customer->getId(),
'entityVersionId' => $customer->getVersionId(),
'referenceData' => [
'customer_id' => $customer->getId(),
'email_template_id' => $data['templateId'],
],
'userId' => null
];
}
}
public function onMailSent(MailSentEvent $event)
{
foreach ($this->preparedData as &$data) {
$data['referenceData']['email_subject'] = $event->getSubject();
$data['referenceData']['email_recipient'] = array_key_first($event->getRecipients());
$emailHtml = $event->getContents();
$data['referenceData']['email_html'] = \array_shift($emailHtml);
}
unset($data);
$this->customTimelineEntriesRepository->create(
$this->preparedData,
$event->getContext()
);
$this->preparedData = [];
}
}