Skip to content

Commit f87b37f

Browse files
committed
Use event listener and custom event trigger
1 parent 7b6ba9a commit f87b37f

File tree

4 files changed

+134
-32
lines changed

4 files changed

+134
-32
lines changed

app/config/services.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@ services:
3131
tags:
3232
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
3333

34-
# Event subscribers are similar to event listeners but they don't need to add
35-
# a separate tag for each listened event. Instead, the PHP class of the event
36-
# subscriber includes a method that returns the list of listened events.
34+
app.comment_notification:
35+
class: AppBundle\EventListener\CommentNotificationListener
36+
arguments: ['@mailer', '@router', '@translator', '%app.notifications.email_sender%']
37+
tags:
38+
- { name: kernel.event_listener, event: comment.created, method: onCommentCreated }
39+
40+
# Event subscribers are similar to event listeners but they don't need service tags.
41+
# Instead, the PHP class of the event subscriber includes a method that returns
42+
# the list of events listened by that class.
3743
# See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
3844
app.console_subscriber:
3945
class: AppBundle\EventListener\ConsoleEventSubscriber

src/AppBundle/Controller/BlogController.php

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313

1414
use AppBundle\Entity\Comment;
1515
use AppBundle\Entity\Post;
16+
use AppBundle\Events;
1617
use AppBundle\Form\CommentType;
1718
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
1819
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
1920
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
2021
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
2122
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
2223
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
24+
use Symfony\Component\EventDispatcher\GenericEvent;
2325
use Symfony\Component\HttpFoundation\Request;
2426
use Symfony\Component\HttpFoundation\Response;
25-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2627

2728
/**
2829
* Controller used to manage blog contents in the public part of the site.
@@ -97,7 +98,11 @@ public function commentNewAction(Request $request, Post $post)
9798
$entityManager->persist($comment);
9899
$entityManager->flush();
99100

100-
$this->notifyAuthorAboutNewComment($comment);
101+
// See http://symfony.com/doc/current/components/event_dispatcher/generic_event.html
102+
$event = new GenericEvent($comment);
103+
104+
// See http://symfony.com/doc/current/components/event_dispatcher.html
105+
$this->get('event_dispatcher')->dispatch(Events::COMMENT_CREATED, $event);
101106

102107
return $this->redirectToRoute('blog_post', ['slug' => $post->getSlug()]);
103108
}
@@ -108,33 +113,6 @@ public function commentNewAction(Request $request, Post $post)
108113
]);
109114
}
110115

111-
private function notifyAuthorAboutNewComment(Comment $comment)
112-
{
113-
$post = $comment->getPost();
114-
115-
$linkToPost = $this->generateUrl('blog_post', ['slug' => $post->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
116-
117-
$translator = $this->get('translator');
118-
119-
$subject = $translator->trans('post.recieved_comment');
120-
$body = $translator->trans('post.recieved_comment_message', [
121-
'%title%' => $post->getTitle(),
122-
'%link%' => $linkToPost.'#comment_'.$comment->getId(),
123-
]);
124-
125-
// See http://symfony.com/doc/current/email.html#sending-emails
126-
$message = \Swift_Message::newInstance()
127-
->setSubject($subject)
128-
->setTo($post->getAuthorEmail())
129-
->setFrom('symfony-demo@localhost')
130-
->setBody($body, 'text/html')
131-
;
132-
133-
// You can view a sent email in the debug toolbar
134-
// See http://symfony.com/doc/current/email/dev_environment.html#viewing-from-the-web-debug-toolbar
135-
$this->get('mailer')->send($message);
136-
}
137-
138116
/**
139117
* This controller is called directly via the render() function in the
140118
* blog/post_show.html.twig template. That's why it's not needed to define
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 <oleg-voronkovich@yandex.ru>
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|null $sender
52+
*/
53+
public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator, $sender = null)
54+
{
55+
$this->mailer = $mailer;
56+
$this->urlGenerator = $urlGenerator;
57+
$this->translator = $translator;
58+
$this->sender = null === $sender ? 'symfony-demo@localhost' : $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', ['slug' => $post->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
70+
71+
$subject = $this->translator->trans('post.recieved_comment');
72+
$body = $this->translator->trans('post.recieved_comment_message', [
73+
'%title%' => $post->getTitle(),
74+
'%link%' => $linkToPost.'#comment_'.$comment->getId(),
75+
]);
76+
77+
// See http://symfony.com/doc/current/email.html#sending-emails
78+
$message = \Swift_Message::newInstance()
79+
->setSubject($subject)
80+
->setTo($post->getAuthorEmail())
81+
->setFrom($this->sender)
82+
->setBody($body, 'text/html')
83+
;
84+
85+
// You can view a sent email in the debug toolbar
86+
// See http://symfony.com/doc/current/email/dev_environment.html#viewing-from-the-web-debug-toolbar
87+
$this->mailer->send($message);
88+
}
89+
}

src/AppBundle/Events.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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;
13+
14+
/**
15+
* Contains all events thrown in the Symfony demo application.
16+
*
17+
* @author Oleg Voronkovich <oleg-voronkovich@yandex.ru>
18+
*/
19+
final class Events
20+
{
21+
/**
22+
* See http://symfony.com/doc/current/components/event_dispatcher.html#naming-conventions.
23+
*
24+
* @Event("Symfony\Component\EventDispatcher\GenericEvent")
25+
*
26+
* @var string
27+
*/
28+
const COMMENT_CREATED = 'comment.created';
29+
}

0 commit comments

Comments
 (0)