Email::addBccRecipients()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEmail;
6
7
use PhpEmail\Content\Contracts\TemplatedContent;
8
9
class Email
10
{
11
    /**
12
     * @var array|Address[]
13
     */
14
    private $toRecipients;
15
16
    /**
17
     * @var array|Address[]
18
     */
19
    private $ccRecipients = [];
20
21
    /**
22
     * @var array|Address[]
23
     */
24
    private $bccRecipients = [];
25
26
    /**
27
     * @var Address
28
     */
29
    private $from;
30
31
    /**
32
     * @var array|Address[]
33
     */
34
    private $replyTos = [];
35
36
    /**
37
     * @var string|null
38
     */
39
    private $subject;
40
41
    /**
42
     * @var Content
43
     */
44
    private $content;
45
46
    /**
47
     * @var array|Attachment[]
48
     */
49
    private $attachments = [];
50
51
    /**
52
     * @var Attachment[]
53
     */
54
    private $embedded = [];
55
56
    /**
57
     * @var Header[]
58
     */
59
    private $headers = [];
60
61
    /**
62
     * @param Content $content
63
     * @param Address $from
64
     * @param array   $toRecipients
65
     * @param string  $subject
66
     *
67
     * @throws ValidationException
68
     */
69 4
    public function __construct(
70
        ?string $subject,
71
        Content $content,
72
        Address $from,
73
        array $toRecipients
74
    ) {
75 4
        Validate::that()
76 4
            ->allInstanceOf('toRecipients', $toRecipients, Address::class)
77 4
            ->using('subject', $subject, function (?string $subject) use ($content) {
78 4
                return $content instanceof TemplatedContent || strlen($subject) >= 1;
79 4
            }, 'Subject must have a length of at least 1 for non-templated emails')
80 4
            ->now();
81
82 3
        $this->content = $content;
83 3
        $this->from    = $from;
84 3
        $this->subject = $subject;
85
86 3
        $this->setToRecipients(...$toRecipients);
87
    }
88
89
    /**
90
     * @return array|Address[]
91
     */
92 2
    public function getToRecipients(): array
93
    {
94 2
        return $this->toRecipients;
95
    }
96
97
    /**
98
     * @param array|Address[] $toRecipients
99
     *
100
     * @return Email
101
     */
102 3
    public function setToRecipients(Address ...$toRecipients): self
103
    {
104 3
        $this->toRecipients = $toRecipients;
105
106 3
        return $this;
107
    }
108
109
    /**
110
     * @param array|Address[] ...$toRecipients
111
     *
112
     * @return Email
113
     */
114 1
    public function addToRecipients(Address ...$toRecipients): self
115
    {
116 1
        $this->toRecipients = array_values(array_unique(array_merge($this->toRecipients, $toRecipients)));
117
118 1
        return $this;
119
    }
120
121
    /**
122
     * @return array|Address[]
123
     */
124 2
    public function getCcRecipients(): array
125
    {
126 2
        return $this->ccRecipients;
127
    }
128
129
    /**
130
     * @param array|Address[] ...$ccRecipients
131
     *
132
     * @return Email
133
     */
134 1
    public function setCcRecipients(Address ...$ccRecipients): self
135
    {
136 1
        $this->ccRecipients = $ccRecipients;
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * @param array|Address[] ...$ccRecipients
143
     *
144
     * @return Email
145
     */
146 1
    public function addCcRecipients(Address ...$ccRecipients): self
147
    {
148 1
        $this->ccRecipients = array_values(array_unique(array_merge($this->ccRecipients, $ccRecipients)));
149
150 1
        return $this;
151
    }
152
153
    /**
154
     * @return array|Address[]
155
     */
156 2
    public function getBccRecipients(): array
157
    {
158 2
        return $this->bccRecipients;
159
    }
160
161
    /**
162
     * @param array|Address[] ...$bccRecipients
163
     *
164
     * @return Email
165
     */
166 1
    public function setBccRecipients(Address ...$bccRecipients): self
167
    {
168 1
        $this->bccRecipients = $bccRecipients;
169
170 1
        return $this;
171
    }
172
173
    /**
174
     * @param array|Address[] ...$bccRecipients
175
     *
176
     * @return Email
177
     */
178 1
    public function addBccRecipients(Address ...$bccRecipients): self
179
    {
180 1
        $this->bccRecipients = array_values(array_unique(array_merge($this->bccRecipients, $bccRecipients)));
181
182 1
        return $this;
183
    }
184
185
    /**
186
     * @return Address
187
     */
188 2
    public function getFrom(): Address
189
    {
190 2
        return $this->from;
191
    }
192
193
    /**
194
     * @param Address $from
195
     *
196
     * @return Email
197
     */
198 1
    public function setFrom(Address $from): self
199
    {
200 1
        $this->from = $from;
201
202 1
        return $this;
203
    }
204
205
    /**
206
     * @return array|Address[]
207
     */
208 2
    public function getReplyTos(): array
209
    {
210 2
        return $this->replyTos;
211
    }
212
213
    /**
214
     * @param array|Address[] $replyTos
215
     *
216
     * @return Email
217
     */
218 1
    public function setReplyTos(Address ...$replyTos): self
219
    {
220 1
        $this->replyTos = $replyTos;
221
222 1
        return $this;
223
    }
224
225
    /**
226
     * @param array|Address[] ...$replyTos
227
     *
228
     * @return Email
229
     */
230 1
    public function addReplyTos(Address ...$replyTos): self
231
    {
232 1
        $this->replyTos = array_values(array_unique(array_merge($this->replyTos, $replyTos)));
233
234 1
        return $this;
235
    }
236
237
    /**
238
     * @return string|null
239
     */
240 2
    public function getSubject(): ?string
241
    {
242 2
        return $this->subject;
243
    }
244
245
    /**
246
     * @param string $subject
247
     *
248
     * @throws ValidationException
249
     *
250
     * @return Email
251
     */
252 1
    public function setSubject(string $subject): self
253
    {
254 1
        Validate::that()
255 1
            ->hasMinLength('subject', $subject, 1)
256 1
            ->now();
257
258 1
        $this->subject = $subject;
259
260 1
        return $this;
261
    }
262
263
    /**
264
     * @return Content
265
     */
266 2
    public function getContent(): Content
267
    {
268 2
        return $this->content;
269
    }
270
271
    /**
272
     * @param Content $content
273
     *
274
     * @return Email
275
     */
276 1
    public function setContent(Content $content): self
277
    {
278 1
        $this->content = $content;
279
280 1
        return $this;
281
    }
282
283
    /**
284
     * @return array|Attachment[]
285
     */
286 2
    public function getAttachments(): array
287
    {
288 2
        return $this->attachments;
289
    }
290
291
    /**
292
     * @param Attachment[] ...$attachments
293
     *
294
     * @throws ValidationException
295
     *
296
     * @return Email
297
     */
298 1
    public function setAttachments(Attachment ...$attachments): self
299
    {
300 1
        $this->attachments = $attachments;
301
302 1
        return $this;
303
    }
304
305
    /**
306
     * @param Attachment[] $attachments
307
     *
308
     * @throws ValidationException
309
     *
310
     * @return Email
311
     */
312 1
    public function addAttachments(Attachment ...$attachments): self
313
    {
314 1
        $this->attachments = array_values(array_unique(array_merge($this->attachments, $attachments)));
315
316 1
        return $this;
317
    }
318
319
    /**
320
     * Embed an attachment into the email, setting the Content ID.
321
     *
322
     * @param Attachment  $attachment
323
     * @param null|string $contentId
324
     *
325
     * @return Email
326
     */
327 2
    public function embed(Attachment $attachment, ?string $contentId = null): self
328
    {
329 2
        $contentId = $contentId ?? $attachment->getContentId() ?? $attachment->getName();
330
331 2
        $this->embedded[$attachment->getContentId()] = $attachment->setContentId($contentId);
332
333 2
        return $this;
334
    }
335
336
    /**
337
     * Set the embedded attachments for the email.
338
     *
339
     * This function assumes that all attachments already have a Content ID set, or it will use the default value. This
340
     * function will remove all currently embedded attachments.
341
     *
342
     * @param Attachment[] ...$attachments
343
     *
344
     * @return Email
345
     */
346 1
    public function setEmbedded(Attachment ...$attachments): self
347
    {
348 1
        $this->embedded = [];
349
350 1
        return $this->addEmbedded(...$attachments);
351
    }
352
353
    /**
354
     * Add a collection of attachments to the email.
355
     *
356
     * This function assumes that all attachments already have a Content ID set, or it will use the default value.
357
     *
358
     * @param Attachment[] ...$attachments
359
     *
360
     * @return Email
361
     */
362 2
    public function addEmbedded(Attachment ...$attachments): self
363
    {
364 2
        foreach ($attachments as $attachment) {
365 2
            $this->embed($attachment);
366
        }
367
368 2
        return $this;
369
    }
370
371
    /**
372
     * @return Attachment[]
373
     */
374 2
    public function getEmbedded(): array
375
    {
376 2
        return array_values($this->embedded);
377
    }
378
379
    /**
380
     * @return Header[]
381
     */
382 2
    public function getHeaders(): array
383
    {
384 2
        return array_values($this->headers);
385
    }
386
387
    /**
388
     * @param Header[] $headers
389
     *
390
     * @return Email
391
     */
392 1
    public function setHeaders(Header ...$headers): self
393
    {
394 1
        $this->headers = [];
395
396 1
        return $this->addHeaders(...$headers);
397
    }
398
399
    /**
400
     * @param Header[] $headers
401
     *
402
     * @return Email
403
     */
404 2
    public function addHeaders(Header ...$headers): self
405
    {
406 2
        foreach ($headers as $header) {
407 2
            $this->headers[$header->getField()] = $header;
408
        }
409
410 2
        return $this;
411
    }
412
}
413