Test Failed
Pull Request — master (#171)
by Zaahid
04:32
created

attachTransferEncodingFilter()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 18
rs 9.2222
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\Message;
8
9
use Psr\Http\Message\StreamInterface;
10
use GuzzleHttp\Psr7\CachingStream;
11
use ZBateson\MailMimeParser\Stream\StreamFactory;
12
13
/**
14
 * Holds the stream and content stream objects for a part.
15
 *
16
 * Note that streams are not explicitly closed or detached on destruction of the
17
 * PartSreamContainer by design: the passed StreamInterfaces will be closed on
18
 * their destruction when no references to them remain, which is useful when the
19
 * streams are passed around.
20
 *
21
 * In addition, all the streams passed to PartStreamContainer should be wrapping
22
 * a ZBateson\StreamDecorators\NonClosingStream unless attached to a part by a
23
 * user, this is because MMP uses a single seekable stream for content and wraps
24
 * it in ZBateson\StreamDecorators\SeekingLimitStream objects for each part.
25
 *
26
 * @author Zaahid Bateson
27
 */
28
class PartStreamContainer
29
{
30
    /**
31
     * @var StreamFactory used to apply psr7 stream decorators to the
32
     *      attached StreamInterface based on encoding.
33
     */
34
    protected $streamFactory;
35
36
    /**
37
     * @var StreamInterface stream containing the part's headers, content and
38
     *      children
39
     */
40
    protected $stream;
41
42
    /**
43
     * @var StreamInterface a stream containing this part's content
44
     */
45
    protected $contentStream;
46
47
    /**
48
     * @var StreamInterface the content stream after attaching transfer encoding
49
     *      streams to $contentStream.
50
     */
51
    protected $decodedStream;
52
53
    /**
54
     * @var StreamInterface attached charset stream to $decodedStream
55
     */
56
    protected $charsetStream;
57
58
    /**
59
     * @var bool true if the stream should be detached when this container is
60
     *      destroyed.
61
     */
62
    protected $detachParsedStream;
63
64
    /**
65
     * @var array map of the active encoding filter on the current handle.
66
     */
67
    private $encoding = [
68
        'type' => null,
69
        'filter' => null
70
    ];
71
72
    /**
73
     * @var array map of the active charset filter on the current handle.
74
     */
75
    private $charset = [
76
        'from' => null,
77
        'to' => null,
78
        'filter' => null
79
    ];
80
81
    public function __construct(StreamFactory $streamFactory)
82
    {
83
        $this->streamFactory = $streamFactory;
84
    }
85
86
    /**
87
     * Sets the part's stream containing the part's headers, content, and
88
     * children.
89
     *
90
     * @param StreamInterface $stream
91
     */
92
    public function setStream(StreamInterface $stream)
93
    {
94
        $this->stream = $stream;
95
    }
96
97
    /**
98
     * Returns the part's stream containing the part's headers, content, and
99
     * children.
100
     *
101
     * @return StreamInterface
102
     */
103
    public function getStream()
104
    {
105
        // error out if called before setStream, getStream should never return
106
        // null.
107
        $this->stream->rewind();
108
        return $this->stream;
109
    }
110
111
    /**
112
     * Returns true if there's a content stream associated with the part.
113
     *
114
     * @return boolean
115
     */
116
    public function hasContent()
117
    {
118
        return ($this->contentStream !== null);
119
    }
120
121
    /**
122
     * Attaches the passed stream as the content portion of this
123
     * StreamContainer.
124
     *
125
     * The content stream would represent the content portion of $this->stream.
126
     *
127
     * If the content is overridden, $this->stream should point to a dynamic
128
     * {@see ZBateson\Stream\MessagePartStream} that dynamically creates the
129
     * RFC822 formatted message based on the IMessagePart this
130
     * PartStreamContainer belongs to.
131
     *
132
     * setContentStream can be called with 'null' to indicate the IMessagePart
133
     * does not contain any content.
134
     *
135
     * @param StreamInterface $contentStream
136
     */
137
    public function setContentStream(StreamInterface $contentStream = null)
138
    {
139
        $this->contentStream = $contentStream;
140
        $this->decodedStream = null;
141
        $this->charsetStream = null;
142
    }
143
144
    /**
145
     * Returns true if the attached stream filter used for decoding the content
146
     * on the current handle is different from the one passed as an argument.
147
     *
148
     * @param string $transferEncoding
149
     * @return boolean
150
     */
151
    private function isTransferEncodingFilterChanged($transferEncoding)
152
    {
153
        return ($transferEncoding !== $this->encoding['type']);
154
    }
155
156
    /**
157
     * Returns true if the attached stream filter used for charset conversion on
158
     * the current handle is different from the one needed based on the passed
159
     * arguments.
160
     *
161
     * @param string $fromCharset
162
     * @param string $toCharset
163
     * @return boolean
164
     */
165
    private function isCharsetFilterChanged($fromCharset, $toCharset)
166
    {
167
        return ($fromCharset !== $this->charset['from']
168
            || $toCharset !== $this->charset['to']);
169
    }
170
171
    /**
172
     * Attaches a decoding filter to the attached content handle, for the passed
173
     * $transferEncoding.
174
     *
175
     * @param string $transferEncoding
176
     */
177
    protected function attachTransferEncodingFilter($transferEncoding)
178
    {
179
        if ($this->decodedStream !== null) {
180
            $this->encoding['type'] = $transferEncoding;
181
            $assign = null;
182
            switch ($transferEncoding) {
183
                case 'base64':
184
                    $assign = $this->streamFactory->newBase64Stream($this->decodedStream);
185
                    break;
186
                case 'x-uuencode':
187
                    $assign = $this->streamFactory->newUUStream($this->decodedStream);
188
                    break;
189
                case 'quoted-printable':
190
                    $assign = $this->streamFactory->newQuotedPrintableStream($this->decodedStream);
191
                    break;
192
            }
193
            if ($assign !== null) {
194
                $this->decodedStream = new CachingStream($assign);
195
            }
196
        }
197
    }
198
199
    /**
200
     * Attaches a charset conversion filter to the attached content handle, for
201
     * the passed arguments.
202
     *
203
     * @param string $fromCharset the character set the content is encoded in
204
     * @param string $toCharset the target encoding to return
205
     */
206
    protected function attachCharsetFilter($fromCharset, $toCharset)
207
    {
208
        if ($this->charsetStream !== null) {
209
            $this->charsetStream = new CachingStream($this->streamFactory->newCharsetStream(
210
                $this->charsetStream,
211
                $fromCharset,
212
                $toCharset
213
            ));
214
            $this->charset['from'] = $fromCharset;
215
            $this->charset['to'] = $toCharset;
216
        }
217
    }
218
219
    /**
220
     * Resets just the charset stream, and rewinds the decodedStream.
221
     */
222
    private function resetCharsetStream()
223
    {
224
        $this->charset = [
225
            'from' => null,
226
            'to' => null,
227
            'filter' => null
228
        ];
229
        $this->decodedStream->rewind();
230
        $this->charsetStream = $this->decodedStream;
231
    }
232
233
    /**
234
     * Resets cached encoding and charset streams, and rewinds the stream.
235
     */
236
    public function reset()
237
    {
238
        $this->encoding = [
239
            'type' => null,
240
            'filter' => null
241
        ];
242
        $this->charset = [
243
            'from' => null,
244
            'to' => null,
245
            'filter' => null
246
        ];
247
        $this->contentStream->rewind();
248
        $this->decodedStream = $this->contentStream;
249
        $this->charsetStream = $this->contentStream;
250
    }
251
252
    /**
253
     * Checks what transfer-encoding decoder stream and charset conversion
254
     * stream are currently attached on the underlying contentStream, and resets
255
     * them if the requested arguments differ from the currently assigned ones.
256
     *
257
     * @param string $transferEncoding the transfer encoding
258
     * @param string $fromCharset the character set the content is encoded in
259
     * @param string $toCharset the target encoding to return
260
     * @return StreamInterface
261
     */
262
    public function getContentStream($transferEncoding, $fromCharset, $toCharset)
263
    {
264
        if ($this->contentStream === null) {
265
            return null;
266
        }
267
        if (empty($fromCharset) || empty($toCharset)) {
268
            return $this->getBinaryContentStream($transferEncoding);
269
        }
270
        if ($this->charsetStream === null
271
            || $this->isTransferEncodingFilterChanged($transferEncoding)
272
            || $this->isCharsetFilterChanged($fromCharset, $toCharset)) {
273
            if ($this->charsetStream === null
274
                || $this->isTransferEncodingFilterChanged($transferEncoding)) {
275
                $this->reset();
276
                $this->attachTransferEncodingFilter($transferEncoding);
277
            }
278
            $this->resetCharsetStream();
279
            $this->attachCharsetFilter($fromCharset, $toCharset);
280
        }
281
        $this->charsetStream->rewind();
282
        return $this->charsetStream;
283
    }
284
285
    /**
286
     * Checks what transfer-encoding decoder stream is attached on the
287
     * underlying stream, and resets it if the requested arguments differ.
288
     *
289
     * @param string $transferEncoding
290
     * @return StreamInterface
291
     */
292
    public function getBinaryContentStream($transferEncoding)
293
    {
294
        if ($this->contentStream === null) {
295
            return null;
296
        }
297
        if ($this->decodedStream === null
298
            || $this->isTransferEncodingFilterChanged($transferEncoding)) {
299
            $this->reset();
300
            $this->attachTransferEncodingFilter($transferEncoding);
301
        }
302
        $this->decodedStream->rewind();
303
        return $this->decodedStream;
304
    }
305
}
306