Passed
Push — master ( c38f03...56099e )
by Alexander
02:30
created

Message   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 381
Duplicated Lines 0 %

Test Coverage

Coverage 90.85%

Importance

Changes 0
Metric Value
eloc 117
dl 0
loc 381
ccs 139
cts 153
cp 0.9085
rs 4.5599
c 0
b 0
f 0
wmc 58

38 Methods

Rating   Name   Duplication   Size   Complexity  
A getHtmlBody() 0 3 1
A setFrom() 0 5 1
A setHtmlBody() 0 5 1
A getTextBody() 0 3 1
A setCc() 0 5 1
A getBcc() 0 3 1
A setSubject() 0 5 1
A getSubject() 0 3 1
A getCharset() 0 3 1
A setReplyTo() 0 5 1
A getReplyTo() 0 3 1
A setTo() 0 5 1
A getFrom() 0 3 1
A setCharset() 0 5 1
A setTextBody() 0 5 1
A setBcc() 0 5 1
A getCc() 0 3 1
A getTo() 0 3 1
A __construct() 0 3 1
A getSwiftMessage() 0 3 1
A setReadReceiptTo() 0 5 1
A embedContent() 0 11 3
A toString() 0 3 1
A getReturnPath() 0 3 1
A getHeader() 0 13 3
A attachSigners() 0 7 2
A setHeaders() 0 7 2
A addHeader() 0 5 1
A attach() 0 12 3
A setPriority() 0 5 1
A __clone() 0 3 1
A setReturnPath() 0 5 1
B setBody() 0 38 7
A getPriority() 0 4 1
A getReadReceiptTo() 0 3 1
A attachContent() 0 12 3
A setHeader() 0 13 3
A embed() 0 11 3

How to fix   Complexity   

Complex Class

Complex classes like Message often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Message, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Mailer\SwiftMailer;
6
7
use Yiisoft\Mailer\BaseMessage;
8
use Yiisoft\Mailer\MessageInterface;
9
10
/**
11
 * Message implements a message class based on SwiftMailer.
12
 *
13
 * @see http://swiftmailer.org/docs/messages.html
14
 * @see Mailer
15
 */
