Completed
Pull Request — master (#8)
by Mr
03:48
created

ResourceStream::read()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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 20
    public function __construct($stream)
26
    {
27 20
        if (!is_resource($stream)) {
28 5
            throw new \InvalidArgumentException(
29 5
                sprintf(
30 5
                    'Argument must be a valid resource type. %s given.',
31 5
                    gettype($stream)
32
                )
33
            );
34
        }
35
36
        // TODO: Should we verify the resource type?
37 15
        $this->stream = $stream;
38 15
    }
39
40
    /**
41
     * @param   int $length
42
     * @return  string
43
     * @throws  \RouterOS\Exceptions\StreamException
44
     * @throws  \InvalidArgumentException
45
     */
46 17
    public function read(int $length): string
47
    {
48 17
        if ($length <= 0) {
49 2
            throw new \InvalidArgumentException('Cannot read zero ot negative count of bytes from a stream');
50
        }
51
52
        // TODO: Ignore errors here, but why?
53 15
        $result = @fread($this->stream, $length);
54
55 15
        if (false === $result) {
56 1
            throw new StreamException("Error reading $length bytes");
57
        }
58
59 14
        return $result;
60
    }
61
62
    /**
63
     * Writes a string to a stream
64
     *
65
     * Write $length bytes of string, if not mentioned, write all the string
66
     * Must be binary safe (as fread).
67
     * if $length is greater than string length, write all string and return number of writen bytes
68
     * if $length os smaller than string length, remaining bytes are losts.
69
     *
70
     * @param   string   $string
71
     * @param   int|null $length the numer of bytes to read
72
     * @return  int the number of written bytes
73
     * @throws  \RouterOS\Exceptions\StreamException
74
     */
75 15
    public function write(string $string, int $length = null): int
76
    {
77 15
        if (null === $length) {
78 15
            $length = strlen($string);
79
        }
80
81
        // TODO: Ignore errors here, but why?
82 15
        $result = @fwrite($this->stream, $string, $length);
83
84 15
        if (false === $result) {
85 2
            throw new StreamException("Error writing $length bytes");
86
        }
87
88 13
        return $result;
89
    }
90
91
    /**
92
     * Close stream connection
93
     *
94
     * @return  void
95
     * @throws  \RouterOS\Exceptions\StreamException
96
     */
97 2
    public function close()
98
    {
99 2
        $hasBeenClosed = false;
100
101 2
        if (null !== $this->stream) {
102 2
            $hasBeenClosed = @fclose($this->stream);
103 2
            $this->stream  = null;
104
        }
105
106 2
        if (false === $hasBeenClosed) {
107 1
            throw new StreamException('Error closing stream');
108
        }
109 2
    }
110
}
111