TDecoding   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 32
rs 10
c 1
b 0
f 0
ccs 12
cts 12
cp 1
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addStringDecoding() 0 4 1
A processStringDecode() 0 9 4
A getStringHeader() 0 4 2
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Http\Answer\DecodeStrings;
4
5
6
/**
7
 * Trait TDecoding
8
 * @package kalanis\RemoteRequest\Protocols\Http\Answer\DecodeStrings
9
 */
10
trait TDecoding
11
{
12
    /** @var ADecoder[] */
13
    protected array $stringDecoders = [];
14
15 14
    public function addStringDecoding(ADecoder $decoder): self
16
    {
17 14
        $this->stringDecoders[] = $decoder;
18 14
        return $this;
19
    }
20
21 13
    public function processStringDecode(string $content): string
22
    {
23 13
        foreach ($this->stringDecoders as $decoder) {
24 7
            $header = $this->getStringHeader($decoder->getHeaderKey());
25 7
            if (!empty($header) && $decoder->canDecode(strval($header))) {
26 3
                $content = $decoder->processDecode($content);
27
            }
28
        }
29 13
        return $content;
30
    }
31
32 7
    protected function getStringHeader(string $key, ?string $default = null): ?string
33
    {
34 7
        $headers = $this->getAllHeaders();
35 7
        return isset($headers[$key]) ? strval(reset($headers[$key])) : $default;
36
    }
37
38
    /**
39
     * @return array<string, array<string>>
40
     */
41
    abstract public function getAllHeaders(): array;
42
}
43