Test Failed
Push — 1.0.0 ( dd1332...30b11b )
by Zaahid
02:32
created

getMessageStringForSignatureVerification()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 0
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 3
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 ZBateson\MailMimeParser\Message\Part\MimePart;
10
use ZBateson\MailMimeParser\Message\PartFilter;
11
use GuzzleHttp\Psr7;
12
13
/**
14
 * A parsed mime message with optional mime parts depending on its type.
15
 * 
16
 * A mime message may have any number of mime parts, and each part may have any
17
 * number of sub-parts, etc...
18
 *
19
 * @author Zaahid Bateson
20
 */
21
class Message extends MimePart
22
{
23
    /**
24
     * Overridden to close the main message StreamInterface used by this
25
     * message.
26
     */
27 6
    public function __destruct()
28
    {
29 6
        if ($this->stream !== null) {
30 6
            $this->stream->close();
31
        }
32 6
    }
33
34
    /**
35
     * Convenience method to parse a handle or string into a Message without
36
     * requiring including MailMimeParser, instantiating it, and calling parse.
37
     * 
38
     * @param resource|string $handleOrString the resource handle to the input
39
     *        stream of the mime message, or a string containing a mime message
40
     */
41
    public static function from($handleOrString)
42
    {
43
        $mmp = new MailMimeParser();
44
        return $mmp->parse($handleOrString);
45
    }
46
47
    /**
48
     * Returns the text/plain part at the given index (or null if not found.)
49
     * 
50
     * @param int $index
51
     * @return \ZBateson\MailMimeParser\Message\Part\MimePart
52
     */
53 1
    public function getTextPart($index = 0)
54
    {
55 1
        return $this->getPart(
56 1
            $index,
57 1
            $this->partFilterFactory->newFilterFromInlineContentType('text/plain')
58
        );
59
    }
60
    
61
    /**
62
     * Returns the number of text/plain parts in this message.
63
     * 
64
     * @return int
65
     */
66 1
    public function getTextPartCount()
67
    {
68 1
        return $this->getPartCount(
69 1
            $this->partFilterFactory->newFilterFromInlineContentType('text/plain')
70
        );
71
    }
72
    
73
    /**
74
     * Returns the text/html part at the given index (or null if not found.)
75
     * 
76
     * @param $index
77
     * @return \ZBateson\MailMimeParser\Message\Part\MimePart
78
     */
79 1
    public function getHtmlPart($index = 0)
80
    {
81 1
        return $this->getPart(
82 1
            $index,
83 1
            $this->partFilterFactory->newFilterFromInlineContentType('text/html')
84
        );
85
    }
86
    
87
    /**
88
     * Returns the number of text/html parts in this message.
89
     * 
90
     * @return int
91
     */
92 1
    public function getHtmlPartCount()
93
    {
94 1
        return $this->getPartCount(
95 1
            $this->partFilterFactory->newFilterFromInlineContentType('text/html')
96
        );
97
    }
98
99
    /**
100
     * Returns the attachment part at the given 0-based index, or null if none
101
     * is set.
102
     * 
103
     * @param int $index
104
     * @return MessagePart
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\MessagePart 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...
105
     */
106 2
    public function getAttachmentPart($index)
107
    {
108 2
        $attachments = $this->getAllAttachmentParts();
109 2
        if (!isset($attachments[$index])) {
110 1
            return null;
111 1
        }
112 1
        return $attachments[$index];
113 1
    }
114
115 1
    /**
116
     * Returns all attachment parts.
117 1
     * 
118
     * "Attachments" are any non-multipart, non-signature and any text or html
119
     * html part witha Content-Disposition set to  'attachment'.
120
     * 
121
     * @return MessagePart[]
122
     */
123
    public function getAllAttachmentParts()
124
    {
125
        $parts = $this->getAllParts(
126
            $this->partFilterFactory->newFilterFromArray([
127
                'multipart' => PartFilter::FILTER_EXCLUDE
128
            ])
129
        );
130
        return array_values(array_filter(
131
            $parts,
132
            function ($part) {
133
                return !(
134
                    $part->isTextPart()
135
                    && $part->getContentDisposition() === 'inline'
136
                );
137
            }
138
        ));
139
    }
140
141
    /**
142
     * Returns the number of attachments available.
143
     * 
144
     * @return int
145
     */
146
    public function getAttachmentCount()
147
    {
148
        return count($this->getAllAttachmentParts());
149 1
    }
150
151 1
    /**
152 1
     * Returns a resource handle where the 'inline' text/plain content at the
153 1
     * passed $index can be read or null if unavailable.
154
     * 
155 1
     * @param int $index
156
     * @param string $charset
157
     * @return resource
158
     */
159
    public function getTextStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
160
    {
161
        $textPart = $this->getTextPart($index);
162
        if ($textPart !== null) {
163
            return $textPart->getContentResourceHandle($charset);
164
        }
165
        return null;
166 1
    }
167
168 1
    /**
169 1
     * Returns the content of the inline text/plain part at the given index.
170 1
     * 
171
     * Reads the entire stream content into a string and returns it.  Returns
172
     * null if the message doesn't have an inline text part.
173 1
     * 
174 1
     * @param int $index
175 1
     * @param string $charset
176
     * @return string
177 1
     */
178 1
    public function getTextContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
179
    {
180 1
        $part = $this->getTextPart($index);
181
        if ($part !== null) {
182
            return $part->getContent($charset);
183
        }
184
        return null;
185
    }
186
187
    /**
188
     * Returns a resource handle where the 'inline' text/html content at the
189 1
     * passed $index can be read or null if unavailable.
190
     * 
191 1
     * @param int $index
192
     * @param string $charset
193
     * @return resource
194
     */
195
    public function getHtmlStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
196
    {
197
        $htmlPart = $this->getHtmlPart($index);
198
        if ($htmlPart !== null) {
199
            return $htmlPart->getContentResourceHandle($charset);
200
        }
201
        return null;
202 1
    }
203
204 1
    /**
205 1
     * Returns the content of the inline text/html part at the given index.
206 1
     * 
207
     * Reads the entire stream content into a string and returns it.  Returns
208 1
     * null if the message doesn't have an inline html part.
209
     * 
210
     * @param int $index
211
     * @param string $charset
212
     * @return string
213
     */
214
    public function getHtmlContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET)
