|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace AppBundle\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\EventDispatcher\GenericEvent; |
| 15 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 16 | +use Symfony\Component\Translation\TranslatorInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Notifies post's author about new comments. |
| 20 | + * |
| 21 | + * @author Oleg Voronkovich <[email protected]> |
| 22 | + */ |
| 23 | +class CommentNotificationListener |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var \Swift_Mailer |
| 27 | + */ |
| 28 | + private $mailer; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var TranslatorInterface |
| 32 | + */ |
| 33 | + private $translator; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var UrlGeneratorInterface |
| 37 | + */ |
| 38 | + private $urlGenerator; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + private $sender; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructor. |
| 47 | + * |
| 48 | + * @param \Swift_Mailer $mailer |
| 49 | + * @param UrlGeneratorInterface $urlGenerator |
| 50 | + * @param TranslatorInterface $translator |
| 51 | + * @param string $sender |
| 52 | + */ |
| 53 | + public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator, $sender) |
| 54 | + { |
| 55 | + $this->mailer = $mailer; |
| 56 | + $this->urlGenerator = $urlGenerator; |
| 57 | + $this->translator = $translator; |
| 58 | + $this->sender = $sender; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param GenericEvent $event |
| 63 | + */ |
| 64 | + public function onCommentCreated(GenericEvent $event) |
| 65 | + { |
| 66 | + $comment = $event->getSubject(); |
| 67 | + $post = $comment->getPost(); |
| 68 | + |
| 69 | + $linkToPost = $this->urlGenerator->generate('blog_post', [ |
| 70 | + 'slug' => $post->getSlug(), |
| 71 | + '_fragment' => 'comment_'.$comment->getId(), |
| 72 | + ], UrlGeneratorInterface::ABSOLUTE_URL); |
| 73 | + |
| 74 | + $subject = $this->translator->trans('notification.comment_created'); |
| 75 | + $body = $this->translator->trans('notification.comment_created.description', [ |
| 76 | + '%title%' => $post->getTitle(), |
| 77 | + '%link%' => $linkToPost, |
| 78 | + ]); |
| 79 | + |
| 80 | + // Symfony uses a library called SwiftMailer to send emails. That's why |
| 81 | + // email messages are created instantiating a Swift_Message class. |
| 82 | + // See http://symfony.com/doc/current/email.html#sending-emails |
| 83 | + $message = \Swift_Message::newInstance() |
| 84 | + ->setSubject($subject) |
| 85 | + ->setTo($post->getAuthorEmail()) |
| 86 | + ->setFrom($this->sender) |
| 87 | + ->setBody($body, 'text/html') |
| 88 | + ; |
| 89 | + |
| 90 | + // In app/config/config_dev.yml the 'disable_delivery' option is set to 'true'. |
| 91 | + // That's why in the development environment you won't actually receive any email. |
| 92 | + // However, you can inspect the contents of those unsent emails using the debug toolbar. |
| 93 | + // See http://symfony.com/doc/current/email/dev_environment.html#viewing-from-the-web-debug-toolbar |
| 94 | + $this->mailer->send($message); |
| 95 | + } |
| 96 | +} |
0 commit comments