Completed
Push — master ( 88acd9...44b0be )
by Daniel
02:07
created

StringReader::setStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the stream package
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Stream\Reader;
9
10
use GravityMedia\Stream\Exception;
11
use GravityMedia\Stream\StreamInterface;
12
13
/**
14
 * String reader
15
 *
16
 * @package GravityMedia\Stream\Reader
17
 */
18
class StringReader
19
{
20
    /**
21
     * @var StreamInterface
22
     */
23
    protected $stream;
24
25
    /**
26
     * @var int
27
     */
28
    protected $length;
29
30
    /**
31
     * Create string reader object
32
     *
33
     * @throws Exception\InvalidArgumentException An exception will be thrown for non-readable streams
34
     *
35
     * @param StreamInterface $stream
36
     * @param int $length
37
     */
38 9
    public function __construct(StreamInterface $stream, $length)
39
    {
40 9
        if (!$stream->isReadable()) {
41 3
            throw new Exception\InvalidArgumentException('Stream not readable');
42
        }
43
44 6
        $this->stream = $stream;
45 6
        $this->length = $length;
46 6
    }
47
48
    /**
49
     * Get stream
50
     *
51
     * @return StreamInterface
52
     */
53 3
    public function getStream()
54
    {
55 3
        return $this->stream;
56
    }
57
58
    /**
59
     * Get length
60
     *
61
     * @return int
62
     */
63 3
    public function getLength()
64
    {
65 3
        return $this->length;
66
    }
67
68
    /**
69
     * Read string data from the stream
70
     *
71
     * @throws Exception\IOException    An exception will be thrown for invalid stream resources or when the data could
72
     *                                  not be read
73
     *
74
     * @return string
75
     */
76 3
    public function read()
77
    {
78 3
        return $this->stream->read($this->length);
79
    }
80
}
81