<?php
declare(strict_types=1);
namespace Wbfk\TimelineExtension\Subscriber;
use BucsItTimeline\Entity\BucsTimelineCustomDefinition;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
use Shopware\Core\Content\MailTemplate\Service\Event\MailSentEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Wbfk\Offer\Core\Entity\Offer\OfferEntity;
use Wbfk\Offer\Event\Flow\OfferEventApproved;
use Wbfk\Offer\Event\Flow\OfferEventApprovedInternal;
use Wbfk\Offer\Event\Flow\OfferEventOrdered;
use function array_shift;
/*
* Based on \BucsItTimeline\Subscriber\AddCustomTimelineEntriesSubscriber, for OfferEntity
*/
class AddCustomTimelineEntriesSubscriber implements EventSubscriberInterface
{
public const TIMELINE_TYPE_OFFER = 'wbfk_offer';
private array $preparedData = [];
public function __construct(
private readonly EntityRepository $customTimelineEntriesRepository,
private readonly RequestStack $requestStack
) {
}
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'onMailBeforeValidate',
MailSentEvent::class => 'onMailSent',
OfferEventApproved::class => 'onOfferEventApproved',
OfferEventApprovedInternal::class => 'onOfferEventApprovedInternal',
OfferEventOrdered::class => 'onOfferEventOrdered',
];
}
public function onMailBeforeValidate(MailBeforeValidateEvent $event): void
{
$templateData = $event->getTemplateData();
$data = $event->getData();
$offer = $templateData['offer'] ?? null;
if ($offer instanceof OfferEntity) {
$this->preparedData = [
[
'timelineType' => self::TIMELINE_TYPE_OFFER,
'title' => 'wbfk-timeline-extension.offer.mailSentTitle',
'message' => 'wbfk-timeline-extension.offer.mailSentMessage',
'type' => 'offer',
'entityId' => $offer->getId(),
'entityVersionId' => $offer->getVersionId(),
'referenceData' => [
'offer_id' => $offer->getId(),
'offer_version_id' => $offer->getVersionId(),
'email_template_id' => $data['templateId'],
],
'userId' => null,
],
];
$offerCustomer = $offer->getCustomer();
if ($offerCustomer) {
$this->preparedData[] = [
'timelineType' => BucsTimelineCustomDefinition::TIMELINE_TYPE_CUSTOMER,
'title' => 'wbfk-timeline-extension.offer.mailSentTitle',
'message' => 'wbfk-timeline-extension.offer.mailSentMessage',
'type' => 'customer',
'entityId' => $offerCustomer->getId(),
'entityVersionId' => $offerCustomer->getVersionId(),
'referenceData' => [
'email_template_id' => $data['templateId'],
'offer_id' => $offer->getId(),
'customer_id' => $offerCustomer->getId(),
'offer_version_id' => $offer->getVersionId(),
'offer_number' => $offer->getOfferNumber(),
],
'userId' => null,
];
}
}
// Customer and comment entry is already added from this listener: \BucsItTimeline\Subscriber\AddCustomTimelineEntriesSubscriber
}
public function onMailSent(MailSentEvent $event): void
{
/** @noinspection DuplicatedCode */
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 = [];
}
public function onOfferEventApproved(OfferEventApproved $approved): void
{
$offer = $approved->getOffer();
$this->preparedData[] = [
'timelineType' => self::TIMELINE_TYPE_OFFER,
'title' => 'wbfk-timeline-extension.offer.approvedTitle',
'message' => 'wbfk-timeline-extension.offer.approvedMessage',
'type' => 'offer',
'entityId' => $offer->getId(),
'entityVersionId' => $offer->getVersionId(),
'referenceData' => [
'offer_id' => $offer->getId(),
'customer_id' => $offer->getCustomerId(),
'offer_version_id' => $offer->getVersionId(),
'offer_number' => $offer->getOfferNumber(),
],
'userId' => $this->requestStack->getMainRequest()?->attributes->get('oauth_user_id'),
];
$this->customTimelineEntriesRepository->create(
$this->preparedData,
$approved->getContext()
);
$this->preparedData = [];
}
public function onOfferEventApprovedInternal(OfferEventApprovedInternal $approved): void
{
$offer = $approved->getOffer();
$this->preparedData[] = [
'timelineType' => self::TIMELINE_TYPE_OFFER,
'title' => 'wbfk-timeline-extension.offer.approvedInternalTitle',
'message' => 'wbfk-timeline-extension.offer.approvedInternalMessage',
'type' => 'offer',
'entityId' => $offer->getId(),
'entityVersionId' => $offer->getVersionId(),
'referenceData' => [
'offer_id' => $offer->getId(),
'customer_id' => $offer->getCustomerId(),
'offer_version_id' => $offer->getVersionId(),
'offer_number' => $offer->getOfferNumber(),
],
'userId' => $this->requestStack->getMainRequest()?->attributes->get('oauth_user_id'),
];
$this->customTimelineEntriesRepository->create(
$this->preparedData,
$approved->getContext()
);
$this->preparedData = [];
}
public function onOfferEventOrdered(OfferEventOrdered $ordered): void
{
$offer = $ordered->getOffer();
$this->preparedData[] = [
'timelineType' => self::TIMELINE_TYPE_OFFER,
'title' => 'wbfk-timeline-extension.offer.orderedTitle',
'message' => 'wbfk-timeline-extension.offer.orderedMessage',
'type' => 'offer',
'entityId' => $offer->getId(),
'entityVersionId' => $offer->getVersionId(),
'referenceData' => [
'offer_id' => $offer->getId(),
'customer_id' => $offer->getCustomerId(),
'offer_version_id' => $offer->getVersionId(),
'offer_number' => $offer->getOfferNumber(),
],
'userId' => null,
];
$this->customTimelineEntriesRepository->create(
$this->preparedData,
$ordered->getContext()
);
$this->preparedData = [];
}
}