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

AsciiEncodedStream::seek()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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