Passed
Push — master ( 63beec...7c9d1c )
by Alexander
01:40
created

Message::setReplyTo()   A

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
namespace Yiisoft\Mailer\SwiftMailer;
4
5
use Yiisoft\Mailer\BaseMessage;
6
use Yiisoft\Mailer\MessageInterface;
7
8
/**
9
 * Message implements a message class based on SwiftMailer.
10
 *
11
 * @see http://swiftmailer.org/docs/messages.html
12
 * @see Mailer
13
 */
14
class Message extends BaseMessage
15
{
16
    /**
17
     * @var \Swift_Message Swift message instance.
18
     */
19
    private $swiftMessage;
20
21
    /**
22
     * @return \Swift_Message Swift message instance.
23
     */
24 9
    public function getSwiftMessage(): \Swift_Message
25
    {
26 9
        return $this->swiftMessage;
27
    }
28
29 53
    public function __construct()
30
    {
31 53
        $this->swiftMessage = new \Swift_Message();
32
    }
33
34
    /**
35
     * This method is called after the object is created by cloning an existing one.
36
     * It ensures [[swiftMessage]] is also cloned.
37
     */
38 1
    public function __clone()
39
    {
40 1
        if (is_object($this->swiftMessage)) {
41 1
            $this->swiftMessage = clone $this->swiftMessage;
42
        }
43
    }
44
45 2
    public function getCharset(): string
46
    {
47 2
        return $this->swiftMessage->getCharset();
48
    }
49
50 4
    public function setCharset(string $charset): MessageInterface
51
    {
52 4
        $this->swiftMessage->setCharset($charset);
53
54 4
        return $this;
55
    }
56
57 4
    public function getFrom()
58
    {
59 4
        return $this->swiftMessage->getFrom();
60
    }
61
62 12
    public function setFrom($from): MessageInterface
63
    {
64 12
        $this->swiftMessage->setFrom($from);
65
66 12
        return $this;
67
    }
68
69 4
    public function getReplyTo()
70
    {
71 4
        return $this->swiftMessage->getReplyTo();
72
    }
73
74 5
    public function setReplyTo($replyTo): MessageInterface
75
    {
76 5
        $this->swiftMessage->setReplyTo($replyTo);
77
78 5
        return $this;
79
    }
80
81 6
    public function getTo()
82
    {
83 6
        return $this->swiftMessage->getTo();
84
    }
85
86 13
    public function setTo($to): MessageInterface
87
    {
88 13
        $this->swiftMessage->setTo($to);
89
90 13
        return $this;
91
    }
92
93 4
    public function getCc()
94
    {
95 4
        return $this->swiftMessage->getCc();
96
    }
97
98 5
    public function setCc($cc): MessageInterface
99
    {
100 5
        $this->swiftMessage->setCc($cc);
101
102 5
        return $this;
103
    }
104
105 4
    public function getBcc()
106
    {
107 4
        return $this->swiftMessage->getBcc();
108
    }
109
110 5
    public function setBcc($bcc): MessageInterface
111
    {
112 5
        $this->swiftMessage->setBcc($bcc);
113
114 5
        return $this;
115
    }
116
117 3
    public function getSubject(): string
118
    {
119 3
        return $this->swiftMessage->getSubject();
120
    }
121
122 10
    public function setSubject(string $subject): MessageInterface
123
    {
124 10
        $this->swiftMessage->setSubject($subject);
125
126 10
        return $this;
127
    }
128
129 1
    public function getTextBody(): string
130
    {
131 1
        return $this->swiftMessage->getBody();
132
    }
133
134 6
    public function setTextBody(string $text): MessageInterface
135
    {
136 6
        $this->setBody($text, 'text/plain');
137
138 6
        return $this;
139
    }
140
141 2
    public function getHtmlBody(): string
142
    {
143 2
        return $this->swiftMessage->getBody();
144
    }
145
146 6
    public function setHtmlBody(string $html): MessageInterface
147
    {
148 6
        $this->setBody($html, 'text/html');
149
150 6
        return $this;
151
    }
152
153
    /**
154
     * Sets the message body.
155
     * If body is already set and its content type matches given one, it will
156
     * be overridden, if content type miss match the multipart message will be composed.
157
     *
158
     * @param string $body        body content.
159
     * @param string $contentType body content type.
160
     */
161 10
    protected function setBody(string $body, string $contentType): void
162
    {
163 10
        $message = $this->swiftMessage;
164 10
        $oldBody = $message->getBody();
165 10
        $charset = $message->getCharset();
166 10
        if (empty($oldBody)) {
167 10
            $parts = $message->getChildren();
168 10
            $partFound = false;
169 10
            foreach ($parts as $key => $part) {
170 3
                if (!($part instanceof \Swift_Mime_Attachment)) {
171
                    /* @var $part \Swift_Mime_MimePart */
172 1
                    if ($part->getContentType() == $contentType) {
173 1
                        $charset = $part->getCharset();
174 1
                        unset($parts[$key]);
175 1
                        $partFound = true;
176 1
                        break;
177
                    }
178
                }
179
            }
180 10
            if ($partFound) {
181 1
                reset($parts);
182 1
                $message->setChildren($parts);
183 1
                $message->addPart($body, $contentType, $charset);
184
            } else {
185 10
                $message->setBody($body, $contentType);
186
            }
187
        } else {
188 3
            $oldContentType = $message->getContentType();
189 3
            if ($oldContentType == $contentType) {
190 1
                $message->setBody($body, $contentType);
191
            } else {
192 2
                $message->setBody(null);
193 2
                $message->setContentType(null);
194 2
                $message->addPart($oldBody, $oldContentType, $charset);
195 2
                $message->addPart($body, $contentType, $charset);
196
            }
197
        }
198
    }
199
200 1
    public function attach(string $fileName, array $options = []): MessageInterface
201
    {
202 1
        $attachment = \Swift_Attachment::fromPath($fileName);
203 1
        if (!empty($options['fileName'])) {
204 1
            $attachment->setFilename($options['fileName']);
205
        }
206 1
        if (!empty($options['contentType'])) {
207 1
            $attachment->setContentType($options['contentType']);
208
        }
209 1
        $this->swiftMessage->attach($attachment);
210
211 1
        return $this;
212
    }
213
214 1
    public function attachContent(string $content, array $options = []): MessageInterface
215
    {
216 1
        $attachment = new \Swift_Attachment($content);
217 1
        if (!empty($options['fileName'])) {
218 1
            $attachment->setFilename($options['fileName']);
219
        }
220 1
        if (!empty($options['contentType'])) {
221 1
            $attachment->setContentType($options['contentType']);
222
        }
223 1
        $this->swiftMessage->attach($attachment);
224
225 1
        return $this;
226
    }
227
228 1
    public function embed(string $fileName, array $options = []): string
229
    {
230 1
        $embedFile = \Swift_EmbeddedFile::fromPath($fileName);
231 1
        if (!empty($options['fileName'])) {
232 1
            $embedFile->setFilename($options['fileName']);
233
        }
234 1
        if (!empty($options['contentType'])) {
235 1
            $embedFile->setContentType($options['contentType']);
236
        }
237
238 1
        return $this->swiftMessage->embed($embedFile);
239
    }
240
241 1
    public function embedContent(string $content, array $options = []): string
242
    {
243 1
        $embedFile = new \Swift_EmbeddedFile($content);
244 1
        if (!empty($options['fileName'])) {
245 1
            $embedFile->setFilename($options['fileName']);
246
        }
247 1
        if (!empty($options['contentType'])) {
248 1
            $embedFile->setContentType($options['contentType']);
249
        }
250
251 1
        return $this->swiftMessage->embed($embedFile);
252
    }
253
254 3
    public function toString(): string
255
    {
256 3
        return $this->swiftMessage->toString();
257
    }
258
259 1
    public function addHeader(string $name, string $value): MessageInterface
260
    {
261 1
        $this->swiftMessage->getHeaders()->addTextHeader($name, $value);
262
263 1
        return $this;
264
    }
265
266 3
    public function setHeader(string $name, $value): MessageInterface
267
    {
268 3
        $headerSet = $this->swiftMessage->getHeaders();
269
270 3
        if ($headerSet->has($name)) {
271 1
            $headerSet->remove($name);
272
        }
273
274 3
        foreach ((array)$value as $v) {
275 3
            $headerSet->addTextHeader($name, $v);
276
        }
277
278 3
        return $this;
279
    }
280
281 3
    public function getHeader(string $name): array
282
    {
283 3
        $headerSet = $this->swiftMessage->getHeaders();
284 3
        if (!$headerSet->has($name)) {
285 2
            return [];
286
        }
287
288 3
        $headers = [];
289 3
        foreach ($headerSet->getAll($name) as $header) {
290 3
            $headers[] = $header->getValue();
291
        }
292
293 3
        return $headers;
294
    }
295
296 1
    public function setHeaders(array $headers): MessageInterface
297
    {
298 1
        foreach ($headers as $name => $value) {
299 1
            $this->setHeader($name, $value);
300
        }
301
302 1
        return $this;
303
    }
304
305
    /**
306
     * Returns the return-path (the bounce address) of this message.
307
     *
308
     * @return string the bounce email address.
309
     */
310 1
    public function getReturnPath(): string
311
    {
312 1
        return $this->swiftMessage->getReturnPath();
313
    }
314
315
    /**
316
     * Set the return-path (the bounce address) of this message.
317
     *
318
     * @param string $address the bounce email address.
319
     *
320
     * @return $this self reference.
321
     */
322 2
    public function setReturnPath(string $address): MessageInterface
323
    {
324 2
        $this->swiftMessage->setReturnPath($address);
325
326 2
        return $this;
327
    }
328
329
    /**
330
     * Returns the priority of this message.
331
     *
332
     * @return int priority value as integer in range: `1..5`,
333
     * where 1 is the highest priority and 5 is the lowest.
334
     */
335 5
    public function getPriority(): int
336
    {
337 5
        return $this->swiftMessage->getPriority();
338
    }
339
340
    /**
341
     * Set the priority of this message.
342
     *
343
     * @param int $priority priority value, should be an integer in range: `1..5`,
344
     * where 1 is the highest priority and 5 is the lowest.
345
     *
346
     * @return $this self reference.
347
     */
348 6
    public function setPriority(int $priority): MessageInterface
349
    {
350 6
        $this->swiftMessage->setPriority($priority);
351
352 6
        return $this;
353
    }
354
355
    /**
356
     * Get the addresses to which a read-receipt will be sent.
357
     *
358
     * @return string|array receipt receive email addresses.
359
     */
360 4
    public function getReadReceiptTo()
361
    {
362 4
        return $this->swiftMessage->getReadReceiptTo();
363
    }
364
365
    /**
366
     * Sets the ask for a delivery receipt from the recipient to be sent to $addresses.
367
     *
368
     * @param string|array $addresses receipt receive email address(es).
369
     *
370
     * @return $this self reference.
371
     */
372 5
    public function setReadReceiptTo($addresses): MessageInterface
373
    {
374 5
        $this->swiftMessage->setReadReceiptTo($addresses);
0 ignored issues
show
Bug introduced by
It seems like $addresses can also be of type string; however, parameter $addresses of Swift_Mime_SimpleMessage::setReadReceiptTo() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

374
        $this->swiftMessage->setReadReceiptTo(/** @scrutinizer ignore-type */ $addresses);
Loading history...
375
376 5
        return $this;
377
    }
378
379
    /**
380
     * Attaches signers.
381
     *
382
     * @param \Swift_Signer[] $signers
383
     *
384
     * @return self
385
     */
386 2
    public function attachSigners(array $signers): self
387
    {
388 2
        foreach ($signers as $signer) {
389 2
            $this->swiftMessage->attachSigner($signer);
390
        }
391
392 2
        return $this;
393
    }
394
}
395