AsciiEncodedStream   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 85.11%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 164
ccs 40
cts 47
cp 0.8511
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
A close() 0 4 1
A detach() 0 10 2
A getSize() 0 4 1
A tell() 0 4 1
A eof() 0 4 1
A isSeekable() 0 4 1
A seek() 0 10 2
A rewind() 0 5 1
A isWritable() 0 4 1
A write() 0 7 1
A isReadable() 0 4 1
A read() 0 6 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\StreamInterface;
7
8
final class AsciiEncodedStream implements StreamInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $text;
14
15
    /**
16
     * @var int
17
     */
18
    private $position = 0;
19
20
    /**
21
     * AsciiEncodedStream constructor.
22
     * @param string $text
23
     * @param int $lineLength
24
     * @param string $lineBreak
25
     */
26 203
    public function __construct(string $text, int $lineLength = 78, string $lineBreak = "\r\n")
27
    {
28 203
        $this->text = \wordwrap($text, $lineLength, $lineBreak);
29 203
    }
30
31
    /**
32
     * @return string
33
     */
34 162
    public function __toString(): string
35
    {
36 162
        return $this->text;
37
    }
38
    
39
    public function close(): void
40
    {
41
        return;
42
    }
43
44
    /**
45
     * @return mixed
46
     */
47 12
    public function detach()
48
    {
49 12
        $handle = \fopen('php://memory', 'r+');
50 12
        if ($handle === false) {
51
            throw new \UnexpectedValueException('Cannot open php://memory for writing');
52
        }
53
54 12
        \fwrite($handle, $this->text);
55 12
        return $handle;
56
    }
57
58
    /**
59
     * @return int|null
60
     */
61 26
    public function getSize(): ?int
62
    {
63 26
        return \strlen($this->text);
64
    }
65
66
    /**
67
     * @return int
68
     * @throws \RuntimeException
69
     */
70 1
    public function tell(): int
71
    {
72 1
        return $this->position;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78 7
    public function eof(): bool
79
    {
80 7
        return $this->position >= \strlen($this->text);
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    public function isSeekable(): bool
87
    {
88
        return true;
89
    }
90
91
    /**
92
     * @param int $offset
93
     * @param int $whence
94
     * @return int
95
     */
96 2
    public function seek(int $offset, int $whence = SEEK_SET): int
97
    {
98 2
        $length = \strlen($this->text);
99 2
        if ($length < $offset) {
100 1
            $offset = $length;
101
        }
102
103 2
        $this->position = $offset;
104 2
        return 0;
105
    }
106
107
    /**
108
     * @return bool
109
     */
110 4
    public function rewind(): bool
111
    {
112 4
        $this->position = 0;
113 4
        return true;
114
    }
115
116
    /**
117
     * @return bool
118
     */
119 1
    public function isWritable(): bool
120
    {
121 1
        return true;
122
    }
123
124
    /**
125
     * @param string $string
126
     * @return int
127
     */
128 1
    public function write($string): int
129
    {
130 1
        $this->text = \substr_replace($this->text, $string, $this->position, \strlen($string));
131 1
        $bytesWritten = \strlen($string);
132 1
        $this->position += $bytesWritten;
133 1
        return $bytesWritten;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139
    public function isReadable(): bool
140
    {
141
        return true;
142
    }
143
144
    /**
145
     * @param int $length
146
     * @return string
147
     */
148 8
    public function read(int $length): string
149
    {
150 8
        $result = \substr($this->text, $this->position, $length);
151 8
        $this->position += \strlen($result);
152 8
        return $result;
153
    }
154
155
    /**
156
     * @return string
157
     */
158 8
    public function getContents(): string
159
    {
160 8
        return \substr($this->text, $this->position);
161
    }
162
163
    /**
164
     * @param array<int, string> $keys
165
     * @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...
166
     */
167 180
    public function getMetadata(array $keys = []): array
168
    {
169 180
        return [];
170
    }
171
}
172