Completed
Pull Request — master (#83)
by Frederik
07:21
created

HeaderDecodedStream   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 64.85%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 149
ccs 24
cts 37
cp 0.6485
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A calculateOptimalStream() 0 6 1
A __toString() 0 4 1
A close() 0 4 1
A detach() 0 4 1
A getSize() 0 4 1
A tell() 0 4 1
A eof() 0 4 1
A isSeekable() 0 4 1
A seek() 0 4 1
A rewind() 0 4 1
A isWritable() 0 4 1
A write() 0 4 1
A isReadable() 0 4 1
A read() 0 4 1
A getContents() 0 4 1
A getMetadata() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Stream;
5
6
use Genkgo\Mail\HeaderInterface;
7
use Genkgo\Mail\Mime\PartInterface;
8
use Genkgo\Mail\StreamInterface;
9
10
final class HeaderDecodedStream implements StreamInterface
11
{
12
    /**
13
     * @var StreamInterface
14
     */
15
    private $decoratedStream;
16
17
    /**
18
     * @param HeaderInterface $header
19
     */
20 8
    public function __construct(HeaderInterface $header)
21
    {
22 8
        $this->decoratedStream = $this->calculateOptimalStream($header);
23 8
    }
24
25
    /**
26
     * @param HeaderInterface $header
27
     * @return StreamInterface
28
     */
29 8
    private function calculateOptimalStream(HeaderInterface $header): StreamInterface
30
    {
31 8
        return new StringStream(
32 8
            \iconv_mime_decode((string)$header->getValue(), \ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8')
33
        );
34
    }
35
36
    /**
37
     * @return string
38
     */
39 3
    public function __toString(): string
40
    {
41 3
        return $this->decoratedStream->__toString();
42
    }
43
44
    public function close(): void
45
    {
46
        $this->decoratedStream->close();
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function detach()
53
    {
54
        return $this->decoratedStream->detach();
55
    }
56
57
    /**
58
     * @return int|null
59
     */
60 1
    public function getSize(): ?int
61
    {
62 1
        return $this->decoratedStream->getSize();
63
    }
64
65
    /**
66
     * @return int
67
     * @throws \RuntimeException
68
     */
69 1
    public function tell(): int
70
    {
71 1
        return $this->decoratedStream->tell();
72
    }
73
74
    /**
75
     * @return bool
76
     */
77
    public function eof(): bool
78
    {
79
        return $this->decoratedStream->eof();
80
    }
81
82
    /**
83
     * @return bool
84
     */
85
    public function isSeekable(): bool
86
    {
87
        return $this->decoratedStream->isSeekable();
88
    }
89
90
    /**
91
     * @param int $offset
92
     * @param int $whence
93
     * @return int
94
     */
95 1
    public function seek(int $offset, int $whence = SEEK_SET): int
96
    {
97 1
        return $this->decoratedStream->seek($offset, $whence);
98
    }
99
100
    /**
101
     * @return bool
102
     */
103 1
    public function rewind(): bool
104
    {
105 1
        return $this->decoratedStream->rewind();
106
    }
107
108
    /**
109
     * @return bool
110
     */
111 1
    public function isWritable(): bool
112
    {
113 1
        return false;
114
    }
115
116
    /**
117
     * @param string $string
118
     * @return int
119
     */
120 1
    public function write($string): int
121
    {
122 1
        throw new \RuntimeException('Cannot write to a decoded stream');
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    public function isReadable(): bool
129
    {
130
        return $this->decoratedStream->isReadable();
131
    }
132
133
    /**
134
     * @param int $length
135
     * @return string
136
     */
137 1
    public function read(int $length): string
138
    {
139 1
        return $this->decoratedStream->read($length);
140
    }
141
142
    /**
143
     * @return string
144
     */
145 3
    public function getContents(): string
146
    {
147 3
        return $this->decoratedStream->getContents();
148
    }
149
150
    /**
151
     * @param array<int, string> $keys
152
     * @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...
153
     */
154
    public function getMetadata(array $keys = []): array
155
    {
156
        return $this->decoratedStream->getMetadata();
157
    }
158
}
159