1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Core\Port\Notification\Client\Email; |
16
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\TemplateEngine\EmailTemplateViewModelInterface; |
18
|
|
|
use Acme\App\Core\Port\TemplateEngine\TemplateEngineInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Akis Krimpenis |
22
|
|
|
* @author Alexandre Eher |
23
|
|
|
* @author Henry Snoek |
24
|
|
|
* @author Henrique Moody |
25
|
|
|
* @author Herberto Graca <[email protected]> |
26
|
|
|
* @author Jeroen Van Den Heuvel |
27
|
|
|
* @author Marijn Koesen |
28
|
|
|
* @author Nicolae Nichifor |
29
|
|
|
* @author Ruud Van Der Weiijde |
30
|
|
|
* @author Vinicius Andrade |
31
|
|
|
*/ |
32
|
|
|
final class EmailGenerator |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var TemplateEngineInterface |
36
|
|
|
*/ |
37
|
|
|
private $templateEngine; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var EmailAddress |
41
|
|
|
*/ |
42
|
|
|
private $defaultFromEmailAddress; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
TemplateEngineInterface $templateEngine, |
46
|
|
|
string $defaultFromEmailAddress, |
47
|
|
|
string $defaultFromEmailName |
48
|
|
|
) { |
49
|
|
|
$this->templateEngine = $templateEngine; |
50
|
|
|
$this->defaultFromEmailAddress = new EmailAddress($defaultFromEmailAddress, $defaultFromEmailName); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create the Email Message that we can send. |
55
|
|
|
* |
56
|
|
|
* This is generated from two (twig) templates and an array with template data |
57
|
|
|
* |
58
|
|
|
* @param EmailAddress $fromEmailAddress The sender of the email |
59
|
|
|
* @param EmailAddress $recipient The recipient of the email |
60
|
|
|
* @param string $subject The subject of the email |
61
|
|
|
* @param string $txtTemplatePath The path to the txt template e.g. |
62
|
|
|
* mails/toConsumer/notify-consumer-new-proposal.txt.twig |
63
|
|
|
* @param string $htmlTemplatePath The path to the txt template e.g. |
64
|
|
|
* mails/toConsumer/notify-consumer-new-proposal.html.twig |
65
|
|
|
* @param EmailTemplateViewModelInterface $emailTemplateViewModel The data that will be used to render the template |
66
|
|
|
* @param EmailAddress[] $cc |
67
|
|
|
* @param EmailAddress[] $bcc |
68
|
|
|
* @param string[] $tagList Can be used for tracking email campaigns |
69
|
|
|
*/ |
70
|
|
|
public function generateEmailMessage( |
71
|
|
|
EmailAddress $recipient, |
72
|
|
|
string $subject, |
73
|
|
|
string $txtTemplatePath, |
74
|
|
|
string $htmlTemplatePath, |
75
|
|
|
EmailTemplateViewModelInterface $emailTemplateViewModel, |
76
|
|
|
EmailAddress $fromEmailAddress = null, |
77
|
|
|
array $cc = [], |
78
|
|
|
array $bcc = [], |
79
|
|
|
array $tagList = [] |
80
|
|
|
): Email { |
81
|
|
|
$email = new Email($subject, $fromEmailAddress ?? $this->defaultFromEmailAddress); |
82
|
|
|
$email->addTo($recipient); |
83
|
|
|
|
84
|
|
|
foreach ($cc as $address) { |
85
|
|
|
$email->addCc($address); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach ($bcc as $address) { |
89
|
|
|
$email->addBcc($address); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$email->setBodyText($this->templateEngine->render($txtTemplatePath, $emailTemplateViewModel)); |
93
|
|
|
$email->setBodyHtml($this->templateEngine->render($htmlTemplatePath, $emailTemplateViewModel)); |
94
|
|
|
$email->setTrackMessageOpening(true); |
95
|
|
|
$email->setTrackClicks(true); |
96
|
|
|
$email->setTags($tagList); |
97
|
|
|
|
98
|
|
|
return $email; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|