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

MessagePartStream::writePartContentTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 12
rs 9.9666
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 ZBateson\MailMimeParser\MailMimeParser;
10
use ZBateson\MailMimeParser\Message\Part\MessagePart;
11
use ZBateson\MailMimeParser\Message\Part\MimePart;
12
use ZBateson\MailMimeParser\Message\Part\ParentHeaderPart;
13
use ZBateson\MailMimeParser\Stream\StreamFactory;
14
use Psr\Http\Message\StreamInterface;
15
use GuzzleHttp\Psr7\StreamDecoratorTrait;
16
use GuzzleHttp\Psr7\AppendStream;
17
use GuzzleHttp\Psr7;
18
19
/**
20
 * Writes a MimePart to a resource handle.
21
 * 
22
 * The class is responsible for writing out the headers and content of a
23
 * MimePart to an output stream buffer, taking care of encoding and filtering.
24
 * 
25
 * @author Zaahid Bateson
26
 */
27
class MessagePartStream implements StreamInterface
28
{
29
    use StreamDecoratorTrait;
30
31
    protected $streamFactory;
32
    protected $part;
33
34
    public function __construct(StreamFactory $sdf, MessagePart $part)
35
    {
36
        $this->streamFactory = $sdf;
37
        $this->part = $part;
38
    }
39
40
    /**
41
     * Sets up a mailmimeparser-encode stream filter on the content resource 
42
     * handle of the passed MimePart if applicable and returns a reference to
43
     * the filter.
44
     *
45
     * @param MimePart $part
46
     * @return StreamInterface a reference to the appended stream filter or null
47
     */
48
    private function getCharsetDecoratorForStream(MessagePart $part, StreamInterface $stream)
49
    {
50
        $charset = $part->getCharset();
51
        if (!empty($charset)) {
52
            $decorator = $this->streamFactory->newCharsetStream(
53
                $stream,
54
                $charset,
55
                MailMimeParser::DEFAULT_CHARSET
56
            );
57
            return $decorator;
58
        }
59
        return $stream;
60
    }
61
    
62
    /**
63
     * Appends a stream filter on the passed MimePart's content resource handle
64
     * based on the type of encoding for the passed part.
65
     *
66
     * @param MimePart $part
67
     * @param resource $handle
68
     * @param StreamLeftover $leftovers
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\Stream\StreamLeftover 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...
69
     * @return StreamInterface the stream filter
70
     */
71
    private function getTransferEncodingDecoratorForStream(
72
        MessagePart $part,
73
        StreamInterface $stream
74
    ) {
75
        $encoding = $part->getContentTransferEncoding();
76
        $decorator = null;
77
        switch ($encoding) {
78
            case 'quoted-printable':
79
                $decorator = $this->streamFactory->newQuotedPrintableStream($stream);
80
                break;
81
            case 'base64':
82
                $decorator = $this->streamFactory->newBase64Stream($stream);
83
                break;
84
            case 'x-uuencode':
85
                $decorator = $this->streamFactory->newUUStream($stream);
86
                $decorator->setFilename($part->getFilename());
87
                break;
88
            default:
89
                return $stream;
90
        }
91
        return $decorator;
92
    }
93
94
    /**
95
     * Writes out the content portion of the mime part based on the headers that
96
     * are set on the part, taking care of character/content-transfer encoding.
97
     *
98
     * @param MessagePart $part
99
     * @param StreamInterface $stream
100
     */
101
    public function writePartContentTo(MessagePart $part, StreamInterface $stream)
102
    {
103
        $contentStream = $part->getContentStream();
104
        if ($contentStream !== null) {
105
            $copyStream = $this->streamFactory->newNonClosingStream($stream);
106
            $es = $this->getTransferEncodingDecoratorForStream(
107
                $part,
108
                $copyStream
109
            );
110
            $cs = $this->getCharsetDecoratorForStream($part, $es);
111
            Psr7\copy_to_stream($contentStream, $cs);
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

111
            /** @scrutinizer ignore-call */ 
112
            Psr7\copy_to_stream($contentStream, $cs);
Loading history...
112
            $cs->close();
113
        }
114
    }
115
116
    protected function getBoundaryAndChildStreams(ParentHeaderPart $part)
117
    {
118
        $streams = [];
119
        $boundary = $part->getHeaderParameter('Content-Type', 'boundary');
120
        foreach ($part->getChildParts() as $child) {
121
            if ($boundary !== null) {
122
                $streams[] = Psr7\stream_for("\r\n--$boundary\r\n");
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

122
                $streams[] = /** @scrutinizer ignore-call */ Psr7\stream_for("\r\n--$boundary\r\n");
Loading history...
123
            }
124
            $streams[] = $child->getStream();
125
        }
126
        if ($boundary !== null) {
0 ignored issues
show
introduced by
The condition $boundary !== null is always true.
Loading history...
127
            $streams[] = Psr7\stream_for("\r\n--$boundary--\r\n");
128
        }
129
    }
130
131
    protected function getStreamsArray()
132
    {
133
        $content = Psr7\stream_for('php://temp');
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

133
        $content = /** @scrutinizer ignore-call */ Psr7\stream_for('php://temp');
Loading history...
134
        $this->writePartContentTo($this->part, $content);
135
        $content->rewind();
136
        $streams = [ new HeaderStream($this->part), $content ];
137
138
        if ($this->part instanceof ParentHeaderPart) {
139
            $streams = array_merge($streams, $this->getBoundaryAndChildStreams($this->part));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getBoundaryAndChildStreams($this->part) targeting ZBateson\MailMimeParser\...undaryAndChildStreams() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
140
        }
141
142
        return $streams;
143
    }
144
145
    /**
146
     * Creates the underlying stream lazily when required.
147
     *
148
     * @return StreamInterface
149
     */
150
    protected function createStream()
151
    {
152
        return new AppendStream($this->getStreamsArray());
153
    }
154
}
155