Completed
Pull Request — 5.x (#16)
by Lars
05:10
created

Swift_Mime_SimpleMessage::setPriority()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0741
Metric Value
dl 0
loc 23
ccs 10
cts 12
cp 0.8333
rs 8.7972
cc 4
eloc 17
nc 6
nop 1
crap 4.0741
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * The default email message class.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Mime_SimpleMessage extends Swift_Mime_MimePart implements Swift_Mime_Message
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * Create a new SimpleMessage with $headers, $encoder and $cache.
20
     *
21
     * @param Swift_Mime_HeaderSet       $headers
22
     * @param Swift_Mime_ContentEncoder  $encoder
23
     * @param Swift_KeyCache             $cache
24
     * @param Swift_EmailValidatorBridge $emailValidator
25
     * @param string                     $charset
26
     */
27 221
    public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_EmailValidatorBridge  $emailValidator, $charset = null)
28
    {
29 221
        parent::__construct($headers, $encoder, $cache, $emailValidator, $charset);
30 221
        $this->getHeaders()->defineOrdering(array(
31 221
            'Return-Path',
32
            'Received',
33
            'DKIM-Signature',
34
            'DomainKey-Signature',
35
            'Sender',
36
            'Message-ID',
37
            'Date',
38
            'Subject',
39
            'From',
40
            'Reply-To',
41
            'To',
42
            'Cc',
43
            'Bcc',
44
            'MIME-Version',
45
            'Content-Type',
46
            'Content-Transfer-Encoding',
47
            ));
48 221
        $this->getHeaders()->setAlwaysDisplayed(array('Date', 'Message-ID', 'From'));
49 221
        $this->getHeaders()->addTextHeader('MIME-Version', '1.0');
50 221
        $this->setDate(time());
51 221
        $this->setId($this->getId());
52 221
        $this->getHeaders()->addMailboxHeader('From');
53 221
    }
54
55
    /**
56
     * Always returns {@link LEVEL_TOP} for a message instance.
57
     *
58
     * @return int
59
     */
60 18
    public function getNestingLevel()
61
    {
62 18
        return self::LEVEL_TOP;
63
    }
64
65
    /**
66
     * Set the subject of this message.
67
     *
68
     * @param string $subject
69
     *
70
     * @return Swift_Mime_SimpleMessage
71
     */
72 112
    public function setSubject($subject)
73
    {
74 112
        if (!$this->_setHeaderFieldModel('Subject', $subject)) {
75 111
            $this->getHeaders()->addTextHeader('Subject', $subject);
76
        }
77
78 112
        return $this;
79
    }
80
81
    /**
82
     * Get the subject of this message.
83
     *
84
     * @return string
85
     */
86 1
    public function getSubject()
87
    {
88 1
        return $this->_getHeaderFieldModel('Subject');
89
    }
90
91
    /**
92
     * Set the date at which this message was created.
93
     *
94
     * @param int $date
95
     *
96
     * @return Swift_Mime_SimpleMessage
97
     */
98 221
    public function setDate($date)
99
    {
100 221
        if (!$this->_setHeaderFieldModel('Date', $date)) {
101 219
            $this->getHeaders()->addDateHeader('Date', $date);
102
        }
103
104 221
        return $this;
105
    }
106
107
    /**
108
     * Get the date at which this message was created.
109
     *
110
     * @return int
111
     */
112 78
    public function getDate()
113
    {
114 78
        return $this->_getHeaderFieldModel('Date');
115
    }
116
117
    /**
118
     * Set the return-path (the bounce address) of this message.
119
     *
120
     * @param string $address
121
     *
122
     * @return Swift_Mime_SimpleMessage
123
     */
124 33
    public function setReturnPath($address)
125
    {
126 33
        if (!$this->_setHeaderFieldModel('Return-Path', $address)) {
127 32
            $this->getHeaders()->addPathHeader('Return-Path', $address);
128
        }
129
130 33
        return $this;
131
    }
132
133
    /**
134
     * Get the return-path (bounce address) of this message.
135
     *
136
     * @return string
137
     */
