Completed
Pull Request — master (#48)
by Frederik
02:13
created

LineIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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 LineIterator extends \IteratorIterator
9
{
10
    /**
11
     * @param StreamInterface $stream
12
     */
13 29
    public function __construct(StreamInterface $stream)
14
    {
15 29
        parent::__construct($this->newIterator($stream));
16 29
    }
17
18
    /**
19
     * @param StreamInterface $stream
20
     * @return \Generator
21
     */
22 29
    private function newIterator(StreamInterface $stream): \Generator
23
    {
24 29
        $stream->rewind();
25
26 29
        $bytes = '';
27 29
        while (!$stream->eof()) {
28 29
            $bytes .= $stream->read(1000);
29
30 29
            $index = 0;
31 29
            while (isset($bytes[$index])) {
32 29
                if ($bytes[$index] === "\r" && isset($bytes[$index + 1]) && $bytes[$index + 1] === "\n") {
33 10
                    $line = \substr($bytes, 0, $index);
34 10
                    $bytes = \substr($bytes, $index + 2);
35 10
                    $index = -1;
36
37 10
                    yield $line;
38
                }
39
40 29
                $index++;
41
            }
42
        }
43
44 27
        yield $bytes;
45 20
    }
46
}
47