Passed
Pull Request — master (#171)
by Zaahid
07:14 queued 03:53
created

Message::getHtmlResourceHandle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
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 GuzzleHttp\Psr7;
10
use ZBateson\MailMimeParser\Header\HeaderConsts;
11
use ZBateson\MailMimeParser\Message\PartHeaderContainer;
12
use ZBateson\MailMimeParser\Message\MimePart;
13
use ZBateson\MailMimeParser\Message\PartChildrenContainer;
14
use ZBateson\MailMimeParser\Message\PartFilter;
15
use ZBateson\MailMimeParser\Message\PartStreamContainer;
16
use ZBateson\MailMimeParser\Message\Helper\MultipartHelper;
17
use ZBateson\MailMimeParser\Message\Helper\PrivacyHelper;
18
19
/**
20
 * An email message.
21
 *
22
 * The message could represent a simple text email, a multipart message with
23
 * children, or a non-mime message containing UUEncoded parts.
24
 *
25
 * @author Zaahid Bateson
26
 */
27
class Message extends MimePart implements IMessage
28
{
29
    /**
30
     * @var MultipartHelper service providing functions for multipart messages.
31
     */
32
    private $multipartHelper;
33
34
    /**
35
     * @var PrivacyHelper service providing functions for multipart/signed
36
     *      messages.
37
     */
38
    private $privacyHelper;
39
40 116
    public function __construct(
41
        PartStreamContainer $streamContainer = null,
42
        PartHeaderContainer $headerContainer = null,
43
        PartChildrenContainer $partChildrenContainer = null,
44
        MultipartHelper $multipartHelper = null,
45
        PrivacyHelper $privacyHelper = null
46
    ) {
47 116
        parent::__construct(
48 116
            null,
49 116
            $streamContainer,
50 116
            $headerContainer,
51 116
            $partChildrenContainer
52
        );
53 116
        if ($multipartHelper === null || $privacyHelper === null) {
54
            $multipartHelper = $di['ZBateson\MailMimeParser\Message\Helper\MultipartHelper'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $di seems to be never defined.
Loading history...
55
            $privacyHelper = $di['ZBateson\MailMimeParser\Message\Helper\PrivacyHelper'];
56
        }
57 116
        $this->multipartHelper = $multipartHelper;
58 116
        $this->privacyHelper = $privacyHelper;
59 116
    }
60
61
    /**
62
     * Convenience method to parse a handle or string into an IMessage without
63
     * requiring including MailMimeParser, instantiating it, and calling parse.
64
     *
65
     * If the passed $resource is a resource handle or StreamInterface, the
66
     * resource must remain open while the returned IMessage object exists.
67
     * Pass true as the second argument to have the resource attached to the
68
     * IMessage and closed for you when it's destroyed, or pass false to
69
     * manually close it if it should remain open after the IMessage object is
70
     * destroyed.
71
     *
72
     * @param resource|StreamInterface|string $resource The resource handle to
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\StreamInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
     *        the input stream of the mime message, or a string containing a
74
     *        mime message.
75
     * @param bool $attached pass true to have it attached to the returned
76
     *        IMessage and destroyed with it.
77
     * @return IMessage
78
     */
79 1
    public static function from($resource, $attached)
80
    {
81 1
        static $mmp = null;
82 1
        if ($mmp === null) {
83 1
            $mmp = new MailMimeParser();
84
        }
85 1
        return $mmp->parse($resource, $attached);
86
    }
87
88
    /**
89
     * Returns true if the current part is a mime part.
90
     *
91
     * The message is considered 'mime' if it has either a Content-Type or
92
     * MIME-Version header defined.
93
     *
94
     * @return bool
95
     */
96 19
    public function isMime()
97
    {
98 19
        $contentType = $this->getHeaderValue(HeaderConsts::CONTENT_TYPE);
99 19
        $mimeVersion = $this->getHeaderValue(HeaderConsts::MIME_VERSION);
100 19
        return ($contentType !== null || $mimeVersion !== null);
101
    }
102
103 72
    public function getTextPart($index = 0)
104
    {
105 72
        return $this->getPart(
106 72
            $index,
107 72
            PartFilter::fromInlineContentType('text/plain')
108
        );
109
    }
110
111 1
    public function getTextPartCount()
112
    {
113 1
        return $this->getPartCount(
114 1
            PartFilter::fromInlineContentType('text/plain')
115
        );
116
    }
117
118 36
    public function getHtmlPart($index = 0)
119
    {
120 36
        return $this->getPart(
121 36
            $index,
122 36
            PartFilter::fromInlineContentType('text/html')
123
        );
124
    }
125
126 1
    public function getHtmlPartCount()
127
    {
128 1
        return $this->getPartCount(
129 1
            PartFilter::fromInlineContentType('text/html')
130
        );
131
    }
132
133 68
    public function getTextStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
134
    {
135 68
        $textPart = $this->getTextPart($index);
136 68
        if ($textPart !== null) {
137 68
            return $textPart->getContentStream($charset);
138
        }
139 1
        return null;
140
    }
141
142 2
    public function getTextContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
143
    {
144 2
        $part = $this->getTextPart($index);
145 2
        if ($part !== null) {
146 2
            return $part->getContent($charset);
147
        }
148 1
        return null;
149
    }
150
151 30
    public function getHtmlStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
152
    {
153 30
        $htmlPart = $this->getHtmlPart($index);
154 30
        if ($htmlPart !== null) {
155 30
            return $htmlPart->getContentStream($charset);
156
        }
157 1
        return null;
158
    }
159
160 2
    public function getHtmlContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
161
    {
162 2
        $part = $this->getHtmlPart($index);
163 2
        if ($part !== null) {
164 2
            return $part->getContent($charset);
165
        }
166 1
        return null;
167
    }
168
169 2
    public function setTextPart($resource, $charset = 'UTF-8')
170
    {
171 2
        $this->multipartHelper
172 2
            ->setContentPartForMimeType(
173 2
                $this, 'text/plain', $resource, $charset
174
            );
175 2
    }
176
177 5
    public function setHtmlPart($resource, $charset = 'UTF-8')
178
    {
179 5
        $this->multipartHelper
180 5
            ->setContentPartForMimeType(
181 5
                $this, 'text/html', $resource, $charset
182
            );
183 5
    }
184
185 4
    public function removeTextPart($index = 0)
186
    {
187 4
        return $this->multipartHelper
188 4
            ->removePartByMimeType(
189 4
                $this, 'text/plain', $index
190
            );
191
    }
192
193 1
    public function removeAllTextParts($moveRelatedPartsBelowMessage = true)
194
    {
195 1
        return $this->multipartHelper
196 1
            ->removeAllContentPartsByMimeType(
197 1
                $this, 'text/plain', $moveRelatedPartsBelowMessage
198
            );
199
    }
200
201 3
    public function removeHtmlPart($index = 0)
202
    {
203 3
        return $this->multipartHelper
204 3
            ->removePartByMimeType(
205 3
                $this, 'text/html', $index
206
            );
207
    }
208
209 2
    public function removeAllHtmlParts($moveRelatedPartsBelowMessage = true)
210
    {
211 2
        return $this->multipartHelper
212 2
            ->removeAllContentPartsByMimeType(
213 2
                $this, 'text/html', $moveRelatedPartsBelowMessage
214
            );
215
    }
216
217 5
    public function getAttachmentPart($index)
218
    {
219 5
        return $this->getPart(
220 5
            $index,
221 5
            PartFilter::fromAttachmentFilter()
222
        );
223
    }
224
225 57
    public function getAllAttachmentParts()
226
    {
227 57
        return $this->getAllParts(
228 57
            PartFilter::fromAttachmentFilter()
229
        );
230
    }
231
232 57
    public function getAttachmentCount()
233
    {
234 57
        return count($this->getAllAttachmentParts());
235
    }
236
237 5
    public function addAttachmentPart($resource, $mimeType, $filename = null, $disposition = 'attachment', $encoding = 'base64')
238
    {
239 5
        $this->multipartHelper
240 5
            ->createAndAddPartForAttachment(
241 5
                $this,
242 5
                $resource,
243 5
                $mimeType,
244 5
                (strcasecmp($disposition, 'inline') === 0) ? 'inline' : 'attachment',
245 5
                $filename,
246 5
                $encoding
247
            );
248 5
    }
249
250 5
    public function addAttachmentPartFromFile($filePath, $mimeType, $filename = null, $disposition = 'attachment', $encoding = 'base64')
251
    {
252 5
        $handle = Psr7\Utils::streamFor(fopen($filePath, 'r'));
253 5
        if ($filename === null) {
254 4
            $filename = basename($filePath);
255
        }
256 5
        $this->addAttachmentPart($handle, $mimeType, $filename, $disposition, $encoding);
257 5
    }
258
259 3
    public function removeAttachmentPart($index)
260
    {
261 3
        $part = $this->getAttachmentPart($index);
262 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

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