src/Aqarmap/Bundle/MainBundle/EventListener/BouncedEmailListener.php line 27

  1. <?php
  2. namespace Aqarmap\Bundle\MainBundle\EventListener;
  3. use Aqarmap\Bundle\MainBundle\Event\SNSNotificationsEvent;
  4. use Aqarmap\Bundle\NotificationBundle\Model\SESNotificationData;
  5. use Aqarmap\Bundle\UserBundle\Entity\User;
  6. use Aqarmap\Bundle\UserBundle\Services\UserManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. /**
  10. * Bounced Email Listener
  11. * This listener does some actions, such as, disabling user account, pause notifiers, ..etc.
  12. */
  13. class BouncedEmailListener implements EventSubscriberInterface
  14. {
  15. public function __construct(private readonly EntityManagerInterface $em, private readonly UserManager $userManager)
  16. {
  17. }
  18. public function disabledAccount(SNSNotificationsEvent $event): void
  19. {
  20. /** @var SESNotificationData $message */
  21. $message = $event->getMessage();
  22. if (preg_match('/bounce/i', (string) $message->getEventType())) {
  23. if (preg_match('/permanent/i', $message->getBounce()->getBounceType())) {
  24. foreach ($message->getBounce()->getBouncedRecipients() as $recipient) {
  25. $email = $recipient['emailAddress'] ?? null;
  26. if (!$email) {
  27. continue;
  28. }
  29. /** @var User $user */
  30. $user = $this->em->getRepository(User::class)->findOneBy(['emailCanonical' => $email]);
  31. if ($user) {
  32. $user->setHasEmail(false);
  33. $this->userManager->disable($user);
  34. }
  35. }
  36. }
  37. }
  38. }
  39. public function globalUnsubscribe(SNSNotificationsEvent $event): void
  40. {
  41. $message = $event->getMessage();
  42. foreach ($message->getMail()->getDestination() as $email) {
  43. /** @var User $user */
  44. $user = $this->em->getRepository(User::class)->findOneBy(['emailCanonical' => $email]);
  45. if ($user) {
  46. $this->userManager->globalUnsubscribe($user);
  47. }
  48. }
  49. }
  50. public static function getSubscribedEvents(): array
  51. {
  52. return [
  53. SNSNotificationsEvent::SNS_NOTIFICATION_BOUNCE => ['disabledAccount'],
  54. SNSNotificationsEvent::SNS_NOTIFICATION_COMPLAINT => ['globalUnsubscribe'],
  55. ];
  56. }
  57. }