src/EventSubscriber/CommentNotificationSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\EventSubscriber;
  11. use App\Entity\Comment;
  12. use App\Event\CommentCreatedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Email;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. /**
  19.  * Notifies post's author about new comments.
  20.  *
  21.  * @author Oleg Voronkovich <oleg-voronkovich@yandex.ru>
  22.  */
  23. class CommentNotificationSubscriber implements EventSubscriberInterface
  24. {
  25.     private $mailer;
  26.     private $translator;
  27.     private $urlGenerator;
  28.     private $sender;
  29.     public function __construct(MailerInterface $mailerUrlGeneratorInterface $urlGeneratorTranslatorInterface $translatorstring $sender)
  30.     {
  31.         $this->mailer $mailer;
  32.         $this->urlGenerator $urlGenerator;
  33.         $this->translator $translator;
  34.         $this->sender $sender;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             CommentCreatedEvent::class => 'onCommentCreated',
  40.         ];
  41.     }
  42.     public function onCommentCreated(CommentCreatedEvent $event): void
  43.     {
  44.         /** @var Comment $comment */
  45.         $comment $event->getComment();
  46.         $post $comment->getPost();
  47.         $linkToPost $this->urlGenerator->generate('blog_post', [
  48.             'slug' => $post->getSlug(),
  49.             '_fragment' => 'comment_'.$comment->getId(),
  50.         ], UrlGeneratorInterface::ABSOLUTE_URL);
  51.         $subject $this->translator->trans('notification.comment_created');
  52.         $body $this->translator->trans('notification.comment_created.description', [
  53.             '%title%' => $post->getTitle(),
  54.             '%link%' => $linkToPost,
  55.         ]);
  56.         // See https://symfony.com/doc/current/mailer.html
  57.         $email = (new Email())
  58.             ->from($this->sender)
  59.             ->to($post->getAuthor()->getEmail())
  60.             ->subject($subject)
  61.             ->html($body)
  62.         ;
  63.         // In config/packages/dev/mailer.yaml the delivery of messages is disabled.
  64.         // That's why in the development environment you won't actually receive any email.
  65.         // However, you can inspect the contents of those unsent emails using the debug toolbar.
  66.         $this->mailer->send($email);
  67.     }
  68. }