Test Failed
Pull Request — master (#171)
by Zaahid
04:55
created

MimePart   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 170
rs 9.6
wmc 35

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getRawHeaderIterator() 0 3 1
A isMime() 0 3 1
A isTextPart() 0 3 1
A isSignaturePart() 0 6 3
A getFilename() 0 8 1
A getHeader() 0 3 1
A removeHeader() 0 4 1
A getContentDisposition() 0 3 1
A getHeaderValue() 0 7 2
A getContentTransferEncoding() 0 12 2
A __construct() 0 24 5
A getContentId() 0 3 1
A setRawHeader() 0 4 1
A getCharset() 0 11 5
A addRawHeader() 0 4 1
A removeSingleHeader() 0 4 1
A getHeaderParameter() 0 7 3
A getContentType() 0 3 1
A getAllHeadersByName() 0 3 1
A getRawHeaders() 0 3 1
A getAllHeaders() 0 3 1
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;
8
9
use ZBateson\MailMimeParser\IMessage;
10
use ZBateson\MailMimeParser\MailMimeParser;
11
use ZBateson\MailMimeParser\Header\ParameterHeader;
12
13
/**
14
 * Implementation of IMimePart.
15
 *
16
 * @author Zaahid Bateson
17
 */
18
class MimePart extends MultiPart implements IMimePart
19
{
20
    /**
21
     * @var PartHeaderContainer Contains headers for this part.
22
     */
23
    protected $headerContainer;
24
25
    public function __construct(
26
        IMimePart $parent = null,
27
        PartStreamContainer $streamContainer = null,
28
        PartHeaderContainer $headerContainer = null,
29
        PartChildrenContainer $partChildrenContainer = null
30
    ) {
31
        $setStream = false;
32
        $di = MailMimeParser::getDependencyContainer();
33
        if ($streamContainer === null || $headerContainer === null || $partChildrenContainer === null) {
34
            $headerContainer = $di['\ZBateson\MailMimeParser\Message\PartHeaderContainer'];
35
            $streamContainer = $di['\ZBateson\MailMimeParser\Message\PartStreamContainer'];
36
            $partChildrenContainer = $di['\ZBateson\MailMimeParser\Message\PartChildrenContainer'];
37
            $setStream = true;
38
        }
39
        parent::__construct(
40
            $parent,
41
            $streamContainer,
42
            $partChildrenContainer
43
        );
44
        if ($setStream) {
45
            $streamFactory = $di['\ZBateson\MailMimeParser\Stream\StreamFactory'];
46
            $streamContainer->setStream($streamFactory->newMessagePartStream($this));
47
        }
48
        $this->headerContainer = $headerContainer;
49
    }
50
51
    public function getFilename()
52
    {
53
        return $this->getHeaderParameter(
54
            'Content-Disposition',
55
            'filename',
56
            $this->getHeaderParameter(
57
                'Content-Type',
58
                'name'
59
            )
60
        );
61
    }
62
63
    public function isMime()
64
    {
65
        return true;
66
    }
67
68
    public function isTextPart()
69
    {
70
        return ($this->getCharset() !== null);
71
    }
72
73
    public function getContentType($default = 'text/plain')
74
    {
75
        return trim(strtolower($this->getHeaderValue('Content-Type', $default)));
76
    }
77
78
    public function getCharset()
79
    {
80
        $charset = $this->getHeaderParameter('Content-Type', 'charset');
81
        if ($charset === null || strcasecmp($charset, 'binary') === 0) {
82
            $contentType = $this->getContentType();
83
            if ($contentType === 'text/plain' || $contentType === 'text/html') {
84
                return 'ISO-8859-1';
85
            }
86
            return null;
87
        }
88
        return trim(strtoupper($charset));
89
    }
90
91
    public function getContentDisposition($default = 'inline')
92
    {
93
        return strtolower($this->getHeaderValue('Content-Disposition', $default));
94
    }
95
96
    public function getContentTransferEncoding($default = '7bit')
97
    {
98
        static $translated = [
99
            'x-uue' => 'x-uuencode',
100
            'uue' => 'x-uuencode',
101
            'uuencode' => 'x-uuencode'
102
        ];
103
        $type = strtolower($this->getHeaderValue('Content-Transfer-Encoding', $default));
104
        if (isset($translated[$type])) {
105
            return $translated[$type];
106
        }
107
        return $type;
108
    }
109
110
    public function getContentId()
111
    {
112
        return $this->getHeaderValue('Content-ID');
113
    }
114
115
    public function isSignaturePart()
116
    {
117
        if ($this->parent === null || !$this->parent instanceof IMessage) {
118
            return false;
119
        }
120
        return $this->parent->getSignaturePart() === $this;
121
    }
122
123
    public function getHeader($name, $offset = 0)
124
    {
125
        return $this->headerContainer->get($name, $offset);
126
    }
127
128
    public function getAllHeaders()
129
    {
130
        return $this->headerContainer->getHeaderObjects();
131
    }
132
133
    public function getAllHeadersByName($name)
134
    {
135
        return $this->headerContainer->getAll($name);
136
    }
137
138
    public function getRawHeaders()
139
    {
140
        return $this->headerContainer->getHeaders();
141
    }
142
143
    public function getRawHeaderIterator()
144
    {
145
        return $this->headerContainer->getIterator();
146
    }
147
148
    public function getHeaderValue($name, $defaultValue = null)
149
    {
150
        $header = $this->getHeader($name);
151
        if ($header !== null) {
152
            return $header->getValue();
153
        }
154
        return $defaultValue;
155
    }
156
157
    public function getHeaderParameter($header, $param, $defaultValue = null)
158
    {
159
        $obj = $this->getHeader($header);
160
        if ($obj && $obj instanceof ParameterHeader) {
161
            return $obj->getValueFor($param, $defaultValue);
162
        }
163
        return $defaultValue;
164
    }
165
166
    public function setRawHeader($name, $value, $offset = 0)
167
    {
168
        $this->headerContainer->set($name, $value, $offset);
169
        $this->notify();
170
    }
171
172
    public function addRawHeader($name, $value)
173
    {
174
        $this->headerContainer->add($name, $value);
175
        $this->notify();
176
    }
177
178
    public function removeHeader($name)
179
    {
180
        $this->headerContainer->removeAll($name);
181
        $this->notify();
182
    }
183
184
    public function removeSingleHeader($name, $offset = 0)
185
    {
186
        $this->headerContainer->remove($name, $offset);
187
        $this->notify();
188
    }
189
}
190