1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Email; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use Exception; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
// From 'charcoal-core' |
11
|
|
|
use Charcoal\Model\AbstractModel; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Email log |
15
|
|
|
*/ |
16
|
|
|
class EmailLog extends AbstractModel |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
use EmailAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The Queue ID |
22
|
|
|
* |
23
|
|
|
* @var string $queueId |
24
|
|
|
*/ |
25
|
|
|
private $queueId; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The error code |
29
|
|
|
* |
30
|
|
|
* @var string $errorCode |
31
|
|
|
*/ |
32
|
|
|
private $errorCode; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The Message-ID (Unique message identifier) |
36
|
|
|
* |
37
|
|
|
* @var string $messageId |
38
|
|
|
*/ |
39
|
|
|
private $messageId; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The campaign ID. |
43
|
|
|
* |
44
|
|
|
* @var string $campaign |
45
|
|
|
*/ |
46
|
|
|
private $campaign; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The sender's email address. |
50
|
|
|
* |
51
|
|
|
* @var string $from |
52
|
|
|
*/ |
53
|
|
|
private $from; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The recipient's email address. |
57
|
|
|
* |
58
|
|
|
* @var string $to |
59
|
|
|
*/ |
60
|
|
|
private $to; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The email subject. |
64
|
|
|
* |
65
|
|
|
* @var string $subject |
66
|
|
|
*/ |
67
|
|
|
private $subject; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* When the email should be sent. |
71
|
|
|
* |
72
|
|
|
* @var DateTimeInterface|null $sendTs |
73
|
|
|
*/ |
74
|
|
|
private $sendTs; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the primary key that uniquely identifies each queue item. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function key() |
82
|
|
|
{ |
83
|
|
|
return 'id'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the queue ID. |
88
|
|
|
* |
89
|
|
|
* @param string $queueId The queue ID. |
90
|
|
|
* @return self |
91
|
|
|
*/ |
92
|
|
|
public function setQueueId($queueId) |
93
|
|
|
{ |
94
|
|
|
$this->queueId = $queueId; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the queue ID. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function queueId() |
105
|
|
|
{ |
106
|
|
|
return $this->queueId; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the error code. |
111
|
|
|
* |
112
|
|
|
* @param string $errorCode The error code. |
113
|
|
|
* @return self |
114
|
|
|
*/ |
115
|
|
|
public function setErrorCode($errorCode) |
116
|
|
|
{ |
117
|
|
|
$this->errorCode = $errorCode; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the error code. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function errorCode() |
128
|
|
|
{ |
129
|
|
|
return $this->errorCode; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the Message-ID. |
134
|
|
|
* |
135
|
|
|
* @param string $messageId The Message-ID. |
136
|
|
|
* @throws InvalidArgumentException If the Message-ID is not a string. |
137
|
|
|
* @return self |
138
|
|
|
*/ |
139
|
|
|
public function setMessageId($messageId) |
140
|
|
|
{ |
141
|
|
|
if (!is_string($messageId)) { |
142
|
|
|
throw new InvalidArgumentException( |
143
|
|
|
'Message-ID must be a string.' |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->messageId = $messageId; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the Message-ID. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function messageId() |
158
|
|
|
{ |
159
|
|
|
return $this->messageId; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the campaign ID. |
164
|
|
|
* |
165
|
|
|
* @param string $campaign The campaign identifier. |
166
|
|
|
* @throws InvalidArgumentException If the campaign is invalid. |
167
|
|
|
* @return self |
168
|
|
|
*/ |
169
|
|
|
public function setCampaign($campaign) |
170
|
|
|
{ |
171
|
|
|
if (!is_string($campaign)) { |
172
|
|
|
throw new InvalidArgumentException( |
173
|
|
|
'Campaign must be a string' |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->campaign = $campaign; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the campaign identifier. |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function campaign() |
188
|
|
|
{ |
189
|
|
|
return $this->campaign; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set the sender's email address. |
194
|
|
|
* |
195
|
|
|
* @param string|array $email An email address. |
196
|
|
|
* @throws InvalidArgumentException If the email address is invalid. |
197
|
|
|
* @return self |
198
|
|
|
*/ |
199
|
|
|
public function setFrom($email) |
200
|
|
|
{ |
201
|
|
|
$this->from = $this->parseEmail($email); |
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get the sender's email address. |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function from() |
211
|
|
|
{ |
212
|
|
|
return $this->from; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Set the recipient's email address. |
217
|
|
|
* |
218
|
|
|
* @param string|array $email An email address. |
219
|
|
|
* @return self |
220
|
|
|
*/ |
221
|
|
|
public function setTo($email) |
222
|
|
|
{ |
223
|
|
|
$this->to = $this->parseEmail($email); |
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get the recipient's email address. |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function to() |
233
|
|
|
{ |
234
|
|
|
return $this->to; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Set the email subject. |
239
|
|
|
* |
240
|
|
|
* @param string $subject The email subject. |
241
|
|
|
* @throws InvalidArgumentException If the subject is not a string. |
242
|
|
|
* @return self |
243
|
|
|
*/ |
244
|
|
|
public function setSubject($subject) |
245
|
|
|
{ |
246
|
|
|
if (!is_string($subject)) { |
247
|
|
|
throw new InvalidArgumentException( |
248
|
|
|
'Subject needs to be a string' |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$this->subject = $subject; |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get the email subject. |
259
|
|
|
* |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
public function subject() |
263
|
|
|
{ |
264
|
|
|
return $this->subject; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param null|string|DateTime $ts The "send date" datetime value. |
269
|
|
|
* @throws InvalidArgumentException If the ts is not a valid datetime value. |
270
|
|
|
* @return self |
271
|
|
|
*/ |
272
|
|
|
public function setSendTs($ts) |
273
|
|
|
{ |
274
|
|
|
if ($ts === null) { |
275
|
|
|
$this->sendTs = null; |
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if (is_string($ts)) { |
280
|
|
|
try { |
281
|
|
|
$ts = new DateTime($ts); |
282
|
|
|
} catch (Exception $e) { |
283
|
|
|
throw new InvalidArgumentException($e->getMessage()); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
if (!($ts instanceof DateTimeInterface)) { |
288
|
|
|
throw new InvalidArgumentException( |
289
|
|
|
'Invalid "Send Date" value. Must be a date/time string or a DateTime object.' |
290
|
|
|
); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
$this->sendTs = $ts; |
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return null|DateTimeInterface |
299
|
|
|
*/ |
300
|
|
|
public function sendTs() |
301
|
|
|
{ |
302
|
|
|
return $this->sendTs; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @see StorableTrait::preSave() |
307
|
|
|
* @return boolean |
308
|
|
|
*/ |
309
|
|
|
protected function preSave() |
310
|
|
|
{ |
311
|
|
|
parent::preSave(); |
312
|
|
|
|
313
|
|
|
if ($this->sendTs() === null) { |
314
|
|
|
$this->setSendTs('now'); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return true; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|