HttpRequest::encodeHeaders()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 11
cts 11
cp 1
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Dazzle\Http\Http;
4
5
use Dazzle\Http\NetworkMessageInterface;
6
use GuzzleHttp\Psr7\Request;
7
8 View Code Duplication
class HttpRequest extends Request implements HttpRequestInterface, NetworkMessageInterface
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * @override
12
     * @inheritDoc
13
     */
14 1
    public function __toString()
15
    {
16 1
        return $this->encode();
17
    }
18
19
    /**
20
     * @override
21
     * @inheritDoc
22
     */
23 6
    public function encode()
24
    {
25 6
        return sprintf(
26 6
            "%s %s HTTP/%s\r\n%s\r\n%s",
27 6
            $this->getMethod(),
28 6
            $this->getTarget(),
29 6
            $this->getProtocolVersion(),
30 6
            $this->encodeHeaders($this->getHeaders()),
31 6
            (string) $this->getBody()
32
        );
33
    }
34
35
    /**
36
     * @override
37
     * @inheritDoc
38
     */
39 6
    public function getTarget()
40
    {
41 6
        return $this->getRequestTarget();
42
    }
43
44
    /**
45
     * @override
46
     * @inheritDoc
47
     */
48 5
    public function read()
49
    {
50 5
        return (string) $this->getBody();
51
    }
52
53
    /**
54
     * Encode headers.
55
     *
56
     * @param string[][] $headers
57
     * @return string
58
     */
59 7
    protected function encodeHeaders($headers = [])
60
    {
61 7
        $data = '';
62
63 7
        foreach ($headers as $name=>$values)
64
        {
65 7
            $temp = [];
66 7
            $data .= $name . ": ";
67 7
            $values = (array) $values;
68
69 7
            foreach ($values as $value)
70
            {
71 7
                $temp[] = $this->encodeHeader($value);
72
            }
73
74 7
            $data .= implode(", ", $temp);
75 7
            $data .= "\r\n";
76
        }
77
78 7
        return $data;
79
    }
80
81
    /**
82
     * Encode single header.
83
     *
84
     * @param string $header
85
     * @return string
86
     */
87 8
    protected function encodeHeader($header)
88
    {
89 8
        return preg_replace_callback(
90 8
            '/(?:[^A-Za-z0-9_\-\.~!\$&\'\(\)\[\]\*\+,:;=\/% ]+|%(?![A-Fa-f0-9]{2}))/',
91 8
            function (array $matches) {
92
                return rawurlencode($matches[0]);
93 8
            },
94 8
            $header
95
        );
96
    }
97
}
98