1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Connector\Auth\Clients\Mandrill\Builders; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Slides\Connector\Auth\Clients\Mandrill\Mailer; |
8
|
|
|
use Slides\Connector\Auth\Clients\Mandrill\VariableResolver; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Email |
12
|
|
|
* |
13
|
|
|
* @package Slides\Connector\Auth\Clients\Mandrill\Builders |
14
|
|
|
*/ |
15
|
|
|
class Email |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Mailer |
19
|
|
|
*/ |
20
|
|
|
protected $mailer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The variable resolver. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $resolver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var VariableResolver |
31
|
|
|
*/ |
32
|
|
|
protected $resolverInstance; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The additional attributes that should be added to email. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $attributes = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The list of context variables. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $context = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $template; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $variables; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Collection |
60
|
|
|
*/ |
61
|
|
|
protected $recipients; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Email constructor. |
65
|
|
|
* |
66
|
|
|
* @param Mailer $mailer |
67
|
|
|
* @param string $resolver |
68
|
|
|
*/ |
69
|
|
|
public function __construct(Mailer $mailer, string $resolver = null) |
70
|
|
|
{ |
71
|
|
|
$this->mailer = $mailer; |
72
|
|
|
$this->recipients = new Collection(); |
73
|
|
|
|
74
|
|
|
$this->resolver = $resolver ?: config('connector.credentials.clients.mandrill.resolver'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retrieve recipients. |
79
|
|
|
* |
80
|
|
|
* @return Collection |
81
|
|
|
*/ |
82
|
|
|
public function getRecipients(): Collection |
83
|
|
|
{ |
84
|
|
|
return $this->recipients; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set the email template. |
89
|
|
|
* |
90
|
|
|
* @param string $template |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function template(string $template) |
95
|
|
|
{ |
96
|
|
|
$this->template = $template; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set additional variables. |
103
|
|
|
* |
104
|
|
|
* @param array $variables |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function variables(array $variables) |
109
|
|
|
{ |
110
|
|
|
$this->variables = $variables; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the message subject. |
117
|
|
|
* |
118
|
|
|
* @param string $subject |
119
|
|
|
* |
120
|
|
|
* @return static |
121
|
|
|
*/ |
122
|
|
|
public function subject(string $subject) |
123
|
|
|
{ |
124
|
|
|
$this->attributes['subject'] = $subject; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set the sender email address and name. |
131
|
|
|
* |
132
|
|
|
* @param string $email |
133
|
|
|
* @param string $name |
134
|
|
|
* |
135
|
|
|
* @return static |
136
|
|
|
*/ |
137
|
|
|
public function from(string $email, ?string $name = null) |
138
|
|
|
{ |
139
|
|
|
$this->attributes['from'] = compact('email', 'name'); |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the email recipients. |
146
|
|
|
* |
147
|
|
|
* @param mixed $recipients |
148
|
|
|
* |
149
|
|
|
* @return static |
150
|
|
|
*/ |
151
|
|
|
public function recipients($recipients) |
152
|
|
|
{ |
153
|
|
|
$this->recipients = collect($recipients); |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set email context. |
160
|
|
|
* |
161
|
|
|
* @param array $context |
162
|
|
|
* |
163
|
|
|
* @return static |
164
|
|
|
*/ |
165
|
|
|
public function context(array $context) |
166
|
|
|
{ |
167
|
|
|
$this->context = $context; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set message tags. |
174
|
|
|
* |
175
|
|
|
* @param array $tags |
176
|
|
|
* |
177
|
|
|
* @return static |
178
|
|
|
*/ |
179
|
|
|
public function tags(array $tags) |
180
|
|
|
{ |
181
|
|
|
$this->attributes['tags'] = $tags; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Send the email. |
188
|
|
|
* |
189
|
|
|
* @param Collection $recipients |
190
|
|
|
* |
191
|
|
|
* @return mixed |
192
|
|
|
*/ |
193
|
|
|
public function send(Collection $recipients = null) |
194
|
|
|
{ |
195
|
|
|
if (is_null($recipients)) { |
196
|
|
|
$recipients = $this->recipients; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this->mailer->send($this->build($recipients)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Chunk recipients before sending. |
204
|
|
|
* |
205
|
|
|
* @param int $size |
206
|
|
|
* |
207
|
|
|
* @return \Generator |
208
|
|
|
*/ |
209
|
|
|
public function sendChunk(int $size = 1000) |
210
|
|
|
{ |
211
|
|
|
foreach ($this->recipients->chunk($size) as $chunk) { |
212
|
|
|
yield $this->send($chunk); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Chunk the recipients. |
218
|
|
|
* |
219
|
|
|
* @param int $size |
220
|
|
|
* |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
protected function chunk(int $size = 1000): array |
224
|
|
|
{ |
225
|
|
|
$chunks = $this->recipients->chunk($size); |
226
|
|
|
|
227
|
|
|
return $chunks->map([$this, 'build'])->toArray(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Build the email to be sent. |
232
|
|
|
* |
233
|
|
|
* @param Collection|null $recipients |
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
protected function build(Collection $recipients): array |
238
|
|
|
{ |
239
|
|
|
return array_merge([ |
240
|
|
|
'template' => $this->template, |
241
|
|
|
'recipients' => $this->buildRecipients($recipients), |
242
|
|
|
'variables' => $this->buildVariables($recipients), |
243
|
|
|
], $this->attributes); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Build the message recipients. |
248
|
|
|
* |
249
|
|
|
* @param Collection $recipients |
250
|
|
|
* |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
|
|
protected function buildRecipients(Collection $recipients): array |
254
|
|
|
{ |
255
|
|
|
return $recipients->map(function (string $email) { |
256
|
|
|
return [ |
257
|
|
|
'email' => $email, |
258
|
|
|
'type' => 'to' |
259
|
|
|
]; |
260
|
|
|
})->toArray(); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Build the variables for given collection of recipients. |
265
|
|
|
* |
266
|
|
|
* @param Collection $recipients |
267
|
|
|
* |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
|
|
protected function buildVariables(Collection $recipients): array |
271
|
|
|
{ |
272
|
|
|
return $recipients->map(function (string $email) { |
273
|
|
|
return [ |
274
|
|
|
'rcpt' => $email, |
275
|
|
|
'vars' => $this->userVariables($email) |
276
|
|
|
]; |
277
|
|
|
})->toArray(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Build variable for a user. |
282
|
|
|
* |
283
|
|
|
* @param string $email |
284
|
|
|
* |
285
|
|
|
* @return array |
286
|
|
|
* |
287
|
|
|
* @throws Exception |
288
|
|
|
*/ |
289
|
|
|
protected function userVariables(string $email): array |
290
|
|
|
{ |
291
|
|
|
if (!$this->resolverInstance) { |
292
|
|
|
$this->resolverInstance = new $this->resolver($this->recipients, $this->context); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return array_map(function ($variable) use ($email) { |
296
|
|
|
return $this->resolverInstance->resolve($variable, $email); |
297
|
|
|
}, $this->variables); |
298
|
|
|
} |
299
|
|
|
} |