src/EventSubscriber/CommentNotificationSubscriber.php line 48

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