Passed
Push — master ( de3d61...be839c )
by Alec
13:42 queued 13s
created

ResourceStream::write()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Output;
6
7
use AlecRabbit\Spinner\Contract\Output\IResourceStream;
8
use AlecRabbit\Spinner\Exception\InvalidArgumentException;
9
use AlecRabbit\Spinner\Exception\RuntimeException;
10
use AlecRabbit\Spinner\Helper\Asserter;
11
use Traversable;
12
13
/**
14
 * @codeCoverageIgnore Not testable
15
 */
16
final class ResourceStream implements IResourceStream
17
{
18
    /**
19
     * @var resource
20
     */
21
    private $stream;
22
23
    /**
24
     * @param resource $stream
25
     *
26
     * @throws InvalidArgumentException
27
     */
28
    public function __construct(
29
        $stream
30
    ) {
31
        Asserter::assertStream($stream);
32
        $this->stream = $stream;
33
    }
34
35
    public function write(Traversable $data): void
36
    {
37
        /** @var string $item */
38
        foreach ($data as $item) {
39
            if (@fwrite($this->stream, $item) === false) {
40
                // should never happen
41
                throw new RuntimeException('Was unable to write to a stream.');
42
            }
43
        }
44
        fflush($this->stream);
45
    }
46
}
47