Passed
Push — master ( 24955d...c95513 )
by Zaahid
06:27 queued 13s
created

Message::removeHtmlPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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;
8
9
use ZBateson\MailMimeParser\Header\HeaderConsts;
10
use ZBateson\MailMimeParser\Message\PartHeaderContainer;
11
use ZBateson\MailMimeParser\Message\MimePart;
12
use ZBateson\MailMimeParser\Message\PartChildrenContainer;
13
use ZBateson\MailMimeParser\Message\PartFilter;
14
use ZBateson\MailMimeParser\Message\PartStreamContainer;
15
use ZBateson\MailMimeParser\Message\Helper\MultipartHelper;
16
use ZBateson\MailMimeParser\Message\Helper\PrivacyHelper;
17
use Psr\Http\Message\StreamInterface;
18
use GuzzleHttp\Psr7;
19
20
/**
21
 * An email message.
22
 *
23
 * The message could represent a simple text email, a multipart message with
24
 * children, or a non-mime message containing UUEncoded parts.
25
 *
26
 * @author Zaahid Bateson
27
 */
28
class Message extends MimePart implements IMessage
29
{
30
    /**
31
     * @var MultipartHelper service providing functions for multipart messages.
32
     */
33
    private $multipartHelper;
34
35
    /**
36
     * @var PrivacyHelper service providing functions for multipart/signed
37
     *      messages.
38
     */
39
    private $privacyHelper;
40
41 116
    public function __construct(
42
        PartStreamContainer $streamContainer = null,
43
        PartHeaderContainer $headerContainer = null,
44
        PartChildrenContainer $partChildrenContainer = null,
45
        MultipartHelper $multipartHelper = null,
46
        PrivacyHelper $privacyHelper = null
47
    ) {
48 116
        parent::__construct(
49
            null,
50
            $streamContainer,
51
            $headerContainer,
52
            $partChildrenContainer
53
        );
54 116
        if ($multipartHelper === null || $privacyHelper === null) {
55
            $di = MailMimeParser::getDependencyContainer();
56
            $multipartHelper = $di['ZBateson\MailMimeParser\Message\Helper\MultipartHelper'];
57
            $privacyHelper = $di['ZBateson\MailMimeParser\Message\Helper\PrivacyHelper'];
58
        }
59 116
        $this->multipartHelper = $multipartHelper;
60 116
        $this->privacyHelper = $privacyHelper;
61
    }
62
63
    /**
64
     * Convenience method to parse a handle or string into an IMessage without
65
     * requiring including MailMimeParser, instantiating it, and calling parse.
66
     *
67
     * If the passed $resource is a resource handle or StreamInterface, the
68
     * resource must remain open while the returned IMessage object exists.
69
     * Pass true as the second argument to have the resource attached to the
70
     * IMessage and closed for you when it's destroyed, or pass false to
71
     * manually close it if it should remain open after the IMessage object is
72
     * destroyed.
73
     *
74
     * @param resource|StreamInterface|string $resource The resource handle to
75
     *        the input stream of the mime message, or a string containing a
76
     *        mime message.
77
     * @param bool $attached pass true to have it attached to the returned
78
     *        IMessage and destroyed with it.
79
     * @return IMessage
80
     */
81 1
    public static function from($resource, $attached)
82
    {
83
        static $mmp = null;
84 1
        if ($mmp === null) {
85 1
            $mmp = new MailMimeParser();
86
        }
87 1
        return $mmp->parse($resource, $attached);
88
    }
89
90
    /**
91
     * Returns true if the current part is a mime part.
92
     *
93
     * The message is considered 'mime' if it has either a Content-Type or
94
     * MIME-Version header defined.
95
     *
96
     * @return bool
97
     */
98 19
    public function isMime()
99
    {
100 19
        $contentType = $this->getHeaderValue(HeaderConsts::CONTENT_TYPE);
101 19
        $mimeVersion = $this->getHeaderValue(HeaderConsts::MIME_VERSION);
102 19
        return ($contentType !== null || $mimeVersion !== null);
103
    }
104
105 72
    public function getTextPart($index = 0)
106
    {
107 72
        return $this->getPart(
108
            $index,
109 72
            PartFilter::fromInlineContentType('text/plain')
110
        );
111
    }
112
113 1
    public function getTextPartCount()
114
    {
115 1
        return $this->getPartCount(
116 1
            PartFilter::fromInlineContentType('text/plain')
117
        );
118
    }
119
120 36
    public function getHtmlPart($index = 0)
121
    {
122 36
        return $this->getPart(
123
            $index,
124 36
            PartFilter::fromInlineContentType('text/html')
125
        );
126
    }
127
128 1
    public function getHtmlPartCount()
129
    {
130 1
        return $this->getPartCount(
131 1
            PartFilter::fromInlineContentType('text/html')
132
        );
133
    }
134
135 68
    public function getTextStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
136
    {
137 68
        $textPart = $this->getTextPart($index);
138 68
        if ($textPart !== null) {
139 68
            return $textPart->getContentStream($charset);
140
        }
141 1
        return null;
142
    }
143
144 2
    public function getTextContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
145
    {
146 2
        $part = $this->getTextPart($index);
147 2
        if ($part !== null) {
148 2
            return $part->getContent($charset);
149
        }
150 1
        return null;
151
    }
152
153 30
    public function getHtmlStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
154
    {
155 30
        $htmlPart = $this->getHtmlPart($index);
156 30
        if ($htmlPart !== null) {
157 30
            return $htmlPart->getContentStream($charset);
158
        }
159 1
        return null;
160
    }
161
162 2
    public function getHtmlContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
163
    {
164 2
        $part = $this->getHtmlPart($index);
165 2
        if ($part !== null) {
166 2
            return $part->getContent($charset);
167
        }
168 1
        return null;
169
    }
170
171 2
    public function setTextPart($resource, $charset = 'UTF-8')
172
    {
173 2
        $this->multipartHelper
174 2
            ->setContentPartForMimeType(
175
                $this, 'text/plain', $resource, $charset
176
            );
177
    }
178
179 5
    public function setHtmlPart($resource, $charset = 'UTF-8')
180
    {
181 5
        $this->multipartHelper
182 5
            ->setContentPartForMimeType(
183
                $this, 'text/html', $resource, $charset
184
            );
185
    }
186
187 4
    public function removeTextPart($index = 0)
188
    {
189 4
        return $this->multipartHelper
190 4
            ->removePartByMimeType(
191
                $this, 'text/plain', $index
192
            );
193
    }
194
195 1
    public function removeAllTextParts($moveRelatedPartsBelowMessage = true)
196
    {
197 1
        return $this->multipartHelper
198 1
            ->removeAllContentPartsByMimeType(
199
                $this, 'text/plain', $moveRelatedPartsBelowMessage
200
            );
201
    }
202
203 3
    public function removeHtmlPart($index = 0)
204
    {
205 3
        return $this->multipartHelper
206 3
            ->removePartByMimeType(
207
                $this, 'text/html', $index
208
            );
209
    }
210
211 2
    public function removeAllHtmlParts($moveRelatedPartsBelowMessage = true)
212
    {
213 2
        return $this->multipartHelper
214 2
            ->removeAllContentPartsByMimeType(
215
                $this, 'text/html', $moveRelatedPartsBelowMessage
216
            );
217
    }
218
219 5
    public function getAttachmentPart($index)
220
    {
221 5
        return $this->getPart(
222
            $index,
223 5
            PartFilter::fromAttachmentFilter()
224
        );
225
    }
226
227 57
    public function getAllAttachmentParts()
228
    {
229 57
        return $this->getAllParts(
230 57
            PartFilter::fromAttachmentFilter()
231
        );
232
    }
233
234 57
    public function getAttachmentCount()
235
    {
236 57
        return count($this->getAllAttachmentParts());
237
    }
238
239 5
    public function addAttachmentPart($resource, $mimeType, $filename = null, $disposition = 'attachment', $encoding = 'base64')
240
    {
241 5
        $this->multipartHelper
242 5
            ->createAndAddPartForAttachment(
243
                $this,
244
                $resource,
245
                $mimeType,
246 5
                (strcasecmp($disposition, 'inline') === 0) ? 'inline' : 'attachment',
247
                $filename,
248
                $encoding
249
            );
250
    }
251
252 5
    public function addAttachmentPartFromFile($filePath, $mimeType, $filename = null, $disposition = 'attachment', $encoding = 'base64')
253
    {
254 5
        $handle = Psr7\Utils::streamFor(fopen($filePath, 'r'));
255 5
        if ($filename === null) {
256 4
            $filename = basename($filePath);
257
        }
258 5
        $this->addAttachmentPart($handle, $mimeType, $filename, $disposition, $encoding);
259
    }
260
261 3
    public function removeAttachmentPart($index)
262
    {
263 3
        $part = $this->getAttachmentPart($index);
264 3
        $this->removePart($part);
0 ignored issues
show
Bug introduced by
It seems like $part can also be of type null; however, parameter $part of ZBateson\MailMimeParser\...MultiPart::removePart() does only seem to accept ZBateson\MailMimeParser\Message\IMessagePart, 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

264
        $this->removePart(/** @scrutinizer ignore-type */ $part);
Loading history...
265
    }
266
267 1
    public function getSignedMessageStream()
268
    {
269
        return $this
270 1
            ->privacyHelper
271 1
            ->getSignedMessageStream($this);
272
    }
273
274 14
    public function getSignedMessageAsString()
275
    {
276
        return $this
277 14
            ->privacyHelper
278 14
            ->getSignedMessageAsString($this);
279
    }
280
281 61
    public function getSignaturePart()
282
    {
283 61
        if (strcasecmp($this->getContentType(), 'multipart/signed') === 0) {
284 18
            return $this->getChild(1);
285
        } else {
286 43
            return null;
287
        }
288
    }
289
290 9
    public function setAsMultipartSigned($micalg, $protocol)
291
    {
292 9
        $this->privacyHelper
293 9
            ->setMessageAsMultipartSigned($this, $micalg, $protocol);
294
    }
295
296 9
    public function setSignature($body)
297
    {
298 9
        $this->privacyHelper
299 9
            ->setSignature($this, $body);
300
    }
301
}
302