Completed
Push — master ( 46f3eb...7c230f )
by Mathieu
02:46 queued 40s
created

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