<?php declare(strict_types=1);
namespace TTechEmailReplyTo\Subscriber;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mime\Address;
class ContactFormMailBeforeSentEventSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var EntityRepositoryInterface
*/
private $mailTemplateRepository;
private ?string $customerEmail = null;
private ?string $customerName = null;
public function __construct(
SystemConfigService $systemConfigService,
EntityRepositoryInterface $mailTemplateRepository
)
{
$this->systemConfigService = $systemConfigService;
$this->mailTemplateRepository = $mailTemplateRepository;
}
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'onBeforeMailValidateEvent',
MailBeforeSentEvent::class => 'onMailBeforeSentEvent',
];
}
public function onBeforeMailValidateEvent(MailBeforeValidateEvent $event) {
if(!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
return;
}
/** @var SalesChannelApiSource $source */
$source = $event->getContext()->getSource();
if($this->systemConfigService->get('TTechEmailReplyTo.config.setReplyToForContactForm', $source->getSalesChannelId()) !== true) {
return;
}
if(
!array_key_exists('templateId', $event->getData())
|| !$this->isCorrectMailTemplate($event->getData()['templateId'], $event->getContext())
) {
$this->customerEmail = null;
$this->customerName = null;
return;
}
$this->customerEmail = $event->getTemplateData()['contactFormData']['email'];
$this->customerName = $event->getTemplateData()['contactFormData']['firstName'] . ' ' . $event->getTemplateData()['contactFormData']['lastName'];
}
public function onMailBeforeSentEvent(MailBeforeSentEvent $event) {
if(!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
return;
}
/** @var SalesChannelApiSource $source */
$source = $event->getContext()->getSource();
if($this->systemConfigService->get('TTechEmailReplyTo.config.setReplyToForContactForm', $source->getSalesChannelId()) !== true) {
return;
}
if(
!array_key_exists('templateId', $event->getData())
|| !$this->isCorrectMailTemplate($event->getData()['templateId'], $event->getContext())
) {
return;
}
if($this->customerEmail === null) {
return;
}
$event->getMessage()->replyTo(new Address($this->customerEmail, $this->customerName));
}
private function isCorrectMailTemplate(string $templateId, Context $context): bool
{
$criteria = new Criteria();
$criteria->addAssociation('mailTemplateType');
$criteria->addFilter(
new EqualsFilter('id', $templateId),
new EqualsFilter('mailTemplateType.technicalName', 'contact_form')
);
$result = $this->mailTemplateRepository->search($criteria, $context);
if($result->getTotal() < 1) {
return false;
}
return true;
}
}