Passed
Push — master ( 37aad3...325143 )
by Zaahid
03:26
created

GenericHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 118
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A copyTypeHeadersAndContent() 0 16 4
A replacePart() 0 9 2
A copyHeader() 0 6 3
A removeTypeHeadersAndContent() 0 8 1
A createNewContentPartFrom() 0 5 1
A movePartContentAndChildren() 0 6 2
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Message\Helper;
8
9
use ZBateson\MailMimeParser\MailMimeParser;
10
use ZBateson\MailMimeParser\Message;
11
use ZBateson\MailMimeParser\Message\Part\MimePart;
12
use ZBateson\MailMimeParser\Message\Part\ParentHeaderPart;
13
14
/**
15
 * Provides common Message helper routines for Message manipulation.
16
 *
17
 * @author Zaahid Bateson
18
 */
19
class GenericHelper extends AbstractHelper
20
{
21
    /**
22
     * Copies the passed $header from $from, to $to or sets the header to
23
     * $default if it doesn't exist in $from.
24
     *
25
     * @param ParentHeaderPart $from
26
     * @param ParentHeaderPart $to
27
     * @param string $header
28
     * @param string $default
29
     */
30 4
    public function copyHeader(ParentHeaderPart $from, ParentHeaderPart $to, $header, $default = null)
31
    {
32 4
        $fromHeader = $from->getHeader($header);
33 4
        $set = ($fromHeader !== null) ? $fromHeader->getRawValue() : $default;
34 4
        if ($set !== null) {
35 2
            $to->setRawHeader($header, $set);
36
        }
37 4
    }
38
39
    /**
40
     * Removes the following headers from the passed part: Content-Type,
41
     * Content-Transfer-Encoding, Content-Disposition, Content-ID and
42
     * Content-Description, then detaches its content stream.
43
     * 
44
     * @param ParentHeaderPart $part
45
     */
46 3
    public function removeTypeHeadersAndContent(ParentHeaderPart $part)
47
    {
48 3
        $part->removeHeader('Content-Type');
49 3
        $part->removeHeader('Content-Transfer-Encoding');
50 3
        $part->removeHeader('Content-Disposition');
51 3
        $part->removeHeader('Content-ID');
52 3
        $part->removeHeader('Content-Description');
53 3
        $part->detachContentStream();
54 3
    }
55
56
    /**
57
     * Copies Content-Type, Content-Disposition and Content-Transfer-Encoding
58
     * headers from the $from header into the $to header. If the Content-Type
59
     * header isn't defined in $from, defaults to text/plain with utf-8 and
60
     * quoted-printable.
61
     *
62
     * @param ParentHeaderPart $from
63
     * @param ParentHeaderPart $to
64
     */
65 3
    public function copyTypeHeadersAndContent(ParentHeaderPart $from, ParentHeaderPart $to, $move = false)
66
    {
67 3
        $this->copyHeader($from, $to, 'Content-Type', 'text/plain; charset=utf-8');
68 3
        if ($from->getHeader('Content-Type') === null) {
69 1
            $this->copyHeader($from, $to, 'Content-Transfer-Encoding', 'quoted-printable');
70
        } else {
71 2
            $this->copyHeader($from, $to, 'Content-Transfer-Encoding');
72
        }
73 3
        $this->copyHeader($from, $to, 'Content-Disposition');
74 3
        $this->copyHeader($from, $to, 'Content-ID');
75 3
        $this->copyHeader($from, $to, 'Content-Description');
76 3
        if ($from->hasContent()) {
77 2
            $to->attachContentStream($from->getContentStream(), MailMimeParser::DEFAULT_CHARSET);
78
        }
79 3
        if ($move) {
80 2
            $this->removeTypeHeadersAndContent($from);
81
        }
82 3
    }
83
84
    /**
85
     * Creates a new content part from the passed part, allowing the part to be
86
     * used for something else (e.g. changing a non-mime message to a multipart
87
     * mime message).
88
     *
89
     * @param ParentHeaderPart $part
90
     * @return MimePart the newly-created MimePart
91
    */
92 1
    public function createNewContentPartFrom(ParentHeaderPart $part)
93
    {
94 1
        $mime = $this->partBuilderFactory->newPartBuilder($this->mimePartFactory)->createMessagePart();
95 1
        $this->copyTypeHeadersAndContent($part, $mime, true);
96 1
        return $mime;
97
    }
98
99
    /**
100
     * Copies type headers (Content-Type, Content-Disposition,
101
     * Content-Transfer-Encoding) from the $from MimePart to $to.  Attaches the
102
     * content resource handle of $from to $to, and loops over child parts,
103
     * removing them from $from and adding them to $to.
104
     *
105
     * @param ParentHeaderPart $from
106
     * @param ParentHeaderPart $to
107
     */
108 1
    public function movePartContentAndChildren(ParentHeaderPart $from, ParentHeaderPart $to)
109
    {
110 1
        $this->copyTypeHeadersAndContent($from, $to, true);
111 1
        foreach ($from->getChildParts() as $child) {
112 1
            $from->removePart($child);
113 1
            $to->addChild($child);
114
        }
115 1
    }
116
117
    /**
118
     * Replaces the $part ParentHeaderPart with $replacement.
119
     *
120
     * Essentially removes $part from its parent, and adds $replacement in its
121
     * same position.  If $part is this Message, then $part can't be removed and
122
     * replaced, and instead $replacement's type headers are copied to $message,
123
     * and any children below $replacement are added directly below $message.
124
     *
125
     * @param ParentHeaderPart $part
126
     * @param ParentHeaderPart $replacement
127
     */
128 2
    public function replacePart(Message $message, ParentHeaderPart $part, ParentHeaderPart $replacement)
129
    {
130 2
        $position = $message->removePart($replacement);
131 2
        if ($part === $message) {
0 ignored issues
show
introduced by
The condition $part === $message is always false.
Loading history...
132 1
            $this->movePartContentAndChildren($replacement, $part);
133 1
            return;
134
        }
135 1
        $parent = $part->getParent();
136 1
        $parent->addChild($replacement, $position);
137 1
    }
138
}
139