Passed
Push — master ( a626ba...b864c0 )
by Zaahid
15:44 queued 12:03
created

Message::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 9
c 2
b 1
f 0
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.9666
cc 1
nc 1
nop 6
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 Psr\Log\LoggerInterface;
13
use ZBateson\MailMimeParser\Header\HeaderConsts;
14
use ZBateson\MailMimeParser\Message\Helper\MultipartHelper;
15
use ZBateson\MailMimeParser\Message\Helper\PrivacyHelper;
16
use ZBateson\MailMimeParser\Message\IMessagePart;
17
use ZBateson\MailMimeParser\Message\MimePart;
18
use ZBateson\MailMimeParser\Message\PartChildrenContainer;
19
use ZBateson\MailMimeParser\Message\PartFilter;
20
use ZBateson\MailMimeParser\Message\PartHeaderContainer;
21
use ZBateson\MailMimeParser\Message\PartStreamContainer;
22
23
/**
24
 * An email message.
25
 *
26
 * The message could represent a simple text email, a multipart message with
27
 * children, or a non-mime message containing UUEncoded parts.
28
 *
29
 * @author Zaahid Bateson
30
 */
31
class Message extends MimePart implements IMessage
32
{
33
    /**
34
     * @var MultipartHelper service providing functions for multipart messages.
35
     */
36
    private MultipartHelper $multipartHelper;
37
38
    /**
39
     * @var PrivacyHelper service providing functions for multipart/signed
40
     *      messages.
41
     */
42
    private PrivacyHelper $privacyHelper;
43
44 119
    public function __construct(
45
        ?LoggerInterface $logger = null,
46
        ?PartStreamContainer $streamContainer = null,
47
        ?PartHeaderContainer $headerContainer = null,
48
        ?PartChildrenContainer $partChildrenContainer = null,
49
        ?MultipartHelper $multipartHelper = null,
50
        ?PrivacyHelper $privacyHelper = null
51
    ) {
52 119
        parent::__construct(
53 119
            null,
54 119
            $logger,
55 119
            $streamContainer,
56 119
            $headerContainer,
57 119
            $partChildrenContainer
58 119
        );
59 119
        $di = MailMimeParser::getGlobalContainer();
60 119
        $this->multipartHelper = $multipartHelper ?? $di->get(MultipartHelper::class);
61 119
        $this->privacyHelper = $privacyHelper ?? $di->get(PrivacyHelper::class);
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
     */
81 1
    public static function from(mixed $resource, bool $attached) : IMessage
82
    {
83 1
        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
     */
97 20
    public function isMime() : bool
98
    {
99 20
        $contentType = $this->getHeaderValue(HeaderConsts::CONTENT_TYPE);
100 20
        $mimeVersion = $this->getHeaderValue(HeaderConsts::MIME_VERSION);
101 20
        return ($contentType !== null || $mimeVersion !== null);
102
    }
103
104 74
    public function getTextPart(int $index = 0) : ?IMessagePart
105
    {
106 74
        return $this->getPart(
107 74
            $index,
108 74
            PartFilter::fromInlineContentType('text/plain')
109 74
        );
110
    }
111
112 1
    public function getTextPartCount() : int
113
    {
114 1
        return $this->getPartCount(
115 1
            PartFilter::fromInlineContentType('text/plain')
116 1
        );
117
    }
118
119 36
    public function getHtmlPart(int $index = 0) : ?IMessagePart
120
    {
121 36
        return $this->getPart(
122 36
            $index,
123 36
            PartFilter::fromInlineContentType('text/html')
124 36
        );
125
    }
126
127 1
    public function getHtmlPartCount() : int
128
    {
129 1
        return $this->getPartCount(
130 1
            PartFilter::fromInlineContentType('text/html')
131 1
        );
132
    }
133
134 70
    public function getTextStream(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface
135
    {
136 70
        $textPart = $this->getTextPart($index);
137 70
        if ($textPart !== null) {
138 70
            return $textPart->getContentStream($charset);
139
        }
140 1
        return null;
141
    }
142
143 2
    public function getTextContent(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string
144
    {
145 2
        $part = $this->getTextPart($index);
146 2
        if ($part !== null) {
147 2
            return $part->getContent($charset);
148
        }
149 1
        return null;
150
    }
151
152 30
    public function getHtmlStream(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface
153
    {
154 30
        $htmlPart = $this->getHtmlPart($index);
155 30
        if ($htmlPart !== null) {
156 30
            return $htmlPart->getContentStream($charset);
157
        }
158 1
        return null;
159
    }
160
161 2
    public function getHtmlContent(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string
162
    {
163 2
        $part = $this->getHtmlPart($index);
164 2
        if ($part !== null) {
165 2
            return $part->getContent($charset);
166
        }
167 1
        return null;
168
    }
169
170 2
    public function setTextPart(mixed $resource, string $charset = 'UTF-8') : static
171
    {
172 2
        $this->multipartHelper
173 2
            ->setContentPartForMimeType(
174 2
                $this,
175 2
                'text/plain',
176 2
                $resource,
177 2
                $charset
178 2
            );
179 2
        return $this;
180
    }
181
182 5
    public function setHtmlPart(mixed $resource, string $charset = 'UTF-8') : static
183
    {
184 5
        $this->multipartHelper
185 5
            ->setContentPartForMimeType(
186 5
                $this,
187 5
                'text/html',
188 5
                $resource,
189 5
                $charset
190 5
            );
191 5
        return $this;
192
    }
193
194 4
    public function removeTextPart(int $index = 0) : bool
195
    {
196 4
        return $this->multipartHelper
197 4
            ->removePartByMimeType(
198 4
                $this,
199 4
                'text/plain',
200 4
                $index
201 4
            );
202
    }
203
204 1
    public function removeAllTextParts(bool $moveRelatedPartsBelowMessage = true) : bool
205
    {
206 1
        return $this->multipartHelper
207 1
            ->removeAllContentPartsByMimeType(
208 1
                $this,
209 1
                'text/plain',
210 1
                $moveRelatedPartsBelowMessage
211 1
            );
212
    }
213
214 3
    public function removeHtmlPart(int $index = 0) : bool
215
    {
216 3
        return $this->multipartHelper
217 3
            ->removePartByMimeType(
218 3
                $this,
219 3
                'text/html',
220 3
                $index
221 3
            );
222
    }
223
224 2
    public function removeAllHtmlParts(bool $moveRelatedPartsBelowMessage = true) : bool
225
    {
226 2
        return $this->multipartHelper
227 2
            ->removeAllContentPartsByMimeType(
228 2
                $this,
229 2
                'text/html',
230 2
                $moveRelatedPartsBelowMessage
231 2
            );
232
    }
233
234 5
    public function getAttachmentPart(int $index) : ?IMessagePart
235
    {
236 5
        return $this->getPart(
237 5
            $index,
238 5
            PartFilter::fromAttachmentFilter()
239 5
        );
240
    }
241
242 57
    public function getAllAttachmentParts() : array
243
    {
244 57
        return $this->getAllParts(
245 57
            PartFilter::fromAttachmentFilter()
246 57
        );
247
    }
248
249 57
    public function getAttachmentCount() : int
250
    {
251 57
        return \count($this->getAllAttachmentParts());
252
    }
253
254 5
    public function addAttachmentPart(mixed $resource, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : static
255
    {
256 5
        $this->multipartHelper
257 5
            ->createAndAddPartForAttachment(
258 5
                $this,
259 5
                $resource,
260 5
                $mimeType,
261 5
                (\strcasecmp($disposition, 'inline') === 0) ? 'inline' : 'attachment',
262 5
                $filename,
263 5
                $encoding
264 5
            );
265 5
        return $this;
266
    }
267
268 5
    public function addAttachmentPartFromFile(string $filePath, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : static
269
    {
270 5
        $handle = Psr7\Utils::streamFor(\fopen($filePath, 'r'));
271 5
        if ($filename === null) {
272 4
            $filename = \basename($filePath);
273
        }
274 5
        $this->addAttachmentPart($handle, $mimeType, $filename, $disposition, $encoding);
275 5
        return $this;
276
    }
277
278 3
    public function removeAttachmentPart(int $index) : static
279
    {
280 3
        $part = $this->getAttachmentPart($index);
281 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

281
        $this->removePart(/** @scrutinizer ignore-type */ $part);
Loading history...
282 3
        return $this;
283
    }
284
285 1
    public function getSignedMessageStream() : ?StreamInterface
286
    {
287 1
        return $this
288 1
            ->privacyHelper
289 1
            ->getSignedMessageStream($this);
290
    }
291
292 15
    public function getSignedMessageAsString() : ?string
293
    {
294 15
        return $this
295 15
            ->privacyHelper
296 15
            ->getSignedMessageAsString($this);
297
    }
298
299 62
    public function getSignaturePart() : ?IMessagePart
300
    {
301 62
        if (\strcasecmp($this->getContentType(), 'multipart/signed') === 0) {
302 19
            return $this->getChild(1);
303
        }
304 43
        return null;
305
    }
306
307 10
    public function setAsMultipartSigned(string $micalg, string $protocol) : static
308
    {
309 10
        $this->privacyHelper
310 10
            ->setMessageAsMultipartSigned($this, $micalg, $protocol);
311 10
        return $this;
312
    }
313
314 10
    public function setSignature(string $body) : static
315
    {
316 10
        $this->privacyHelper
317 10
            ->setSignature($this, $body);
318 10
        return $this;
319
    }
320
321 73
    public function getMessageId() : ?string
322
    {
323 73
        return $this->getHeaderValue(HeaderConsts::MESSAGE_ID);
324
    }
325
326 73
    public function getErrorLoggingContextName() : string
327
    {
328 73
        $params = '';
329 73
        if (!empty($this->getMessageId())) {
330 73
            $params .= ', message-id=' . $this->getContentId();
331
        }
332 73
        $params .= ', content-type=' . $this->getContentType();
333 73
        $nsClass = static::class;
334 73
        $class = \substr($nsClass, (\strrpos($nsClass, '\\') ?? -1) + 1);
335 73
        return $class . '(' . \spl_object_id($this) . $params . ')';
336
    }
337
}
338