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