src/Subscriber/UserGroups/UserGroupPostNotificationSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\UserGroups;
  3. use App\Dictionary\MailPriority;
  4. use App\Event\UserGroups\UserGroupPostCreatedEvent;
  5. use App\Event\UserGroups\UserGroupPostNotificationTransmitEvent;
  6. use App\Service\Mail\MailService;
  7. use App\Service\User\AccountService;
  8. use App\Service\UserGroups\PostNotificationService;
  9. use App\Subscriber\AbstractMailSubscriber;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Twig\Environment;
  12. class UserGroupPostNotificationSubscriber extends AbstractMailSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var PostNotificationService
  16.      */
  17.     protected $service;
  18.     /**
  19.      * UserGroupPostNotificationSubscriber constructor.
  20.      */
  21.     public function __construct(PostNotificationService $notificationServiceAccountService $accountService,
  22.         MailService $mailServiceEnvironment $twig)
  23.     {
  24.         parent::__construct($accountService$mailService$twig);
  25.         $this->service $notificationService;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             UserGroupPostCreatedEvent::class => [
  31.                 ['onGroupPostSendNotification'0],
  32.             ],
  33.             UserGroupPostNotificationTransmitEvent::class => [
  34.                 ['onGroupPostNotificationSendEmail'0],
  35.             ],
  36.         ];
  37.     }
  38.     public function onGroupPostSendNotification(UserGroupPostCreatedEvent $event)
  39.     {
  40.         $this->service->enqueuePostNotification($event->getEntity());
  41.     }
  42.     public function onGroupPostNotificationSendEmail(UserGroupPostNotificationTransmitEvent $event): bool
  43.     {
  44.         if (!$event->getSendEmail()) {
  45.             return false;
  46.         }
  47.         $post $event->getEntity();
  48.         $recipient $event->getRecipient();
  49.         $account $recipient->getAccount();
  50.         $group $post->getThread()->getUserGroup();
  51.         $mail = new \Swift_Message('Beitrag in Gruppe '.$group->getName());
  52.         $mail->addTo($account->getEmail());
  53.         $mail->setFrom('server@frivol.com''Frivol.com');
  54.         $mail->setBody($this->getTwigEnvironment()->render('emails/groups-new-post.html.twig', [
  55.             'member' => $recipient,
  56.             'author' => $post->getAuthor(),
  57.             'group' => $group,
  58.             'thread' => $post->getThread(),
  59.             'login' => $this->getAccountService()->getAutologinParameter($account),
  60.             'locale' => 'de',
  61.         ]), 'text/html');
  62.         return $this->getMailService()->queueMessage($mailMailPriority::GROUP_NEW_POST);
  63.     }
  64. }