LineStream::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Stream;
13
14
use IteratorAggregate;
15
use Sunrise\Http\Message\Exception\InvalidArgumentException;
16
use Sunrise\Http\Message\Exception\RuntimeException;
17
use Sunrise\Http\Message\Stream;
18
use Traversable;
19
20
use function fgets;
21
use function is_resource;
22
use function rtrim;
23
24
/**
25
 * @implements IteratorAggregate<int, string>
26
 *
27
 * @since 3.6.0
28
 */
29
final class LineStream extends Stream implements IteratorAggregate
30
{
31
    /**
32
     * @var mixed
33
     */
34
    private $resource;
35
36
    /**
37
     * @param mixed $resource
38
     *
39
     * @throws InvalidArgumentException
40
     */
41 1
    public function __construct($resource, bool $autoClose = true)
42
    {
43 1
        parent::__construct($resource, $autoClose);
44
45 1
        $this->resource = $resource;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 1
    public function getIterator(): Traversable
52
    {
53 1
        if (!is_resource($this->resource)) {
54
            throw new RuntimeException('Stream has no resource');
55
        }
56
57 1
        if (!$this->isReadable()) {
58
            throw new RuntimeException('Stream is not readable');
59
        }
60
61 1
        while (($line = fgets($this->resource)) !== false) {
62 1
            yield rtrim($line);
63
        }
64
    }
65
}
66