ADecoder::decodings()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
ccs 6
cts 6
cp 1
crap 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