Passed
Push — master ( 9e6d2f...c6b62f )
by Zaahid
06:54
created

StreamFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 68
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0
wmc 11

10 Methods

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