Passed
Pull Request — master (#213)
by
unknown
03:11
created

Message::removeTextPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
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
8
namespace ZBateson\MailMimeParser;
9
10
use GuzzleHttp\Psr7;
11
use Psr\Http\Message\StreamInterface;
12
use ZBateson\MailMimeParser\Header\HeaderConsts;
13
use ZBateson\MailMimeParser\Message\Helper\MultipartHelper;
14
use ZBateson\MailMimeParser\Message\Helper\PrivacyHelper;
15
use ZBateson\MailMimeParser\Message\MimePart;
16
use ZBateson\MailMimeParser\Message\PartChildrenContainer;
17
use ZBateson\MailMimeParser\Message\PartFilter;
18
use ZBateson\MailMimeParser\Message\PartHeaderContainer;
19
use ZBateson\MailMimeParser\Message\PartStreamContainer;
20
21
/**
22
 * An email message.
23
 *
24
 * The message could represent a simple text email, a multipart message with
25
 * children, or a non-mime message containing UUEncoded parts.
26
 *
27
 * @author Zaahid Bateson
28
 */
29
class Message extends MimePart implements IMessage
30
{
31
    /**
32
     * @var MultipartHelper service providing functions for multipart messages.
33
     */
34
    private $multipartHelper;
35
36
    /**
37
     * @var PrivacyHelper service providing functions for multipart/signed
38
     *      messages.
39
     */
40
    private $privacyHelper;
41
42 116
    public function __construct(
43
        ?PartStreamContainer $streamContainer = null,
44
        ?PartHeaderContainer $headerContainer = null,
45
        ?PartChildrenContainer $partChildrenContainer = null,
46
        ?MultipartHelper $multipartHelper = null,
47
        ?PrivacyHelper $privacyHelper = null
48
    ) {
49 116
        parent::__construct(
50 116
            null,
51 116
            $streamContainer,
52 116
            $headerContainer,
53 116
            $partChildrenContainer
54 116
        );
55 116
        if ($multipartHelper === null || $privacyHelper === null) {
56
            $di = MailMimeParser::getDependencyContainer();
57
            $multipartHelper = $di[\ZBateson\MailMimeParser\Message\Helper\MultipartHelper::class];
58
            $privacyHelper = $di[\ZBateson\MailMimeParser\Message\Helper\PrivacyHelper::class];
59
        }
60 116
        $this->multipartHelper = $multipartHelper;
61 116
        $this->privacyHelper = $privacyHelper;
62
    }
63
64
    /**
65
     * Convenience method to parse a handle or string into an IMessage without
66
     * requiring including MailMimeParser, instantiating it, and calling parse.
67
     *
68
     * If the passed $resource is a resource handle or StreamInterface, the
69
     * resource must remain open while the returned IMessage object exists.
70
     * Pass true as the second argument to have the resource attached to the
71
     * IMessage and closed for you when it's destroyed, or pass false to
72
     * manually close it if it should remain open after the IMessage object is
73
     * destroyed.
74
     *
75
     * @param resource|StreamInterface|string $resource The resource handle to
76
     *        the input stream of the mime message, or a string containing a
77
     *        mime message.
78
     * @param bool $attached pass true to have it attached to the returned
79
     *        IMessage and destroyed with it.
80
     * @return IMessage
81
     */
82 1
    public static function from($resource, $attached)
83
    {
84 1
        static $mmp = null;
85 1
        if ($mmp === null) {
86 1
            $mmp = new MailMimeParser();
87
        }
88 1
        return $mmp->parse($resource, $attached);
89
    }
90
91
    /**
92
     * Returns true if the current part is a mime part.
93
     *
94
     * The message is considered 'mime' if it has either a Content-Type or
95
     * MIME-Version header defined.
96
     *
97
     */
98 19
    public function isMime() : bool
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 72
            $index,
109 72
            PartFilter::fromInlineContentType('text/plain')
110 72
        );
111
    }
112
113 1
    public function getTextPartCount()
114
    {
115 1
        return $this->getPartCount(
116 1
            PartFilter::fromInlineContentType('text/plain')
117 1
        );
118
    }
119
120 36
    public function getHtmlPart($index = 0)
121
    {
122 36
        return $this->getPart(
123 36
            $index,
124 36
            PartFilter::fromInlineContentType('text/html')
125 36
        );
126
    }
127
128 1
    public function getHtmlPartCount()