138 5
    public function getReturnPath()
139
    {
140 5
        return $this->_getHeaderFieldModel('Return-Path');
141
    }
142
143
    /**
144
     * Set the sender of this message.
145
     *
146
     * This does not override the From field, but it has a higher significance.
147
     *
148
     * @param string|array $address
149
     * @param string       $name    optional
150
     *
151
     * @return Swift_Mime_SimpleMessage
152
     */
153 10 View Code Duplication
    public function setSender($address, $name = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155 10
        if (!is_array($address) && isset($name)) {
156 1
            $address = array($address => $name);
157
        }
158
159 10
        if (!$this->_setHeaderFieldModel('Sender', (array) $address)) {
160 9
            $this->getHeaders()->addMailboxHeader('Sender', (array) $address);
161
        }
162
163 10
        return $this;
164
    }
165
166
    /**
167
     * Get the sender of this message.
168
     *
169
     * @return string
170
     */
171 5
    public function getSender()
172
    {
173 5
        return $this->_getHeaderFieldModel('Sender');
174
    }
175
176
    /**
177
     * Add a From: address to this message.
178
     *
179
     * If $name is passed this name will be associated with the address.
180
     *
181
     * @param string $address
182
     * @param string $name    optional
183
     *
184
     * @return Swift_Mime_SimpleMessage
185
     */
186 1
    public function addFrom($address, $name = null)
187
    {
188 1
        $current = $this->getFrom();
189 1
        $current[$address] = $name;
190
191 1
        return $this->setFrom($current);
192
    }
193
194
    /**
195
     * Set the from address of this message.
196
     *
197
     * You may pass an array of addresses if this message is from multiple people.
198
     *
199
     * If $name is passed and the first parameter is a string, this name will be
200
     * associated with the address.
201
     *
202
     * @param string|array $addresses
203
     * @param string       $name      optional
204
     *
205
     * @return Swift_Mime_SimpleMessage
206
     */
