TextResponse   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 45
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 18 3
A read() 0 8 3
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
}