TDecoding::processStreamDecode()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
ccs 6
cts 6
cp 1
crap 4
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Http\Answer\DecodeStreams;
4
5
6
/**
7
 * Trait TDecoding
8
 * @package kalanis\RemoteRequest\Protocols\Http\Answer\DecodeStreams
9
 */
10
trait TDecoding
11
{
12
    /** @var ADecoder[] */
13
    protected array $streamsDecoders = [];
14
15 1
    public function addStreamDecoder(ADecoder $decoder): self
16
    {
17 1
        $this->streamsDecoders[] = $decoder;
18 1
        return $this;
19
    }
20
21
    /**
22
     * @param resource $content stream handler
23
     * @return resource
24
     */
25 4
    public function processStreamDecode($content)
26
    {
27 4
        foreach ($this->streamsDecoders as $decoder) {
28 1
            $header = $this->getStreamHeader($decoder->getHeaderKey());
29 1
            if (!empty($header) && $decoder->canDecode(strval($header))) {
30 1
                $content = $decoder->processDecode($content);
31
            }
32
        }
33 4
        return $content;
34
    }
35
36 1
    protected function getStreamHeader(string $key, ?string $default = null): ?string
37
    {
38 1
        $headers = $this->getAllHeaders();
39 1
        return isset($headers[$key]) ? strval(reset($headers[$key])) : $default;
40
    }
41
42
    /**
43
     * @return array<string, array<string>>
44
     */
45
    abstract public function getAllHeaders(): array;
46
}
47