StringStream::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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