StreamBuffer::getPosition()   A
last analyzed

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 0
Metric Value
c 1
b 0
f 0
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
 * This file is part of graze/csv-token
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/csv-token/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/csv-token
12
 */
13
14
namespace Graze\CsvToken\Buffer;
15
16
class StreamBuffer implements BufferInterface
17
{
18
    const READ_LENGTH = 128;
19
20
    /** @var string */
21
    protected $contents;
22
    /** @var int */
23
    protected $length;
24
    /** @var int */
25
    protected $position;
26
    /** @var bool */
27
    protected $eof = false;
28
    /** @var int */
29
    protected $minSize;
30
31
    /** @var resource */
32
    private $stream;
33
    /** @var int */
34
    private $readLength;
35
36
    /**
37
     * Create a buffer around a stream
38
     *
39
     * @param resource $stream
40
     * @param int      $readLength
41
     * @param int      $minSize
42
     */
43 33
    public function __construct($stream, $readLength = self::READ_LENGTH, $minSize = -1)
44
    {
45 33
        $this->stream = $stream;
46 33
        $this->readLength = $readLength;
47 33
        $this->position = ftell($stream);
48 33
        $this->minSize = $minSize;
49 33
        $this->length = 0;
50 33
        $this->eof = false;
51 33
    }
52
53
    /**
54
     * @return string
55
     */
56 31
    public function getContents()
57
    {
58 31
        return $this->contents;
59
    }
60
61
    /**
62
     * @return int
63
     */
64 28
    public function getLength()
65
    {
66 28
        return $this->length;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72 30
    public function isEof()
73
    {
74 30
        return $this->length === 0;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 33
    public function read()
81
    {
82 33
        if ($this->canRead()) {
83 33
            $next = fread($this->stream, $this->readLength);
84 33
            if (strlen($next) > 0) {
85 31
                $this->contents .= $next;
86 31
                $this->length += strlen($next);
87 31
                return true;
88
            } else {
89 30
                $this->eof = true;
90
            }
91
        }
92 31
        return false;
93
    }
94
95
    /**
96
     * Remove the first $length characters from the buffer
97
     *
98
     * @param int $length
99
     *
100
     * @return bool
101
     */
102 28
    public function move($length)
103
    {
104 28
        $this->contents = substr($this->contents, $length);
105 28
        $newLen = max(0, $this->length - $length);
106 28
        $this->position += $this->length - $newLen;
107 28
        $this->length = $newLen;
108 28
        return true;
109
    }
110
111
    /**
112
     * Determine if we can read more from the stream
113
     *
114
     * @return bool
115
     */
116 33
    private function canRead()
117
    {
118 33
        return ((!$this->eof)
119 33
            && ($this->minSize < 0 || $this->length <= $this->minSize));
120
    }
121
122
    /**
123
     * @return bool
124
     */
125 1
    public function isSourceEof()
126
    {
127 1
        return $this->eof;
128
    }
129
130
    /**
131
     * @return int
132
     */
133 28
    public function getPosition()
134
    {
135 28
        return $this->position;
136
    }
137
138
    /**
139
     * @return int
140
     */
141 1
    public function getMinBufferSize()
142
    {
143 1
        return $this->minSize;
144
    }
145
146
    /**
147
     * @param int $size
148
     *
149
     * @return $this
150
     */
151 4
    public function setMinBufferSize($size)
152
    {
153 4
        $this->minSize = $size;
154 4
        return $this;
155
    }
156
}
157