-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailer.php
182 lines (151 loc) · 6 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Brouzie\Mailer;
use Brouzie\Mailer\Container\SimpleServiceLocator;
use Brouzie\Mailer\Exception\InvalidArgumentException;
use Brouzie\Mailer\Exception\PredefinedEmailNotFoundException;
use Brouzie\Mailer\Exception\RendererNotFoundException;
use Brouzie\Mailer\Exception\TransportNotFoundException;
use Brouzie\Mailer\Model\Address;
use Brouzie\Mailer\Model\Email;
use Brouzie\Mailer\Model\PredefinedEmail;
use Brouzie\Mailer\Renderer\Renderer;
use Brouzie\Mailer\Transport\Transport;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
class Mailer
{
private $renderer;
/**
* @var ContainerInterface
*/
private $transports;
private $defaultTransport;
private $defaultSender;
/**
* @var ContainerInterface
*/
private $predefinedEmails;
private $defaultContext;
private $defaultHeaders;
/**
* @param Renderer $renderer
* @param array|Transport[]|ContainerInterface $transports
* @param string $defaultTransport
* @param Address $defaultSender
* @param array|PredefinedEmail[]|ContainerInterface $predefinedEmails
* @param array $defaultContext
* @param array $defaultHeaders
*/
public function __construct(
Renderer $renderer,
$transports,
string $defaultTransport = 'default',
Address $defaultSender,
$predefinedEmails = [],
array $defaultContext = [],
array $defaultHeaders = []
) {
$this->renderer = $renderer;
if ($transports instanceof ContainerInterface) {
$this->transports = $transports;
} elseif (is_array($transports)) {
$this->transports = new SimpleServiceLocator($transports);
} else {
throw new InvalidArgumentException(
'Expected an array of transports or "Psr\Container\ContainerInterface" instance.'
);
}
$this->defaultTransport = $defaultTransport;
if (!$this->transports->has($defaultTransport)) {
throw new TransportNotFoundException(sprintf('Missing default transport "%s".', $defaultTransport));
}
$this->defaultSender = $defaultSender;
if ($predefinedEmails instanceof ContainerInterface) {
$this->predefinedEmails = $predefinedEmails;
} elseif (is_array($predefinedEmails)) {
$this->predefinedEmails = new SimpleServiceLocator($predefinedEmails);
} else {
throw new InvalidArgumentException(
'Expected an array of predefined emails or "Psr\Container\ContainerInterface" instance.'
);
}
$this->defaultContext = $defaultContext;
$this->defaultHeaders = $defaultHeaders;
}
public function sendEmail(Email $email, array $context = [], $transport = null)
{
//TODO: add events?
$context = array_replace($this->defaultContext, $context, ['_email' => $email]);
$email->addHeaders($this->defaultHeaders);
//TODO: add support of already rendered emails?
if (!$this->renderer->supports($email)) {
throw new RendererNotFoundException(sprintf('No renderer found for email of type "%s".', get_class($email)));
}
$this->renderer->render($email, $context);
if (null === $email->getSender()) {
$email->setSender($this->defaultSender);
}
$this->getTransport($transport)->send($email);
}
/**
* @param $name
* @param string|iterable|Address|Address[] $recipients
* @param array $context
* @param callable|null $configurator
*/
public function sendNamedEmail(string $name, $recipients, array $context = [], callable $configurator = null)
{
/** @var PredefinedEmail $predefinedEmail */
try {
$predefinedEmail = $this->predefinedEmails->get($name);
} catch (NotFoundExceptionInterface $e) {
$availableEmails = method_exists($e, 'getAlternatives') ? $e->getAlternatives() : [];
throw PredefinedEmailNotFoundException::create($name, $availableEmails, $e);
}
$context = array_replace($predefinedEmail->getDefaultContext(), $context, ['_email_name' => $name]);
$predefinedEmail->validateContext($context);
$email = $predefinedEmail->getEmail();
$email->addRecipients(self::normalizeRecipients($recipients));
if (null !== $configurator) {
$configurator($email, $context);
}
$this->sendEmail($email, $context, $predefinedEmail->getTransport());
}
/**
* @param string|iterable|Address|Address[] $recipients
*
* @return Address[]
*/
public static function normalizeRecipients($recipients): array
{
$targetRecipients = [];
if (is_string($recipients)) {
$targetRecipients[] = new Address($recipients);
} elseif ($recipients instanceof Address) {
$targetRecipients[] = $recipients;
} elseif (is_iterable($recipients)) {
foreach ($recipients as $key => $recipient) {
if ($recipient instanceof Address) {
$targetRecipients[] = $recipient;
} elseif (is_int($key)) {
$targetRecipients[] = new Address($recipient);
} else {
$targetRecipients[] = new Address($key, $recipient);
}
}
} else {
throw new InvalidArgumentException('Expected string/array/Address instance/array of Address instances.');
}
return $targetRecipients;
}
private function getTransport($transport): Transport
{
$transport = $transport ?: $this->defaultTransport;
try {
return $this->transports->get($transport);
} catch (NotFoundExceptionInterface $e) {
$availableTransports = method_exists($e, 'getAlternatives') ? $e->getAlternatives() : [];
throw TransportNotFoundException::create($transport, $availableTransports, $e);
}
}
}