ProgressiveStreamReader::read()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4286
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
namespace Cassandra\Response;
3
4
class ProgressiveStreamReader extends StreamReader{
5
    
6
    /**
7
     */
8
    protected $source;
9
10
    protected $dataLength = 0;
11
     
12
    public function __construct($data = ''){
13
        $this->data = $data;
14
        $this->dataLength = strlen($data);
15
    }
16
    
17
    public function setSource($source){
18
        $this->source = $source;
19
    }
20
    
21
    /**
22
     * Read data from stream.
23
     *
24
     * NOTICE When $this->offset == strlen($this->data), substr() will return false.  You'd better avoid call read() when $length == 0.
25
     *
26
     * @param int $length  $length should be > 0.
27
     * @return string
28
     */
29
    protected function read($length) {
30
        while($this->dataLength < $this->offset + $length){
31
            if ($this->source === null)
32
                throw new Exception('The response is incomplete, or types expectation mismatch.');
33
            
34
            $this->data .= $received = $this->source->readOnce($this->offset + $length - $this->dataLength);
35
            $this->dataLength += strlen($received);
36
        }
37
    
38
        $output = substr($this->data, $this->offset, $length);
39
        $this->offset += $length;
40
        return $output;
41
    }
42
}
43