207 84 View Code Duplication
    public function setFrom($addresses, $name = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
    {
209 84
        if (!is_array($addresses) && isset($name)) {
210 1
            $addresses = array($addresses => $name);
211
        }
212
213 84
        if (!$this->_setHeaderFieldModel('From', (array) $addresses)) {
214 3
            $this->getHeaders()->addMailboxHeader('From', (array) $addresses);
215
        }
216
217 84
        return $this;
218
    }
219
220
    /**
221
     * Get the from address of this message.
222
     *
223
     * @return mixed
224
     */
225 6
    public function getFrom()
226
    {
227 6
        return $this->_getHeaderFieldModel('From');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_getHeaderFieldModel('From'); (string) is incompatible with the return type declared by the interface Swift_Mime_Message::getFrom of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
228
    }
229
230
    /**
231
     * Add a Reply-To: address to this message.
232
     *
233
     * If $name is passed this name will be associated with the address.
234
     *
235
     * @param string $address
236
     * @param string $name    optional
237
     *
238
     * @return Swift_Mime_SimpleMessage
239
     */
240 1
    public function addReplyTo($address, $name = null)
241
    {
242 1
        $current = $this->getReplyTo();
243 1
        $current[$address] = $name;
244
245 1
        return $this->setReplyTo($current);
246
    }
247
248
    /**
249
     * Set the reply-to address of this message.
250
     *
251
     * You may pass an array of addresses if replies will go to multiple people.
252
     *
253
     * If $name is passed and the first parameter is a string, this name will be
254
     * associated with the address.
255
     *
256
     * @param mixed  $addresses
257
     * @param string $name      optional
258
     *
259
     * @return Swift_Mime_SimpleMessage
260
     */
261 21 View Code Duplication
    public function setReplyTo($addresses, $name = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
    {
263 21
        if (!is_array($addresses) && isset($name)) {
264 1
            $addresses = array($addresses => $name);
265
        }
266
267 21
        if (!$this->_setHeaderFieldModel('Reply-To', (array) $addresses)) {
268 19
            $this->getHeaders()->addMailboxHeader('Reply-To', (array) $addresses);
269
        }
270
271 21
        return $this;
272
    }
273
274
    /**
275
     * Get the reply-to address of this message.
276
     *
277
     * @return string
278
     */
279 2
    public function getReplyTo()
280
    {
281 2
        return $this->_getHeaderFieldModel('Reply-To');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_getHeaderFieldModel('Reply-To'); (string) is incompatible with the return type declared by the interface Swift_Mime_Message::getReplyTo of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
282
    }
283
284
    /**
285
     * Add a To: address to this message.
286
     *
287
     * If $name is passed this name will be associated with the address.
288
     *
289
     * @param string $address
290
     * @param string $name    optional
291
     *
292
     * @return Swift_Mime_SimpleMessage
293
     */
294 5
    public function addTo($address, $name = null)
295
    {
296 5
        $current = $this->getTo();
297 5
        $current[$address] = $name;
298
299 5
        return $this->setTo($current);
300
    }
301
302
    /**
303
     * Set the to addresses of this message.
304
     *
305
     * If multiple recipients will receive the message an array should be used.
306
     * Example: array('[email protected]', '[email protected]' => 'A name')
307
     *
308
     * If $name is passed and the first parameter is a string, this name will be
309
     * associated with the address.
310
     *
311
     * @param mixed  $addresses
312
     * @param string $name      optional
313
     *
314
     * @return Swift_Mime_SimpleMessage
315
     */
316 44 View Code Duplication
    public function setTo($addresses, $name = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
317
    {
318 44
        if (!is_array($addresses) && isset($name)) {
319 1
            $addresses = array($addresses => $name);
320
        }
321
322 44
        if (!$this->_setHeaderFieldModel('To', (array) $addresses)) {
323 42
            $this->getHeaders()->addMailboxHeader('To', (array) $addresses);
324
        }
325
326 44
        return $this;
327
    }
328
329
    /**
330
     * Get the To addresses of this message.
331
     *
332
     * @return array
333
     */
334 11
    public function getTo()
335
    {
336 11
        return $this->_getHeaderFieldModel('To');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_getHeaderFieldModel('To'); (string) is incompatible with the return type declared by the interface Swift_Mime_Message::getTo of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
337
    }
338
339
    /**
340
     * Add a Cc: address to this message.
341
     *
342
     * If $name is passed this name will be associated with the address.
343
     *
344
     * @param string $address
345
     * @param string $name    optional
346
     *
347
     * @return Swift_Mime_SimpleMessage
348
     */
349 1
    public function addCc($address, $name = null)
350
    {
351 1
        $current = $this->getCc();
352 1
        $current[$address] = $name;
353
354 1
        return $this->setCc($current);
355
    }
356
357
    /**
358
     * Set the Cc addresses of this message.
359
     *
360
     * If $name is passed and the first parameter is a string, this name will be
361
     * associated with the address.
362
     *
363
     * @param mixed  $addresses
364
     * @param string $name      optional
365
     *
366
     * @return Swift_Mime_SimpleMessage
367
     */
368 21 View Code Duplication
    public function setCc($addresses, $name = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
369
    {
370 21
        if (!is_array($addresses) && isset($name)) {
371 1
            $addresses = array($addresses => $name);
372
        }
373
374 21
        if (!$this->_setHeaderFieldModel('Cc', (array) $addresses)) {
375 19
            $this->getHeaders()->addMailboxHeader('Cc', (array) $addresses);
376
        }
377
378 21
        return $this;
379
    }
380
381
    /**
382
     * Get the Cc address of this message.
383
     *
384
     * @return array
385
     */
386 10
    public function getCc()
387
    {
388 10
        return $this->_getHeaderFieldModel('Cc');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_getHeaderFieldModel('Cc'); (string) is incompatible with the return type declared by the interface Swift_Mime_Message::getCc of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
389
    }
390
391
    /**
392
     * Add a Bcc: address to this message.
393
     *
394
     * If $name is passed this name will be associated with the address.
395
     *
396
     * @param string $address
397
     * @param string $name    optional
398
     *
399
     * @return Swift_Mime_SimpleMessage
400
     */
401 1
    public function addBcc($address, $name = null)
402
    {
403 1
        $current = $this->getBcc();
404 1
        $current[$address] = $name;
405
406 1
        return $this->setBcc($current);
407
    }
408
409
    /**
410
     * Set the Bcc addresses of this message.
411
     *
412
     * If $name is passed and the first parameter is a string, this name will be
413
     * associated with the address.
414
     *
415
     * @param mixed  $addresses
416
     * @param string $name      optional
417
     *
418
     * @return Swift_Mime_SimpleMessage
419
     */
420 17 View Code Duplication
    public function setBcc($addresses, $name = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
421
    {
422 17
        if (!is_array($addresses) && isset($name)) {
423 1
            $addresses = array($addresses => $name);
424
        }
425
426 17
        if (!$this->_setHeaderFieldModel('Bcc', (array) $addresses)) {
427 15
            $this->getHeaders()->addMailboxHeader('Bcc', (array) $addresses);
428
        }
429
430 17
        return $this;
431
    }
432
433
    /**
434
     * Get the Bcc addresses of this message.
435
     *
436
     * @return array
437
     */
438 10
    public function getBcc()
439
    {
440 10
        return $this->_getHeaderFieldModel('Bcc');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_getHeaderFieldModel('Bcc'); (string) is incompatible with the return type declared by the interface Swift_Mime_Message::getBcc of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
441
    }
442
443
    /**
444
     * Set the priority of this message.
445
     *
446
     * The value is an integer where 1 is the highest priority and 5 is the lowest.
447
     *
448
     * @param int $priority
449
     *
450
     * @return Swift_Mime_SimpleMessage
451
     */
452 3
    public function setPriority($priority)
453
    {
454
        $priorityMap = array(
455 3
            1 => 'Highest',
456
            2 => 'High',
457
            3 => 'Normal',
458
            4 => 'Low',
459
            5 => 'Lowest',
460
        );
461 3
        $pMapKeys = array_keys($priorityMap);
462 3
        if ($priority > max($pMapKeys)) {
463
            $priority = max($pMapKeys);
464 3
        } elseif ($priority < min($pMapKeys)) {
465
            $priority = min($pMapKeys);
466
        }
467 3
        if (!$this->_setHeaderFieldModel('X-Priority',
468 3
            sprintf('%d (%s)', $priority, $priorityMap[$priority]))) {
469 2
            $this->getHeaders()->addTextHeader('X-Priority',
470 2
                sprintf('%d (%s)', $priority, $priorityMap[$priority]));
471
        }
472
473 3
        return $this;
474
    }
475
476
    /**
477
     * Get the priority of this message.
478
     *
479
     * The returned value is an integer where 1 is the highest priority and 5
480
     * is the lowest.
481
     *
482
     * @return int
483
     */
484 1
    public function getPriority()
485
    {
486 1
        list($priority) = sscanf($this->_getHeaderFieldModel('X-Priority'), '%[1-5]');
487
488 1
        return isset($priority) ? $priority : 3;
489
    }
490
491
    /**
492
     * Ask for a delivery receipt from the recipient to be sent to $addresses.
493
     *
494
     * @param array $addresses
495
     *
496
     * @return Swift_Mime_SimpleMessage
497
     */
498 3
    public function setReadReceiptTo($addresses)
499
    {
500 3
        if (!$this->_setHeaderFieldModel('Disposition-Notification-To', $addresses)) {
501 2
            $this->getHeaders()->addMailboxHeader('Disposition-Notification-To', $addresses);
502
        }
503
504 3
        return $this;
505
    }
506
507
    /**
508
     * Get the addresses to which a read-receipt will be sent.
509
     *
510
     * @return string
511
     */
512 1
    public function getReadReceiptTo()
513
    {
514 1
        return $this->_getHeaderFieldModel('Disposition-Notification-To');
515
    }
516
517
    /**
518
     * Attach a {@link Swift_Mime_MimeEntity} such as an Attachment or MimePart.
519
     *
520
     * @param Swift_Mime_MimeEntity $entity
521
     *
522
     * @return Swift_Mime_SimpleMessage
523
     */
524 41
    public function attach(Swift_Mime_MimeEntity $entity)
525
    {
526 41
        $this->setChildren(array_merge($this->getChildren(), array($entity)));
527
528 41
        return $this;
529
    }
530
531
    /**
532
     * Remove an already attached entity.
533
     *
534
     * @param Swift_Mime_MimeEntity $entity
535
     *
536
     * @return Swift_Mime_SimpleMessage
537
     */
538 6
    public function detach(Swift_Mime_MimeEntity $entity)
539
    {
540 6
        $newChildren = array();
541 6
        foreach ($this->getChildren() as $child) {
542 6
            if ($entity !== $child) {
543 6
                $newChildren[] = $child;
544
            }
545
        }
546 6
        $this->setChildren($newChildren);
547
548 6
        return $this;
549
    }
550
551
    /**
552
     * Attach a {@link Swift_Mime_MimeEntity} and return it's CID source.
553
     * This method should be used when embedding images or other data in a message.
554
     *
555
     * @param Swift_Mime_MimeEntity $entity
556
     *
557
     * @return string
558
     */
559 8
    public function embed(Swift_Mime_MimeEntity $entity)
560
    {
561 8
        $this->attach($entity);
562
563 8
        return 'cid:' . $entity->getId();
564
    }
565
566
    /**
567
     * Get this message as a complete string.
568
     *
569
     * @return string
570
     */
571 96 View Code Duplication
    public function toString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
572
    {
573 96
        if (count($children = $this->getChildren()) > 0 && $this->getBody() != '') {
574 8
            $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
575 8
            $string = parent::toString();
576 8
            $this->setChildren($children);
577
        } else {
578 88
            $string = parent::toString();
579
        }
580
581 96
        return $string;
582
    }
583
584
    /**
585
     * Returns a string representation of this object.
586
     *
587
     * @see toString()
588
     *
589
     * @return string
590
     */
591
    public function __toString()
592
    {
593
        return $this->toString();
594
    }
595
596
    /**
597
     * Write this message to a {@link Swift_InputByteStream}.
598
     *
599
     * @param Swift_InputByteStream $is
600
     */
601 21 View Code Duplication
    public function toByteStream(Swift_InputByteStream $is)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
602
    {
603 21
        if (count($children = $this->getChildren()) > 0 && $this->getBody() != '') {
604 8
            $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
605 8
            parent::toByteStream($is);
606 8
            $this->setChildren($children);
607
        } else {
608 13
            parent::toByteStream($is);
609
        }
610 21
    }
611
612
    /** @see Swift_Mime_SimpleMimeEntity::_getIdField() */
613 221
    protected function _getIdField()
614
    {
615 221
        return 'Message-ID';
616
    }
617
618
    /** Turn the body of this message into a child of itself if needed */
619 17
    protected function _becomeMimePart()
620
    {
621 17
        $part = new parent(
622 17
            $this->getHeaders()->newInstance(),
623 17
            $this->getEncoder(),
624 17
            $this->_getCache(),
625 17
            $this->_getEmailValidator(),
626 17
            $this->_userCharset
627
        );
628 17
        $part->setContentType($this->_userContentType);
629 17
        $part->setBody($this->getBody());
630 17
        $part->setFormat($this->_userFormat);
631 17
        $part->setDelSp($this->_userDelSp);
632 17
        $part->setMaxLineLength($this->getMaxLineLength());
633 17
        $part->_setNestingLevel($this->_getTopNestingLevel());
634
635 17
        return $part;
636
    }
637
638
    /** Get the highest nesting level nested inside this message */
639 17
    private function _getTopNestingLevel()
640
    {
641 17
        $highestLevel = $this->getNestingLevel();
642 17
        foreach ($this->getChildren() as $child) {
643 17
            $childLevel = $child->getNestingLevel();
644 17
            if ($highestLevel < $childLevel) {
645 17
                $highestLevel = $childLevel;
646
            }
647
        }
648
649 17
        return $highestLevel;
650
    }
651
}
652