Completed
Pull Request — master (#8)
by Harry
28:53
created

StreamBuffer::getContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Graze\CsvToken\Buffer;
4
5
class StreamBuffer implements BufferInterface
6
{
7
    const READ_LENGTH = 128;
8
9
    /** @var string */
10
    protected $contents;
11
    /** @var int */
12
    protected $length;
13
    /** @var int */
14
    protected $position;
15
    /** @var bool */
16
    protected $eof = false;
17
    /** @var int */
18
    protected $minSize;
19
20
    /** @var resource */
21
    private $stream;
22
    /** @var int */
23
    private $readLength;
24
25
    /**
26
     * Create a buffer around a stream
27
     *
28
     * @param resource $stream
29
     * @param int      $readLength
30
     * @param int      $minSize
31
     */
32 34
    public function __construct($stream, $readLength = self::READ_LENGTH, $minSize = -1)
33
    {
34 34
        $this->stream = $stream;
35 34
        $this->readLength = $readLength;
36 34
        $this->position = ftell($stream);
37 34
        $this->minSize = $minSize;
38 34
        $this->length = 0;
39 34
        $this->eof = false;
40 34
    }
41
42
    /**
43
     * @return string
44
     */
45 32
    public function getContents()
46
    {
47 32
        return $this->contents;
48
    }
49
50
    /**
51
     * @return int
52
     */
53 29
    public function getLength()
54
    {
55 29
        return $this->length;
56
    }
57
58
    /**
59
     * @return bool
60
     */
61 31
    public function isEof()
62
    {
63 31
        return $this->length === 0;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69 34
    public function read()
70
    {
71 34
        if (!$this->eof) {
72 34
            if ($this->minSize < 0 || $this->length <= $this->minSize) {
73 34
                $next = fread($this->stream, $this->readLength);
74 34
                if (strlen($next) > 0) {
75 32
                    $this->contents .= $next;
76 32
                    $this->length += strlen($next);
77 32
                    return true;
78
                } else {
79 31
                    $this->eof = true;
80
                }
81 31
            }
82 32
        }
83 32
        return false;
84
    }
85
86
    /**
87
     * Remove the first $length characters from the buffer
88
     *
89
     * @param int $length
90
     *
91
     * @return bool
92
     */
93 29
    public function move($length)
94
    {
95 29
        $this->contents = substr($this->contents, $length);
96 29
        $newLen = max(0, $this->length - $length);
97 29
        $this->position += $this->length - $newLen;
98 29
        $this->length = $newLen;
99 29
        return true;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105 1
    public function isSourceEof()
106
    {
107 1
        return $this->eof;
108
    }
109
110
    /**
111
     * @return int
112
     */
113 29
    public function getPosition()
114
    {
115 29
        return $this->position;
116
    }
117
118
    /**
119
     * @return int
120
     */
121 1
    public function getMinBufferSize()
122
    {
123 1
        return $this->minSize;
124
    }
125
126
    /**
127
     * @param int $size
128
     *
129
     * @return $this
130
     */
131 4
    public function setMinBufferSize($size)
132
    {
133 4
        $this->minSize = $size;
134 4
        return $this;
135
    }
136
}
137