Chunked   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 40
ccs 15
cts 15
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaderKey() 0 3 1
A canDecode() 0 3 1
A processDecode() 0 20 3
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Http\Answer\DecodeStrings;
4
5
6
use kalanis\RemoteRequest\Protocols;
7
8
9
/**
10
 * Class Chunked
11
 * @package kalanis\RemoteRequest\Protocols\Http\Answer\DecodeStrings
12
 * Glue chunked strings back to original
13
 */
14
class Chunked extends ADecoder
15
{
16 5
    public function getHeaderKey(): string
17
    {
18 5
        return 'Transfer-Encoding';
19
    }
20
21 2
    public function canDecode(string $header): bool
22
    {
23 2
        return 'chunked' == mb_strtolower($header);
24
    }
25
26
    /**
27
     * Repair chunked transport
28
     * do not ask how it works...
29
     * @param string $content
30
     * @return string
31
     * @link https://en.wikipedia.org/wiki/Chunked_transfer_encoding
32
     * @link https://tools.ietf.org/html/rfc2616#section-3.6
33
     */
34 2
    public function processDecode(string $content): string
35
    {
36 2
        $partialData = $content;
37 2
        $cleared = '';
38
        do {
39 2
            if (preg_match('#^(([0-9a-fA-F]+)\r\n)(.*)#m', $partialData, $matches)) {
40 2
                $segmentLength = hexdec($matches[2]);
41
                // skip bytes defined as chunk size and get next with length of chunk size
42 2
                $chunk = mb_substr($partialData, mb_strlen($matches[1]), intval($segmentLength));
43 2
                $cleared .= $chunk;
44
                // remove bytes with chunk size, chunk itself and ending crlf
45 2
                $partialData = mb_substr($partialData, mb_strlen($matches[1]) + mb_strlen($chunk) + mb_strlen(Protocols\Http::DELIMITER));
46
            } else {
47
                // @codeCoverageIgnoreStart
48
                $segmentLength = 0;
49
            }
50
            // @codeCoverageIgnoreEnd
51 2
        } while (0 < $segmentLength);
52 2
        $content = $cleared;
53 2
        return $content;
54
    }
55
}
56