<?php
declare(strict_types=1);
namespace NetInventors\NetiNextAccessManager\Subscriber;
use NetInventors\NetiNextAccessManager\Events\BusinessEvent\CustomerRegisteredOperatorEvent;
use NetInventors\NetiNextAccessManager\Service\Repository\CustomerMediaRepository;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MailSubscriber implements EventSubscriberInterface
{
private string $customerId = '';
private CustomerMediaRepository $customerUploadFilesRepository;
public function __construct(CustomerMediaRepository $customerUploadFilesRepository)
{
$this->customerUploadFilesRepository = $customerUploadFilesRepository;
}
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'onMailBeforeValidate',
CustomerRegisteredOperatorEvent::class => 'onOperatorMailEvent',
];
}
public function onOperatorMailEvent(CustomerRegisteredOperatorEvent $event): void
{
$this->customerId = $event->getCustomer()->getId();
}
public function onMailBeforeValidate(MailBeforeValidateEvent $event): void
{
if ('' === $this->customerId) {
return;
}
$files = $this->customerUploadFilesRepository->getUploadFilesIdsFromCustomer(
$this->customerId,
$event->getContext()
);
$event->addData('mediaIds', $files);
$this->customerId = '';
}
}