TextResponse::read()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 12
1
<?php
2
namespace Disque\Connection\Response;
3
4
use Disque\Connection\ConnectionException;
5
6
class TextResponse extends BaseResponse
7
{
8
    const READ_BUFFER_LENGTH = 8192;
9
10
    /**
11
     * Parse response
12
     *
13
     * @return string Response
14
     * @throws ConnectionException
15
     */
16
    public function parse()
17
    {
18
        $bytes = (int) $this->data;
19
        if ($bytes < 0) {
20
            return null;
21
        }
22
23
        $bytes += 2; // CRLF
24
        $string = '';
25
26
        do {
27
            $buffer = $this->read($bytes);
28
            $string .= $buffer;
29
            $bytes -= strlen($buffer);
30
        } while ($bytes > 0);
31
32
        return substr($string, 0, -2); // Remove last CRLF
33
    }
34
35
    /**
36
     * Read text
37
     *
38
     * @param int $bytes Bytes to read
39
     * @return string Text
40
     * @throws ConnectionException
41
     */
42
    private function read($bytes)
43
    {
44
        $buffer = call_user_func($this->reader, min($bytes, self::READ_BUFFER_LENGTH));
45
        if ($buffer === false || $buffer === '') {
46
            throw new ConnectionException('Error while reading buffered string from client');
47
        }
48
        return $buffer;
49
    }
50
}