src/Subscriber/Messenger/MailSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Messenger;
  3. use App\Dictionary\ConversationMessageSource;
  4. use App\Dictionary\MailPriority;
  5. use App\Entity\ConversationMessage;
  6. use App\Entity\Member;
  7. use App\Event\Messenger\NewMessageEvent;
  8. use App\Service\Mail\MailService;
  9. use App\Service\Messenger\MessengerPropertyService;
  10. use App\Service\Property\MemberPropertyService as PropertyService;
  11. use App\Service\System\BounceService;
  12. use App\Service\User\AccountService;
  13. use App\Service\User\Layer\AmateurLayer;
  14. use App\Subscriber\AbstractMailSubscriber;
  15. use Frivol\Common\Dict\AccountStatus;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Twig\Environment;
  18. class MailSubscriber extends AbstractMailSubscriber implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         private AmateurLayer $amateurLayer,
  22.         private PropertyService $propertyService,
  23.         private BounceService $bounceService,
  24.         AccountService $accountService,
  25.         MailService $mailService,
  26.         Environment $twig,
  27.     ) {
  28.         parent::__construct($accountService$mailService$twig);
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             NewMessageEvent::class => [
  34.                 ['onNewMessage'0],
  35.             ],
  36.         ];
  37.     }
  38.     /**
  39.      * @throws \Twig\Error\LoaderError
  40.      * @throws \Twig\Error\RuntimeError
  41.      * @throws \Twig\Error\SyntaxError
  42.      */
  43.     public function onNewMessage(NewMessageEvent $event)
  44.     {
  45.         $message $event->getEntity();
  46.         $sender $event->getSender();
  47.         $recipient $message->getConversation()?->getPartner($sender);
  48.         if (null === $recipient
  49.             || $recipient->isMemberOnline()
  50.             || $this->isEmailNotificationDisabled($recipient)
  51.             || false === $recipient->getIsActive()
  52.             || false === in_array($recipient->getAccount()->getStatus(), [AccountStatus::ACTIVEAccountStatus::NEW], true)
  53.             || $this->bounceService->hasBounce($recipient->getAccount()->getEmail())
  54.         ) {
  55.             return;
  56.         }
  57.         $mail $this->createSwiftInstance($message$recipient$sender);
  58.         if (ConversationMessageSource::MASSMAILER === $message->getSource()) {
  59.             $priority MailPriority::MESSENGER_NEW_MESSAGE_MASSMAILER;
  60.             // gitlab #507: only send each third message as an email to reduce mail volume
  61.             if (=== random_int(16)) {
  62.                 $this->getMailService()->queueMessage($mail$priority);
  63.             }
  64.         } else {
  65.             $priority MailPriority::MESSENGER_NEW_MESSAGE;
  66.             $this->getMailService()->queueMessage($mail$priority);
  67.         }
  68.     }
  69.     /**
  70.      * Returns TRUE, if the user does NOT want email notifications when he or she is offline.
  71.      */
  72.     protected function isEmailNotificationDisabled(Member $recipient): bool
  73.     {
  74.         $prop MessengerPropertyService::PROPERTY_MESSENGER_EMAIL_NOTIFICATION_DISABLED;
  75.         return true === $this->propertyService->getValueForMember($recipient$prop);
  76.     }
  77.     protected function getSwiftMailSubject(Member $sender): string
  78.     {
  79.         return sprintf('Neue Nachricht von %s'$sender->getUsername());
  80.     }
  81.     /**
  82.      * @throws \Twig\Error\LoaderError
  83.      * @throws \Twig\Error\RuntimeError
  84.      * @throws \Twig\Error\SyntaxError
  85.      */
  86.     protected function createSwiftInstance(ConversationMessage $messageMember $recipientMember $sender): \Swift_Message
  87.     {
  88.         $account $recipient->getAccount();
  89.         $topAmateursPaginator $this->amateurLayer->getTopAmateurs(130truefalse);
  90.         $topAmateurs $topAmateursPaginator->getIterator()->getArrayCopy();
  91.         shuffle($topAmateurs);
  92.         $subject $this->getSwiftMailSubject($sender);
  93.         $mail = new \Swift_Message($subject);
  94.         $mail->addTo($account->getEmail());
  95.         $mail->setFrom('server@frivol.com''Frivol.com');
  96.         $mail->setBody($this->getTwigEnvironment()->render('emails/new-message-notification.html.twig', [
  97.             'message' => $message,
  98.             'subject' => $subject,
  99.             'sender' => $sender,
  100.             'amateurs' => array_slice($topAmateurs04),
  101.             'recipient' => $recipient,
  102.             'login' => $this->getAccountService()->getAutologinParameter($account),
  103.         ]), 'text/html');
  104.         return $mail;
  105.     }
  106. }