Passed
Push — master ( 37aad3...325143 )
by Zaahid
03:26
created

HeaderStream::createStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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 ZBateson\MailMimeParser\Message\Part\ParentHeaderPart;
10
use ZBateson\MailMimeParser\Message\Part\MessagePart;
11
use Psr\Http\Message\StreamInterface;
12
use GuzzleHttp\Psr7\StreamDecoratorTrait;
13
use GuzzleHttp\Psr7;
14
15
/**
16
 * Psr7 stream decorator implementation providing a readable stream for a part's
17
 * headers.
18
 *
19
 * HeaderStream is only used by a MimePart parent.  It can accept any
20
 * MessagePart - for non-MimeParts, only type headers are generated based on
21
 * available information.
22
 *
23
 * @author Zaahid Bateson
24
 */
25
class HeaderStream implements StreamInterface
26
{
27
    use StreamDecoratorTrait;
28
29
    /**
30
     * @var MessagePart the part to read from.
31
     */
32
    protected $part;
33
34
    /**
35
     * @param MessagePart $part
36
     */
37 2
    public function __construct(MessagePart $part)
38
    {
39 2
        $this->part = $part;
40 2
    }
41
42
    /**
43
     * Returns a header array for the current part.
44
     *
45
     * If the part is not a MimePart, Content-Type, Content-Disposition and
46
     * Content-Transfer-Encoding headers are generated manually.
47
     *
48
     * @return array
49
     */
50 2
    private function getPartHeadersArray()
51
    {
52 2
        if ($this->part instanceof ParentHeaderPart) {
53 1
            return $this->part->getRawHeaders();
54 1
        } elseif ($this->part->getParent() !== null && $this->part->getParent()->isMime()) {
55
            return [
56 1
                [ 'Content-Type', $this->part->getContentType() ],
57 1
                [ 'Content-Disposition', $this->part->getContentDisposition() ],
58 1
                [ 'Content-Transfer-Encoding', $this->part->getContentTransferEncoding() ]
59
            ];
60
        }
61 1
        return [];
62
    }
63
64
    /**
65
     * Writes out headers for $this->part and follows them with an empty line.
66
     *
67
     * @param StreamInterface $stream
68
     */
69 2
    public function writePartHeadersTo(StreamInterface $stream)
70
    {
71 2
        $headers = $this->getPartHeadersArray($this->part);
0 ignored issues
show
Unused Code introduced by
The call to ZBateson\MailMimeParser\...::getPartHeadersArray() has too many arguments starting with $this->part. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        /** @scrutinizer ignore-call */ 
72
        $headers = $this->getPartHeadersArray($this->part);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72 2
        foreach ($headers 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