Completed
Push — master ( cc370f...f8592e )
by Frederik
02:45 queued 01:07
created

MimeBodyDecodedStream::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Stream;
5
6
use Genkgo\Mail\Mime\PartInterface;
7
use Genkgo\Mail\StreamInterface;
8
9
final class MimeBodyDecodedStream implements StreamInterface
10
{
11
    /**
12
     * @var StreamInterface
13
     */
14
    private $decoratedStream;
15
16
    /**
17
     * @param PartInterface $mimePart
18
     */
19 20
    public function __construct(PartInterface $mimePart)
20
    {
21 20
        $this->decoratedStream = $this->calculateOptimalStream($mimePart);
22 20
    }
23
24
    /**
25
     * @param PartInterface $part
26
     * @return StreamInterface
27
     */
28 20
    private function calculateOptimalStream(PartInterface $part): StreamInterface
29
    {
30 20
        if (!$part->hasHeader('Content-Transfer-Encoding')) {
31
            return $part->getBody();
32
        }
33
34 20
        $encoding = $part->getHeader('Content-Transfer-Encoding')->getValue();
35 20
        switch ($encoding) {
36 20
            case 'quoted-printable':
37 2
                return QuotedPrintableDecodedStream::fromString((string)$part->getBody());
38 19
            case 'base64':
39 7
                return Base64DecodedStream::fromString((string)$part->getBody());
40 12
            case '7bit':
41
            case '8bit':
42 12
                return $part->getBody();
43
            default:
44
                throw new \UnexpectedValueException(
45
                    'Cannot decode body of mime part, unknown transfer encoding ' . $encoding
46
                );
47
        }
48
    }
49
50
51
    /**
52
     * @return string
53
     */
54 15
    public function __toString(): string
55
    {
56 15
        return $this->decoratedStream->__toString();
57
    }
58
59
    public function close(): void
60
    {
61
        $this->decoratedStream->close();
62
    }
63
64
    /**
65
     * @return mixed
66
     */
67
    public function detach()
68
    {
69
        return $this->decoratedStream->detach();
70
    }
71
72
    /**
73
     * @return int|null
74
     */
75 1
    public function getSize(): ?int
76
    {
77 1
        return $this->decoratedStream->getSize();
78
    }
79
80
    /**
81
     * @return int
82
     * @throws \RuntimeException
83
     */
84 1
    public function tell(): int
85
    {
86 1
        return $this->decoratedStream->tell();
87
    }
88
89
    /**
90
     * @return bool
91
     */
92
    public function eof(): bool
93
    {
94
        return $this->decoratedStream->eof();
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function isSeekable(): bool
101
    {
102
        return $this->decoratedStream->isSeekable();
103
    }
104
105
    /**
106
     * @param int $offset
107
     * @param int $whence
108
     * @return int
109
     */
110 1
    public function seek(int $offset, int $whence = SEEK_SET): int
111
    {
112 1
        return $this->decoratedStream->seek($offset, $whence);
113
    }
114
115
    /**
116
     * @return bool
117
     */
118 1
    public function rewind(): bool
119
    {
120 1
        return $this->decoratedStream->rewind();
121
    }
122
123
    /**
124
     * @return bool
125
     */
126 1
    public function isWritable(): bool
127
    {
128 1
        return $this->decoratedStream->isWritable();
129
    }
130
131
    /**
132
     * @param string $string
133
     * @return int
134
     */
135 1
    public function write($string): int
136
    {
137 1
        return $this->decoratedStream->write($string);
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function isReadable(): bool
144
    {
145
        return $this->decoratedStream->isReadable();
146
    }
147
148
    /**
149
     * @param int $length
150
     * @return string
151
     */
152 1
    public function read(int $length): string
153
    {
154 1
        return $this->decoratedStream->read($length);
155
    }
156
157
    /**
158
     * @return string
159
     */
160 2
    public function getContents(): string
161
    {
162 2
        return $this->decoratedStream->getContents();
163
    }
164
165
    /**
166
     * @param array<int, string> $keys
167
     * @return array<string, mixed>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
168
     */
169
    public function getMetadata(array $keys = []): array
170
    {
171
        return $this->decoratedStream->getMetadata();
172
    }
173
}
174