1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NewsBundle\Mailer; |
15
|
|
|
|
16
|
|
|
use Sonata\NewsBundle\Model\BlogInterface; |
17
|
|
|
use Sonata\NewsBundle\Model\CommentInterface; |
18
|
|
|
use Sonata\NewsBundle\Util\HashGeneratorInterface; |
19
|
|
|
use Symfony\Component\Routing\RouterInterface; |
20
|
|
|
use Symfony\Component\Templating\EngineInterface; |
21
|
|
|
|
22
|
|
|
class Mailer implements MailerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var RouterInterface |
26
|
|
|
*/ |
27
|
|
|
protected $router; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EngineInterface |
31
|
|
|
*/ |
32
|
|
|
protected $templating; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $emails; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var HashGeneratorInterface |
41
|
|
|
*/ |
42
|
|
|
protected $hashGenerator; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \Swift_Mailer |
46
|
|
|
*/ |
47
|
|
|
protected $mailer; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var BlogInterface |
51
|
|
|
*/ |
52
|
|
|
protected $blog; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \Swift_Mailer $mailer |
56
|
|
|
*/ |
57
|
|
|
public function __construct($mailer, BlogInterface $blog, HashGeneratorInterface $generator, RouterInterface $router, EngineInterface $templating, array $emails) |
58
|
|
|
{ |
59
|
|
|
$this->blog = $blog; |
60
|
|
|
$this->mailer = $mailer; |
61
|
|
|
$this->hashGenerator = $generator; |
62
|
|
|
$this->router = $router; |
63
|
|
|
$this->templating = $templating; |
64
|
|
|
$this->emails = $emails; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function sendCommentNotification(CommentInterface $comment): void |
68
|
|
|
{ |
69
|
|
|
$rendered = $this->templating->render($this->emails['notification']['template'], [ |
70
|
|
|
'comment' => $comment, |
71
|
|
|
'post' => $comment->getPost(), |
72
|
|
|
'hash' => $this->hashGenerator->generate($comment), |
73
|
|
|
'blog' => $this->blog, |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
$this->sendEmailMessage( |
77
|
|
|
$rendered, |
78
|
|
|
$this->emails['notification']['from'], |
79
|
|
|
$this->emails['notification']['emails'] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $renderedTemplate |
85
|
|
|
* @param string $fromEmail |
86
|
|
|
* @param string $toEmail |
87
|
|
|
*/ |
88
|
|
|
protected function sendEmailMessage($renderedTemplate, $fromEmail, $toEmail): void |
89
|
|
|
{ |
90
|
|
|
// Render the email, use the first line as the subject, and the rest as the body |
91
|
|
|
list($subject, $body) = explode("\n", trim($renderedTemplate), 2); |
92
|
|
|
|
93
|
|
|
$message = $this->mailer->createMessage() |
94
|
|
|
->setSubject($subject) |
95
|
|
|
->setFrom($fromEmail) |
96
|
|
|
->setTo($toEmail) |
97
|
|
|
->setBody($body); |
98
|
|
|
|
99
|
|
|
$this->mailer->send($message); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|