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

Open in your IDE?
  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.     private EntityManagerInterface $em;
  16.     private UserManager $userManager;
  17.     public function __construct(EntityManagerInterface $emUserManager $userManager)
  18.     {
  19.         $this->em $em;
  20.         $this->userManager $userManager;
  21.     }
  22.     public function disabledAccount(SNSNotificationsEvent $event): void
  23.     {
  24.         /** @var SESNotificationData $message */
  25.         $message $event->getMessage();
  26.         if (preg_match('/bounce/i'$message->getEventType())) {
  27.             if (preg_match('/permanent/i'$message->getBounce()->getBounceType())) {
  28.                 foreach ($message->getMail()->getDestination() as $email) {
  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. }