<?php
declare(strict_types=1);
namespace Harmonizely\User\EventSubscriber;
use Harmonizely\Core\Email\ListContainer\EmailListContainer;
use Harmonizely\Entity\DeletionReasonEntity;
use Harmonizely\Repository\Contract\IDeletionReasonRepository;
use Harmonizely\User\UserDeletedEvent;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
class DeleteUserSubscriber implements EventSubscriberInterface
{
/**
* @var MailerInterface
*/
private MailerInterface $mailer;
/**
* @var EmailListContainer
*/
private EmailListContainer $emailListContainer;
/**
* @var IDeletionReasonRepository
*/
private IDeletionReasonRepository $deletionReasonRepository;
/**
* DeleteUserSubscriber constructor.
* @param MailerInterface $mailer
* @param EmailListContainer $emailListContainer
* @param IDeletionReasonRepository $deletionReasonRepository
*/
public function __construct(
MailerInterface $mailer,
EmailListContainer $emailListContainer,
IDeletionReasonRepository $deletionReasonRepository
)
{
$this->mailer = $mailer;
$this->emailListContainer = $emailListContainer;
$this->deletionReasonRepository = $deletionReasonRepository;
}
public static function getSubscribedEvents(): array
{
return [
UserDeletedEvent::NAME => 'onUserDeleted',
];
}
public function onUserDeleted(UserDeletedEvent $event): void
{
$user = $event->getUser();
$this->deletionReasonRepository->save(new DeletionReasonEntity(
$user->getEmail(),
$event->getReason(),
$event->isDeleted()
));
$email = (new TemplatedEmail())
->from(new Address(
$this->emailListContainer->get('notification')->getEmail(),
$this->emailListContainer->get('notification')->getName()
))
->to(new Address(
$this->emailListContainer->get('main')->getEmail(),
$this->emailListContainer->get('main')->getName()
))
->subject('Account has been deleted!')
->htmlTemplate('emails/user_deleted.html.twig')
->context([
'username' => $user->getFullName(),
'emailCanonical' => $user->getEmailCanonical(),
'reason' => $event->getReason(),
]);
$this->mailer->send($email);
}
}