Completed
Push — master ( 92f54d...8e8aad )
by Frederik
02:23
created

StringStream::eof()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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