<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\EventSubscriber;
use App\Event\CommentCreatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Notifies post's author about new comments.
*
* @author Oleg Voronkovich <oleg-voronkovich@yandex.ru>
*/
class CommentNotificationSubscriber implements EventSubscriberInterface
{
public function __construct(
private MailerInterface $mailer,
private UrlGeneratorInterface $urlGenerator,
private TranslatorInterface $translator,
private string $sender
) {
}
public static function getSubscribedEvents(): array
{
return [
CommentCreatedEvent::class => 'onCommentCreated',
];
}
public function onCommentCreated(CommentCreatedEvent $event): void
{
$comment = $event->getComment();
$post = $comment->getPost();
$linkToPost = $this->urlGenerator->generate('blog_post', [
'slug' => $post->getSlug(),
'_fragment' => 'comment_'.$comment->getId(),
], UrlGeneratorInterface::ABSOLUTE_URL);
$subject = $this->translator->trans('notification.comment_created');
$body = $this->translator->trans('notification.comment_created.description', [
'title' => $post->getTitle(),
'link' => $linkToPost,
]);
// See https://symfony.com/doc/current/mailer.html
$email = (new Email())
->from($this->sender)
->to($post->getAuthor()->getEmail())
->subject($subject)
->html($body)
;
// In config/packages/dev/mailer.yaml the delivery of messages is disabled.
// That's why in the development environment you won't actually receive any email.
// However, you can inspect the contents of those unsent emails using the debug toolbar.
$this->mailer->send($email);
}
}