src/EventSubscriber/CommentNotificationSubscriber.php line 47

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\Events\CommentCreatedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. /**
  17.  * Notifies post's author about new comments.
  18.  *
  19.  * @author Oleg Voronkovich <oleg-voronkovich@yandex.ru>
  20.  */
  21. class CommentNotificationSubscriber implements EventSubscriberInterface
  22. {
  23.     private $mailer;
  24.     private $translator;
  25.     private $urlGenerator;
  26.     private $sender;
  27.     public function __construct(\Swift_Mailer $mailerUrlGeneratorInterface $urlGeneratorTranslatorInterface $translator$sender)
  28.     {
  29.         $this->mailer $mailer;
  30.         $this->urlGenerator $urlGenerator;
  31.         $this->translator $translator;
  32.         $this->sender $sender;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             CommentCreatedEvent::class => 'onCommentCreated',
  38.         ];
  39.     }
  40.     public function onCommentCreated(CommentCreatedEvent $event): void
  41.     {
  42.         /** @var Comment $comment */
  43.         $comment $event->getComment();
  44.         $post $comment->getPost();
  45.         $linkToPost $this->urlGenerator->generate('blog_post', [
  46.             'slug' => $post->getSlug(),
  47.             '_fragment' => 'comment_'.$comment->getId(),
  48.         ], UrlGeneratorInterface::ABSOLUTE_URL);
  49.         $subject $this->translator->trans('notification.comment_created');
  50.         $body $this->translator->trans('notification.comment_created.description', [
  51.             '%title%' => $post->getTitle(),
  52.             '%link%' => $linkToPost,
  53.         ]);
  54.         // Symfony uses a library called SwiftMailer to send emails. That's why
  55.         // email messages are created instantiating a Swift_Message class.
  56.         // See https://symfony.com/doc/current/email.html#sending-emails
  57.         $message = (new \Swift_Message())
  58.             ->setSubject($subject)
  59.             ->setTo($post->getAuthor()->getEmail())
  60.             ->setFrom($this->sender)
  61.             ->setBody($body'text/html')
  62.         ;
  63.         // In config/packages/dev/swiftmailer.yaml the 'disable_delivery' option is set to 'true'.
  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.         // See https://symfony.com/doc/current/email/dev_environment.html#viewing-from-the-web-debug-toolbar
  67.         $this->mailer->send($message);
  68.     }
  69. }