16
class Message extends BaseMessage
17
{
18
    /**
19
     * @var \Swift_Message Swift message instance.
20
     */
21
    private \Swift_Message $swiftMessage;
22
23
    /**
24
     * @return \Swift_Message Swift message instance.
25
     */
26 7
    public function getSwiftMessage(): \Swift_Message
27
    {
28 7
        return $this->swiftMessage;
29
    }
30
31 51
    public function __construct()
32
    {
33 51
        $this->swiftMessage = new \Swift_Message();
34 51
    }
35
36
    /**
37
     * This method is called after the object is created by cloning an existing one.
38
     * It ensures [[swiftMessage]] is also cloned.
39
     */
40 1
    public function __clone()
41
    {
42 1
        $this->swiftMessage = clone $this->swiftMessage;
43 1
    }
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 10
    public function setFrom($from): MessageInterface
63
    {
64 10
        $this->swiftMessage->setFrom($from);
65
66 10
        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 11
    public function setTo($to): MessageInterface
87
    {
88 11
        $this->swiftMessage->setTo($to);
89
90 11
        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 8
    public function setSubject(string $subject): MessageInterface
123
    {
124 8
        $this->swiftMessage->setSubject($subject);
125
126 8
        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 4
    public function setHtmlBody(string $html): MessageInterface
147
    {
148 4
        $this->setBody($html, 'text/html');
149
150 4
        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 8
    protected function setBody(string $body, string $contentType): void
162
    {
163 8
        $message = $this->swiftMessage;
164 8
        $oldBody = $message->getBody();
165 8
        $charset = $message->getCharset();
166 8
        if (empty($oldBody)) {
167 8
            $parts = $message->getChildren();
168 8
            $partFound = false;
169 8
            foreach ($parts as $key => $part) {
170 1
                if (!($part instanceof \Swift_Mime_Attachment)) {
171
                    /**
172
                     * @var \Swift_Mime_MimePart $part
173
                     * @psalm-suppress UndefinedMethod
174
                     */
175 1
                    if ($part->getContentType() == $contentType) {
176 1
                        $charset = $part->getCharset();
177 1
                        unset($parts[$key]);
178 1
                        $partFound = true;
179 1
                        break;
180
                    }
181
                }
182
            }
183 8
            if ($partFound) {
184 1
                reset($parts);
185 1
                $message->setChildren($parts);
186 1
                $message->addPart($body, $contentType, $charset);
187
            } else {
188 8
                $message->setBody($body, $contentType);
189
            }
190
        } else {
191 3
            $oldContentType = $message->getContentType();
192 3
            if ($oldContentType == $contentType) {
193 1
                $message->setBody($body, $contentType);
194
            } else {
195 2
                $message->setBody(null);
196 2
                $message->setContentType('');
197 2
                $message->addPart($oldBody, $oldContentType, $charset);
198 2
                $message->addPart($body, $contentType, $charset);
199
            }
200
        }
201 8
    }
202
203 1
    public function attach(string $fileName, array $options = []): MessageInterface
204
    {
205 1
        $attachment = \Swift_Attachment::fromPath($fileName);
206 1
        if (!empty($options['fileName'])) {
207 1
            $attachment->setFilename($options['fileName']);
208
        }
209 1
        if (!empty($options['contentType'])) {
210 1
            $attachment->setContentType($options['contentType']);
211
        }
212 1
        $this->swiftMessage->attach($attachment);
213
214 1
        return $this;
215
    }
216
217 1
    public function attachContent(string $content, array $options = []): MessageInterface
218
    {
219 1
        $attachment = new \Swift_Attachment($content);
220 1
        if (!empty($options['fileName'])) {
221 1
            $attachment->setFilename($options['fileName']);
222
        }
223 1
        if (!empty($options['contentType'])) {
224 1
            $attachment->setContentType($options['contentType']);
225
        }
226 1
        $this->swiftMessage->attach($attachment);
227
228 1
        return $this;
229
    }
230
231
    public function embed(string $fileName, array $options = []): string
232
    {
233
        $embedFile = \Swift_EmbeddedFile::fromPath($fileName);
234
        if (!empty($options['fileName'])) {
235
            $embedFile->setFilename($options['fileName']);
236
        }
237
        if (!empty($options['contentType'])) {
238
            $embedFile->setContentType($options['contentType']);
239
        }
240
241
        return $this->swiftMessage->embed($embedFile);
242
    }
243
244
    public function embedContent(string $content, array $options = []): string
245
    {
246
        $embedFile = new \Swift_EmbeddedFile($content);
247
        if (!empty($options['fileName'])) {
248
            $embedFile->setFilename($options['fileName']);
249
        }
250
        if (!empty($options['contentType'])) {
251
            $embedFile->setContentType($options['contentType']);
252
        }
253
254
        return $this->swiftMessage->embed($embedFile);
255
    }
256
257 3
    public function toString(): string
258
    {
259 3
        return $this->swiftMessage->toString();
260
    }
261
262 1
    public function addHeader(string $name, string $value): MessageInterface
263
    {
264 1
        $this->swiftMessage->getHeaders()->addTextHeader($name, $value);
265
266 1
        return $this;
267
    }
268
269 3
    public function setHeader(string $name, $value): MessageInterface
270
    {
271 3
        $headerSet = $this->swiftMessage->getHeaders();
272
273 3
        if ($headerSet->has($name)) {
274 1
            $headerSet->remove($name);
275
        }
276
277 3
        foreach ((array)$value as $v) {
278 3
            $headerSet->addTextHeader($name, $v);
279
        }
280
281 3
        return $this;
282
    }
283
284 3
    public function getHeader(string $name): array
285
    {
286 3
        $headerSet = $this->swiftMessage->getHeaders();
287 3
        if (!$headerSet->has($name)) {
288 2
            return [];
289
        }
290
291 3
        $headers = [];
292 3
        foreach ($headerSet->getAll($name) as $header) {
293 3
            $headers[] = $header->getValue();
294
        }
295
296 3
        return $headers;
297
    }
298
299 1
    public function setHeaders(array $headers): MessageInterface
300
    {
301 1
        foreach ($headers as $name => $value) {
302 1
            $this->setHeader($name, $value);
303
        }
304
305 1
        return $this;
306
    }
307
308
    /**
309
     * Returns the return-path (the bounce address) of this message.
310
     *
311
     * @return string the bounce email address.
312
     */
313 1
    public function getReturnPath(): string
314
    {
315 1
        return $this->swiftMessage->getReturnPath();
316
    }
317
318
    /**
319
     * Set the return-path (the bounce address) of this message.
320
     *
321
     * @param string $address the bounce email address.
322
     *
323
     * @return $this self reference.
324
     */
325 2
    public function setReturnPath(string $address): MessageInterface
326
    {
327 2
        $this->swiftMessage->setReturnPath($address);
328
329 2
        return $this;
330
    }
331
332
    /**
333
     * Returns the priority of this message.
334
     *
335
     * @return int priority value as integer in range: `1..5`,
336
     * where 1 is the highest priority and 5 is the lowest.
337
     */
338 5
    public function getPriority(): int
339
    {
340
        /** @psalm-suppress RedundantCastGivenDocblockType */
341 5
        return (int)$this->swiftMessage->getPriority();
342
    }
343
344
    /**
345
     * Set the priority of this message.
346
     *
347
     * @param int $priority priority value, should be an integer in range: `1..5`,
348
     * where 1 is the highest priority and 5 is the lowest.
349
     *
350
     * @return $this self reference.
351
     */
352 6
    public function setPriority(int $priority): MessageInterface
353
    {
354 6
        $this->swiftMessage->setPriority($priority);
355
356 6
        return $this;
357
    }
358
359
    /**
360
     * Get the addresses to which a read-receipt will be sent.
361
     *
362
     * @return array|string receipt receive email addresses.
363
     */
364 4
    public function getReadReceiptTo()
365
    {
366 4
        return $this->swiftMessage->getReadReceiptTo();
367
    }
368
369
    /**
370
     * Sets the ask for a delivery receipt from the recipient to be sent to $addresses.
371
     *
372
     * @param array $addresses receipt receive email address(es).
373
     *
374
     * @return $this self reference.
375
     */
376 5
    public function setReadReceiptTo($addresses): MessageInterface
377
    {
378 5
        $this->swiftMessage->setReadReceiptTo($addresses);
379
380 5
        return $this;
381
    }
382
383
    /**
384
     * Attaches signers.
385
     *
386
     * @param \Swift_Signer[] $signers
387
     *
388
     * @return self
389
     */
390 2
    public function attachSigners(array $signers): self
391
    {
392 2
        foreach ($signers as $signer) {
393 2
            $this->swiftMessage->attachSigner($signer);
394
        }
395
396 2
        return $this;
397
    }
398
}
399