HttpParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 53
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parseRequest() 0 23 5
A parseResponse() 0 18 3
1
<?php
2
3
namespace Dazzle\Http\Http\Driver\Parser;
4
5
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException;
6
use Dazzle\Http\Http\HttpRequest;
7
use Dazzle\Http\Http\HttpResponse;
8
use GuzzleHttp\Psr7;
9
10
class HttpParser implements HttpParserInterface
11
{
12
    /**
13
     * @override
14
     * @inheritDoc
15
     */
16 6
    public function parseRequest($message)
17
    {
18 6
        $data = Psr7\_parse_message($message);
19 6
        $matches = [];
20
21 6
        if (!preg_match('/^[a-zA-Z]+\s+([a-zA-Z]+:\/\/|\/).*/', $data['start-line'], $matches))
22
        {
23 2
            throw new InvalidArgumentException('Invalid request string');
24
        }
25
26 4
        $parts = explode(' ', $data['start-line'], 3);
27 4
        $version = isset($parts[2]) ? explode('/', $parts[2])[1] : '1.1';
28
29 4
        $request = new HttpRequest(
30 4
            $parts[0],
31 4
            $matches[1] === '/' ? Psr7\_parse_request_uri($parts[1], $data['headers']) : $parts[1],
32 4
            $data['headers'],
33 4
            $data['body'],
34 4
            $version
35
        );
36
37 4
        return $matches[1] === '/' ? $request : $request->withRequestTarget($parts[1]);
38
    }
39
40
    /**
41
     * @override
42
     * @inheritDoc
43
     */
44 1
    public function parseResponse($message)
45
    {
46 1
        $data = Psr7\_parse_message($message);
47
48 1
        if (!preg_match('/^HTTP\/.* [0-9]{3} .*/', $data['start-line']))
49
        {
50
            throw new InvalidArgumentException('Invalid response string');
51
        }
52 1
        $parts = explode(' ', $data['start-line'], 3);
53
54 1
        return new HttpResponse(
55 1
            $parts[1],
56 1
            $data['headers'],
57 1
            $data['body'],
58 1
            explode('/', $parts[0])[1],
59 1
            isset($parts[2]) ? $parts[2] : null
60
        );
61
    }
62
}
63