HttpReader::readResponse()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 12

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 5.1158

Importance

Changes 0
Metric Value
dl 27
loc 27
ccs 10
cts 12
cp 0.8333
rs 8.439
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 2
crap 5.1158
1
<?php
2
3
namespace Dazzle\Http\Http\Driver\Reader;
4
5
use Dazzle\Throwable\Exception\Runtime\ReadException;
6
use Dazzle\Throwable\Exception\Logic\InvalidFormatException;
7
use Dazzle\Http\Http\Driver\Parser\HttpParser;
8
use Dazzle\Http\Http\Driver\Parser\HttpParserInterface;
9
use Dazzle\Util\Buffer\BufferInterface;
10
use GuzzleHttp\Psr7;
11
use Error;
12
use Exception;
13
14
class HttpReader implements HttpReaderInterface
15
{
16
    /**
17
     * @var int
18
     */
19
    const DEFAULT_MAX_SIZE = 0x4000; // 16 kB
20
21
    /**
22
     * @var int
23
     */
24
    const DEFAULT_START_LINE_LENGTH = 0x400; // 1 kB
25
26
    /**
27
     * @var string
28
     */
29
    const HTTP_EOM = "\r\n\r\n";
30
31
    /**
32
     * @var HttpParserInterface
33
     */
34
    protected $parser;
35
36
    /**
37
     * @var int
38
     */
39
    protected $maxFrameSize;
40
41
    /**
42
     * @param mixed[] $options
43
     */
44 77
    public function __construct($options = [])
45
    {
46 77
        $this->parser = new HttpParser();
47
48 77
        $this->maxFrameSize = isset($options['maxFrameSize'])
49 2
            ? (int) $options['maxFrameSize']
50 75
            : self::DEFAULT_MAX_SIZE;
51 77
    }
52
53
    /**
54
     *
55
     */
56 32
    public function __destruct()
57
    {
58 32
        unset($this->parser);
59 32
        unset($this->maxFrameSize);
60 32
    }
61
62
    /**
63
     * @override
64
     * @inheritDoc
65
     */
66 6 View Code Duplication
    public function readRequest(BufferInterface $buffer, $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68 6
        $buffer->push($data);
69
70 6
        if (($position = $buffer->search(self::HTTP_EOM)) === false)
71
        {
72 1
            if ($buffer->length() > $this->maxFrameSize)
73
            {
74 1
                throw new ReadException(
75 1
                    sprintf('Message start line exceeded maximum size of %d bytes.', $this->maxFrameSize)
76
                );
77
            }
78
79
            return null;
80
        }
81
82
        try
83
        {
84 5
            return $this->parser->parseRequest($buffer->drain());
85
        }
86 1
        catch (Error $ex)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
87
        {}
88 1
        catch (Exception $ex)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
89
        {}
90
91 1
        throw new InvalidFormatException('Could not parse start line.', 0, $ex);
92
    }
93
94
    /**
95
     * @override
96
     * @inheritDoc
97
     */
98 3 View Code Duplication
    public function readResponse(BufferInterface $buffer, $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100 3
        $buffer->push($data);
101
102 3
        if (($position = $buffer->search(self::HTTP_EOM)) === false)
103
        {
104 1
            if ($buffer->length() > $this->maxFrameSize)
105
            {
106 1
                throw new ReadException(
107 1
                    sprintf('Message start line exceeded maximum size of %d bytes.', $this->maxFrameSize)
108
                );
109
            }
110
111
            return null;
112
        }
113
114
        try
115
        {
116 2
            return $this->parser->parseResponse($buffer->drain());
117
        }
118 1
        catch (Error $ex)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
119
        {}
120 1
        catch (Exception $ex)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
121
        {}
122
123 1
        throw new InvalidFormatException('Could not parse start line.');
124
    }
125
}
126