Passed
Push — master ( ad3faa...5c4918 )
by Zaahid
03:33
created

HeaderStream::writePartHeadersTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
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 ArrayIterator;
10
use GuzzleHttp\Psr7;
11
use GuzzleHttp\Psr7\StreamDecoratorTrait;
12
use Psr\Http\Message\StreamInterface;
13
use ZBateson\MailMimeParser\Message\Part\ParentHeaderPart;
14
use ZBateson\MailMimeParser\Message\Part\MessagePart;
15
16
/**
17
 * Psr7 stream decorator implementation providing a readable stream for a part's
18
 * headers.
19
 *
20
 * HeaderStream is only used by a MimePart parent.  It can accept any
21
 * MessagePart - for non-MimeParts, only type headers are generated based on
22
 * available information.
23
 *
24
 * @author Zaahid Bateson
25
 */
26
class HeaderStream implements StreamInterface
27
{
28
    use StreamDecoratorTrait;
29
30
    /**
31
     * @var MessagePart the part to read from.
32
     */
33
    protected $part;
34
35
    /**
36
     * @param MessagePart $part
37
     */
38 2
    public function __construct(MessagePart $part)
39
    {
40 2
        $this->part = $part;
41 2
    }
42
43
    /**
44
     * Returns a header array for the current part.
45
     *
46
     * If the part is not a MimePart, Content-Type, Content-Disposition and
47
     * Content-Transfer-Encoding headers are generated manually.
48
     *
49
     * @return array
50
     */
51 2
    private function getPartHeadersIterator()
52
    {
53 2
        if ($this->part instanceof ParentHeaderPart) {
54 1
            return $this->part->getRawHeaderIterator();
55 1
        } elseif ($this->part->getParent() !== null && $this->part->getParent()->isMime()) {
56 1
            return new ArrayIterator([
57 1
                [ 'Content-Type', $this->part->getContentType() ],
58 1
                [ 'Content-Disposition', $this->part->getContentDisposition() ],
59 1
                [ 'Content-Transfer-Encoding', $this->part->getContentTransferEncoding() ]
60
            ]);
61
        }
62 1
        return new ArrayIterator();
63
    }
64
65
    /**
66
     * Writes out headers for $this->part and follows them with an empty line.
67
     *
68
     * @param StreamInterface $stream
69
     */
70 2
    public function writePartHeadersTo(StreamInterface $stream)
71
    {
72 2
        foreach ($this->getPartHeadersIterator() as $header) {
73 2
            $stream->write("${header[0]}: ${header[1]}\r\n");
74
        }
75 2
        $stream->write("\r\n");
76 2
    }
77
78
    /**
79
     * Creates the underlying stream lazily when required.
80
     *
81
     * @return StreamInterface
82
     */
83
    protected function createStream()
84
    {
85 2
        $stream = Psr7\stream_for();
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

85
        $stream = /** @scrutinizer ignore-call */ Psr7\stream_for();
Loading history...
86 2
        $this->writePartHeadersTo($stream);
87 2
        $stream->rewind();
88 2
        return $stream;
89
    }
90
}
91