ADecoder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 42
rs 10
c 1
b 0
f 0
ccs 14
cts 14
cp 1
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaderKey() 0 3 1
A canDecode() 0 9 3
A decodings() 0 8 2
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Http\Answer\DecodeStrings;
4
5
6
/**
7
 * Class ADecoder
8
 * @package kalanis\RemoteRequest\Protocols\Http\Answer\DecodeStrings
9
 * Encode content processable in strings
10
 * Due changes by content encoding there shall be a way to expand to the original content
11
 * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
12
 */
13
abstract class ADecoder
14
{
15
    /** @var string[] */
16
    protected array $contentEncoded = [];
17
18 7
    public function getHeaderKey(): string
19
    {
20 7
        return 'Content-Encoding';
21
    }
22
23 6
    public function canDecode(string $header): bool
24
    {
25 6
        $encode = $this->decodings($header);
26 6
        foreach ($encode as $coding) {
27 6
            if (in_array($coding, $this->contentEncoded)) {
28 5
                return true;
29
            }
30
        }
31 5
        return false;
32
    }
33
34
    /**
35
     * Extract encodings from its compiled content header
36
     * @param string $headers
37
     * @return string[]
38
     */
39 6
    protected function decodings(string $headers): array
40
    {
41 6
        if (empty($headers)) {
42 3
            return [];
43
        }
44 6
        return array_map(function ($enc) {
45 6
            return trim(mb_strtolower($enc));
46 6
        }, explode(',', $headers));
47
    }
48
49
    /**
50
     * In and Out are strings - everything in memory
51
     * @param string $content
52
     * @return string
53
     */
54
    abstract public function processDecode(string $content): string;
55
}
56