215
    {
216
        $part = $this->getHtmlPart($index);
217
        if ($part !== null) {
218
            return $part->getContent($charset);
219
        }
220
        return null;
221 1
    }
222
223 1
    /**
224 1
     * Returns true if either a Content-Type or Mime-Version header are defined
225 1
     * in this Message.
226
     * 
227 1
     * @return bool
228
     */
229
    public function isMime()
230
    {
231
        $contentType = $this->getHeaderValue('Content-Type');
232
        $mimeVersion = $this->getHeaderValue('Mime-Version');
233
        return ($contentType !== null || $mimeVersion !== null);
234
    }
235
236
    /**
237
     * Saves the message as a MIME message to the passed resource handle.
238 1
     * 
239
     * @param resource $handle
240 1
     */
241 1
    public function save($handle)
242 1
    {
243
        if ($this->stream !== null) {
244 1
            $dest = Psr7\stream_for($handle);
0 ignored issues
show
Bug introduced by
The function stream_for was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

244
            $dest = /** @scrutinizer ignore-call */ Psr7\stream_for($handle);
Loading history...
245
            $this->stream->rewind();
246
            Psr7\copy_to_stream($this->stream, $dest);
0 ignored issues
show
Bug introduced by
The function copy_to_stream was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
            /** @scrutinizer ignore-call */ 
247
            Psr7\copy_to_stream($this->stream, $dest);
Loading history...
247
            // don't close when out of scope
248
            $dest->detach();
249
        }
250
    }
251
252
    /**
253
     * Shortcut to call Message::save with a php://temp stream and return the
254
     * written email message as a string.
255
     * 
256
     * @return string
257 1
     */
258
    public function __toString()
259 1
    {
260 1
        if ($this->stream !== null) {
261 1
            $this->stream->rewind();
262
            return $this->stream->getContents();
263 1
        }
264
        return '';
265
    }
266
}
267