src/User/EventSubscriber/DeleteUserSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\User\EventSubscriber;
  4. use Harmonizely\Core\Email\ListContainer\EmailListContainer;
  5. use Harmonizely\Entity\DeletionReasonEntity;
  6. use Harmonizely\Repository\Contract\IDeletionReasonRepository;
  7. use Harmonizely\User\UserDeletedEvent;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\Mime\Address;
  12. class DeleteUserSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var MailerInterface
  16.      */
  17.     private MailerInterface $mailer;
  18.     /**
  19.      * @var EmailListContainer
  20.      */
  21.     private EmailListContainer $emailListContainer;
  22.     /**
  23.      * @var IDeletionReasonRepository
  24.      */
  25.     private IDeletionReasonRepository $deletionReasonRepository;
  26.     /**
  27.      * DeleteUserSubscriber constructor.
  28.      * @param MailerInterface $mailer
  29.      * @param EmailListContainer $emailListContainer
  30.      * @param IDeletionReasonRepository $deletionReasonRepository
  31.      */
  32.     public function __construct(
  33.         MailerInterface $mailer,
  34.         EmailListContainer $emailListContainer,
  35.         IDeletionReasonRepository $deletionReasonRepository
  36.     )
  37.     {
  38.         $this->mailer $mailer;
  39.         $this->emailListContainer $emailListContainer;
  40.         $this->deletionReasonRepository $deletionReasonRepository;
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             UserDeletedEvent::NAME => 'onUserDeleted',
  46.         ];
  47.     }
  48.     public function onUserDeleted(UserDeletedEvent $event): void
  49.     {
  50.         $user $event->getUser();
  51.         $this->deletionReasonRepository->save(new DeletionReasonEntity(
  52.             $user->getEmail(),
  53.             $event->getReason(),
  54.             $event->isDeleted()
  55.         ));
  56.         $email = (new TemplatedEmail())
  57.             ->from(new Address(
  58.                 $this->emailListContainer->get('notification')->getEmail(),
  59.                 $this->emailListContainer->get('notification')->getName()
  60.             ))
  61.             ->to(new Address(
  62.                 $this->emailListContainer->get('main')->getEmail(),
  63.                 $this->emailListContainer->get('main')->getName()
  64.             ))
  65.             ->subject('Account has been deleted!')
  66.             ->htmlTemplate('emails/user_deleted.html.twig')
  67.             ->context([
  68.                 'username' => $user->getFullName(),
  69.                 'emailCanonical' => $user->getEmailCanonical(),
  70.                 'reason' => $event->getReason(),
  71.             ]);
  72.         $this->mailer->send($email);
  73.     }
  74. }