Completed
Pull Request — master (#40)
by Mr
06:35
created

ResourceStream::read()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 5
cts 7
cp 0.7143
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.2098
1
<?php
2
3
namespace RouterOS\Streams;
4
5
use RouterOS\Interfaces\StreamInterface;
6
use RouterOS\Exceptions\StreamException;
7
8
/**
9
 * class ResourceStream
10
 *
11
 * Stream using a resource (socket, file, pipe etc.)
12
 *
13
 * @package RouterOS
14
 * @since   0.9
15
 */
16
class ResourceStream implements StreamInterface
17
{
18
    protected $stream;
19
20
    /**
21
     * ResourceStream constructor.
22
     *
23
     * @param $stream
24
     */
25 7
    public function __construct($stream)
26
    {
27 7
        if (!is_resource($stream)) {
28
            throw new \InvalidArgumentException(
29
                sprintf(
30
                    'Argument must be a valid resource type. %s given.',
31
                    gettype($stream)
32
                )
33
            );
34
        }
35
36
        // TODO: Should we verify the resource type?
37 7
        $this->stream = $stream;
38 7
    }
39
40
    /**
41
     * @inheritDoc
42
     *
43
     * @throws \RouterOS\Exceptions\StreamException when length parameter is invalid
44
     * @throws \InvalidArgumentException when the stream have been totally read and read method is called again
45
     */
46 7
    public function read(int $length): string
47
    {
48 7
        if ($length <= 0) {
49
            throw new \InvalidArgumentException('Cannot read zero ot negative count of bytes from a stream');
50
        }
51
52
        // TODO: Ignore errors here, but why?
53 7
        $result = @fread($this->stream, $length);
54
55 7
        if (false === $result) {
56
            throw new StreamException("Error reading $length bytes");
57
        }
58
59 7
        return $result;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     *
65
     * @throws \RouterOS\Exceptions\StreamException when not possible to write bytes
66
     */
67 7
    public function write(string $string, int $length = null): int
68
    {
69 7
        if (null === $length) {
70 7
            $length = strlen($string);
71
        }
72
73
        // TODO: Ignore errors here, but why?
74 7
        $result = @fwrite($this->stream, $string, $length);
75
76 7
        if (false === $result) {
77
            throw new StreamException("Error writing $length bytes");
78
        }
79
80 7
        return $result;
81
    }
82
83
    /**
84
     * @inheritDoc
85
     *
86
     * @throws \RouterOS\Exceptions\StreamException when not possible to close the stream
87
     */
88
    public function close(): void
89
    {
90
        $hasBeenClosed = false;
91
92
        if (null !== $this->stream) {
93
            $hasBeenClosed = @fclose($this->stream);
94
            $this->stream  = null;
95
        }
96
97
        if (false === $hasBeenClosed) {
98
            throw new StreamException('Error closing stream');
99
        }
100
    }
101
}
102