Passed
Push — master ( d452fd...6fd11e )
by Chauncey
03:50
created

EmailLog::sendError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: hasProperty, p, properties, property
Loading history...
17
{
18
    use EmailAwareTrait;
19
20
    /**
21
     * Type of log (e.g., "email").
22
     *
23
     * @var string $type
24
     */
25
    private $type;
26
27
    /**
28
     * The action logged (e.g., "send").
29
     *
30
     * @var string $action
31
     */
32
    private $action;
33
34
    /**
35
     * The mailer's raw response.
36
     *
37
     * @var mixed $rawResponse
38
     */
39
    private $rawResponse;
40
41
    /**
42
     * The Message-ID (Unique message identifier)
43
     *
44
     * @var string $messageId
45
     */
46
    private $messageId;
47
48
    /**
49
     * The campaign ID.
50
     *
51
     * @var string $campaign
52
     */
53
    private $campaign;
54
55
    /**
56
     * The sender's email address.
57
     *
58
     * @var string $from
59
     */
60
    private $from;
61
62
    /**
63
     * The recipient's email address.
64
     *
65
     * @var string $to
66
     */
67
    private $to;
68
69
    /**
70
     * The email subject.
71
     *
72
     * @var string $subject
73
     */
74
    private $subject;
75
76
    /**
77
     * Whether the email has been semt.
78
     *
79
     * Error code (0 = success)
80
     *
81
     * @var integer $sendStatus
82
     */
83
    private $sendStatus;
84
85
    /**
86
     * The error message from a failed send.
87
     *
88
     * @var string $sendError
89
     */
90
    private $sendError;
91
92
    /**
93
     * When the email should be sent.
94
     *
95
     * @var DateTimeInterface|null $sendTs
96
     */
97
    private $sendTs;
98
99
    /**
100
     * The current IP address at the time of the log.
101
     *
102
     * @var string $ip
103
     */
104
    private $ip;
105
106
    /**
107
     * The current session ID at the time of the log.
108
     *
109
     * @var string $sessionId
110
     */
111
    private $sessionId;
112
113
    /**
114
     * Get the primary key that uniquely identifies each queue item.
115
     *
116
     * @return string
117
     */
118
    public function key()
119
    {
120
        return 'id';
121
    }
122
123
    /**
124
     * Set the type of log.
125
     *
126
     * @param  string $type The log type. (e.g., "email").
127
     * @throws InvalidArgumentException If the log type is not a string.
128
     * @return self
129
     */
130
    public function setType($type)
131
    {
132
        if (!is_string($type)) {
133
            throw new InvalidArgumentException(
134
                'Log type must be a string.'
135
            );
136
        }
137
138
        $this->type = $type;
139
140
        return $this;
141
    }
142
143
    /**
144
     * Get the log type.
145
     *
146
     * @return string
147
     */
148
    public function type()
149
    {
150
        return $this->type;
151
    }
152
153
    /**
154
     * Set the logged action.
155
     *
156
     * @param  string $action The log action (e.g., "send").
157
     * @throws InvalidArgumentException If the action is not a string.
158
     * @return self
159
     */
160
    public function setAction($action)
161
    {
162
        if (!is_string($action)) {
163
            throw new InvalidArgumentException(
164
                'Action must be a string.'
165
            );
166
        }
167
168
        $this->action = $action;
169
170
        return $this;
171
    }
172
173
    /**
174
     * Get the logged action.
175
     *
176
     * @return string
177
     */
178
    public function action()
179
    {
180
        return $this->action;
181
    }
182
183
    /**
184
     * Set the raw response from the mailer.
185
     *
186
     * @param mixed $res The response object or array.
187
     * @return self
188
     */
189
    public function setRawResponse($res)
190
    {
191
        $this->rawResponse = $res;
192
        return $this;
193
    }
194
195
    /**
196
     * Get the raw response from the mailer.
197
     *
198
     * @return mixed
199
     */
200
    public function rawResponse()
201
    {
202
        return $this->rawResponse;
203
    }
204
205
    /**
206
     * Set the Message-ID.
207
     *
208
     * @param string $messageId The Message-ID.
209
     * @throws InvalidArgumentException If the Message-ID is not a string.
210
     * @return self
211
     */
212
    public function setMessageId($messageId)
213
    {
214
        if (!is_string($messageId)) {
215
            throw new InvalidArgumentException(
216
                'Message-ID must be a string.'
217
            );
218
        }
219
220
        $this->messageId = $messageId;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Get the Message-ID.
227
     *
228
     * @return string
229
     */
230
    public function messageId()
231
    {
232
        return $this->messageId;
233
    }
234
235
236
    /**
237
     * Set the campaign ID.
238
     *
239
     * @param  string $campaign The campaign identifier.
240
     * @throws InvalidArgumentException If the campaign is invalid.
241
     * @return self
242
     */
243
    public function setCampaign($campaign)
244
    {
245
        if (!is_string($campaign)) {
246
            throw new InvalidArgumentException(
247
                'Campaign must be a string'
248
            );
249
        }
250
251
        $this->campaign = $campaign;
252
253
        return $this;
254
    }
255
256
    /**
257
     * Get the campaign identifier.
258
     *
259
     * @return string
260
     */
261
    public function campaign()
262
    {
263
        return $this->campaign;
264
    }
265
266
    /**
267
     * Set the sender's email address.
268
     *
269
     * @param  string|array $email An email address.
270
     * @throws InvalidArgumentException If the email address is invalid.
271
     * @return self
272
     */
273
    public function setFrom($email)
274
    {
275
        $this->from = $this->parseEmail($email);
276
        return $this;
277
    }
278
279
    /**
280
     * Get the sender's email address.
281
     *
282
     * @return string
283
     */
284
    public function from()
285
    {
286
        return $this->from;
287
    }
288
289
    /**
290
     * Set the recipient's email address.
291
     *
292
     * @param  string|array $email An email address.
293
     * @return self
294
     */
295
    public function setTo($email)
296
    {
297
        $this->to = $this->parseEmail($email);
298
        return $this;
299
    }
300
301
    /**
302
     * Get the recipient's email address.
303
     *
304
     * @return string
305
     */
306
    public function to()
307
    {
308
        return $this->to;
309
    }
310
311
    /**
312
     * Set the email subject.
313
     *
314
     * @param  string $subject The email subject.
315
     * @throws InvalidArgumentException If the subject is not a string.
316
     * @return self
317
     */
318
    public function setSubject($subject)
319
    {
320
        if (!is_string($subject)) {
321
            throw new InvalidArgumentException(
322
                'Subject needs to be a string'
323
            );
324
        }
325
326
        $this->subject = $subject;
327
328
        return $this;
329
    }
330
331
    /**
332
     * Get the email subject.
333
     *
334
     * @return string
335
     */
336
    public function subject()
337
    {
338
        return $this->subject;
339
    }
340
341
    /**
342
     * @param  string $status The mailer's status code or description.
343
     * @return self
344
     */
345
    public function setSendStatus($status)
346
    {
347
        $this->sendStatus = $status;
0 ignored issues
show
Documentation Bug introduced by
The property $sendStatus was declared of type integer, but $status is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
348
        return $this;
349
    }
350
351
    /**
352
     * @return string|null
353
     */
354
    public function sendStatus()
355
    {
356
        return $this->sendStatus;
357
    }
358
359
    /**
360
     * @param  string $errorMessage The mailer's error code or description.
361
     * @return self
362
     */
363
    public function setSendError($errorMessage)
364
    {
365
        $this->sendError = $errorMessage;
366
        return $this;
367
    }
368
369
    /**
370
     * @return string|null
371
     */
372
    public function sendError()
373
    {
374
        return $this->sendError;
375
    }
376
377
    /**
378
     * @param  null|string|DateTime $ts The "send date" datetime value.
379
     * @throws InvalidArgumentException If the ts is not a valid datetime value.
380
     * @return self
381
     */
382
    public function setSendTs($ts)
383
    {
384
        if ($ts === null) {
385
            $this->sendTs = null;
386
            return $this;
387
        }
388
389
        if (is_string($ts)) {
390
            try {
391
                $ts = new DateTime($ts);
392
            } catch (Exception $e) {
393
                throw new InvalidArgumentException($e->getMessage());
394
            }
395
        }
396
397
        if (!($ts instanceof DateTimeInterface)) {
398
            throw new InvalidArgumentException(
399
                'Invalid "Send Date" value. Must be a date/time string or a DateTime object.'
400
            );
401
        }
402
403
        $this->sendTs = $ts;
404
        return $this;
405
    }
406
407
    /**
408
     * @return null|DateTimeInterface
409
     */
410
    public function sendTs()
411
    {
412
        return $this->sendTs;
413
    }
414
415
    /**
416
     * @param mixed $ip The IP adress.
417
     * @return self
418
     */
419
    public function setIp($ip)
420
    {
421
        $this->ip = $ip;
422
        return $this;
423
    }
424
425
    /**
426
     * @return mixed
427
     */
428
    public function ip()
429
    {
430
        return $this->ip;
431
    }
432
433
    /**
434
     * @param string $sessionId The session identifier.
435
     * @return self
436
     */
437
    public function setSessionId($sessionId)
438
    {
439
        $this->sessionId = $sessionId;
440
        return $this;
441
    }
442
443
    /**
444
     * @return string
445
     */
446
    public function sessionId()
447
    {
448
        return $this->sessionId;
449
    }
450
451
    /**
452
     * @see    StorableTrait::preSave()
453
     * @return boolean
454
     */
455
    protected function preSave()
456
    {
457
        parent::preSave();
458
459
        $ip = (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
460
        $sessionId = session_id();
461
462
        $this->setIp($ip);
463
        $this->setSessionId($sessionId);
464
465
        if ($this->sendTs() === null) {
466
            $this->setSendTs('now');
467
        }
468
469
        return true;
470
    }
471
}
472