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

StreamFactory::newLimitStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 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\Stream;
8
9
use GuzzleHttp\Psr7\CachingStream;
10
use Psr\Http\Message\StreamInterface;
11
use ZBateson\StreamDecorators\SeekingLimitStream;
12
use ZBateson\StreamDecorators\Base64Stream;
13
use ZBateson\StreamDecorators\QuotedPrintableStream;
14
use ZBateson\StreamDecorators\UUStream;
15
use ZBateson\StreamDecorators\CharsetStream;
16
use ZBateson\StreamDecorators\NonClosingStream;
17
use ZBateson\StreamDecorators\PregReplaceFilterStream;
18
use ZBateson\MailMimeParser\Message\Part\PartBuilder;
19
20
/**
21
 * Factory class for Psr7 stream decorators used in MailMimeParser.
22
 *
23
 * @author Zaahid Bateson
24
 */
25
class StreamFactory
26
{
27
    public function getLimitedPartStream(StreamInterface $stream, PartBuilder $part)
28
    {
29
        return $this->newLimitStream(
30
            $stream,
31
            $part->getStreamPartLength(),
32
            $part->getStreamPartStartOffset()
33
        );
34
    }
35
36
    public function getLimitedContentStream(StreamInterface $stream, PartBuilder $part)
37
    {
38
        $length = $part->getStreamContentLength();
39
        if ($length !== 0) {
40
            return $this->newLimitStream(
41
                $stream,
42
                $part->getStreamContentLength(),
43
                $part->getStreamContentStartOffset()
44
            );
45
        }
46
        return null;
47
    }
48
49
    private function newLimitStream(StreamInterface $stream, $length, $start)
50
    {
51
        return new SeekingLimitStream(
52
            $this->newNonClosingStream($stream),
53
            $length,
54
            $start
55
        );
56
    }
57
58
    public function newNonClosingStream(StreamInterface $stream)
59
    {
60
        return new NonClosingStream($stream);
61
    }
62
63
    public function newBase64Stream(StreamInterface $stream)
64
    {
65
        return new CachingStream(
66
            new Base64Stream(
67
                new PregReplaceFilterStream($stream, '/[^a-zA-Z0-9\/\+=]/', '')
68
            )
69
        );
70
    }
71
72
    public function newQuotedPrintableStream(StreamInterface $stream)
73
    {
74
        return new CachingStream(new QuotedPrintableStream($stream));
75
    }
76
77
    public function newUUStream(StreamInterface $stream)
78
    {
79
        return new CachingStream(new UUStream($stream));
80
    }
81
82
    public function newCharsetStream(StreamInterface $stream, $fromCharset, $toCharset)
83
    {
84
        return new CachingStream(new CharsetStream($stream, $fromCharset, $toCharset));
85
    }
86
87
    public function newMessagePartStream(MessagePart $part)
88
    {
89
        return new MessagePartStream($this, $part);
90
    }
91
}
92