129
    {
130 1
        return $this->getPartCount(
131 1
            PartFilter::fromInlineContentType('text/html')
132 1
        );
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, string $charset = 'UTF-8') : self
172
    {
173 2
        $this->multipartHelper
174 2
            ->setContentPartForMimeType(
175 2
                $this,
176 2
                'text/plain',
177 2
                $resource,
178 2
                $charset
179 2
            );
180 2
        return $this;
181
    }
182
183 5
    public function setHtmlPart($resource, string $charset = 'UTF-8') : self
184
    {
185 5
        $this->multipartHelper
186 5
            ->setContentPartForMimeType(
187 5
                $this,
188 5
                'text/html',
189 5
                $resource,
190 5
                $charset
191 5
            );
192 5
        return $this;
193
    }
194
195 4
    public function removeTextPart(int $index = 0) : bool
196
    {
197 4
        return $this->multipartHelper
198 4
            ->removePartByMimeType(
199 4
                $this,
200 4
                'text/plain',
201 4
                $index
202 4
            );
203
    }
204
205 1
    public function removeAllTextParts(bool $moveRelatedPartsBelowMessage = true) : bool
206
    {
207 1
        return $this->multipartHelper
208 1
            ->removeAllContentPartsByMimeType(
209 1
                $this,
210 1
                'text/plain',
211 1
                $moveRelatedPartsBelowMessage
212 1
            );
213
    }
214
215 3
    public function removeHtmlPart(int $index = 0) : bool
216
    {
217 3
        return $this->multipartHelper
218 3
            ->removePartByMimeType(
219 3
                $this,
220 3
                'text/html',
221 3
                $index
222 3
            );
223
    }
224
225 2
    public function removeAllHtmlParts(bool $moveRelatedPartsBelowMessage = true) : bool
226
    {
227 2
        return $this->multipartHelper
228 2
            ->removeAllContentPartsByMimeType(
229 2
                $this,
230 2
                'text/html',
231 2
                $moveRelatedPartsBelowMessage
232 2
            );
233
    }
234
235 5
    public function getAttachmentPart(int $index)
236
    {
237 5
        return $this->getPart(
238 5
            $index,
239 5
            PartFilter::fromAttachmentFilter()
240 5
        );
241
    }
242
243 57
    public function getAllAttachmentParts()
244
    {
245 57
        return $this->getAllParts(
246 57
            PartFilter::fromAttachmentFilter()
247 57
        );
248
    }
249
250 57
    public function getAttachmentCount() : int
251
    {
252 57
        return \count($this->getAllAttachmentParts());
253
    }
254
255 5
    public function addAttachmentPart($resource, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : self
256
    {
257 5
        $this->multipartHelper
258 5
            ->createAndAddPartForAttachment(
259 5
                $this,
260 5
                $resource,
261 5
                $mimeType,
262 5
                (\strcasecmp($disposition, 'inline') === 0) ? 'inline' : 'attachment',
263 5
                $filename,
264 5
                $encoding
265 5
            );
266 5
        return $this;
267
    }
268
269 5
    public function addAttachmentPartFromFile($filePath, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : self
270
    {
271 5
        $handle = Psr7\Utils::streamFor(\fopen($filePath, 'r'));
272 5
        if ($filename === null) {
273 4
            $filename = \basename($filePath);
274
        }
275 5
        $this->addAttachmentPart($handle, $mimeType, $filename, $disposition, $encoding);
276 5
        return $this;
277
    }
278
279 3
    public function removeAttachmentPart(int $index) : self
280
    {
281 3
        $part = $this->getAttachmentPart($index);
282 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

282
        $this->removePart(/** @scrutinizer ignore-type */ $part);
Loading history...
283 3
        return $this;
284
    }
285
286 1
    public function getSignedMessageStream()
287
    {
288 1
        return $this
289 1
            ->privacyHelper
290 1
            ->getSignedMessageStream($this);
291
    }
292
293 14
    public function getSignedMessageAsString()
294
    {
295 14
        return $this
296 14
            ->privacyHelper
297 14
            ->getSignedMessageAsString($this);
298
    }
299
300 61
    public function getSignaturePart()
301
    {
302 61
        if (\strcasecmp($this->getContentType(), 'multipart/signed') === 0) {
303 18
            return $this->getChild(1);
304
        }
305 43
            return null;
306
307
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
308
309 9
    public function setAsMultipartSigned(string $micalg, string $protocol) : self
310
    {
311 9
        $this->privacyHelper
312 9
            ->setMessageAsMultipartSigned($this, $micalg, $protocol);
313 9
        return $this;
314
    }
315
316 9
    public function setSignature(string $body) : self
317
    {
318 9
        $this->privacyHelper
319 9
            ->setSignature($this, $body);
320 9
        return $this;
321
    }
322
}
323