Passed
Push — 1.0.0 ( 6e8983...dd1332 )
by Zaahid
06:14
created

StreamDecoratorFactory::newCharsetStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 StreamDecoratorFactory
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(new NonClosingStream($stream), $length, $start);
52
    }
53
54
    public function newBase64Stream(StreamInterface $stream)
55
    {
56
        return new CachingStream(
57
            new Base64Stream(
58
                new PregReplaceFilterStream($stream, '/[^a-zA-Z0-9\/\+=]/', '')
59
            )
60
        );
61
    }
62
63
    public function newQuotedPrintableStream(StreamInterface $stream)
64
    {
65
        return new CachingStream(new QuotedPrintableStream($stream));
66
    }
67
68
    public function newUUStream(StreamInterface $stream)
69
    {
70
        return new CachingStream(new UUStream($stream));
71
    }
72
73
    public function newCharsetStream(StreamInterface $stream, $fromCharset, $toCharset)
74
    {
75
        return new CachingStream(new CharsetStream($stream, $fromCharset, $toCharset));
76
    }
77
}
78