Decoder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A addTypeDecoder() 0 3 1
A getContentTypes() 0 3 1
A decode() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Decoder;
6
7
use Chubbyphp\Deserialization\DeserializerLogicException;
8
use Chubbyphp\Deserialization\DeserializerRuntimeException;
9
10
final class Decoder implements DecoderInterface
11
{
12
    /**
13
     * @var TypeDecoderInterface[]
14
     */
15
    private $decoderTypes;
16
17
    /**
18
     * @param TypeDecoderInterface[] $decoderTypes
19
     */
20 3
    public function __construct(array $decoderTypes)
21
    {
22 3
        $this->decoderTypes = [];
23 3
        foreach ($decoderTypes as $decoderType) {
24 3
            $this->addTypeDecoder($decoderType);
25
        }
26 3
    }
27
28
    public function getContentTypes(): array
29
    {
30
        return array_keys($this->decoderTypes);
31 1
    }
32
33 1
    /**
34
     * @throws DeserializerLogicException
35
     * @throws DeserializerRuntimeException
36
     */
37
    public function decode(string $data, string $contentType): array
38
    {
39
        if (isset($this->decoderTypes[$contentType])) {
40
            return $this->decoderTypes[$contentType]->decode($data);
41
        }
42
43
        throw DeserializerLogicException::createMissingContentType($contentType);
44
    }
45 2
46
    private function addTypeDecoder(TypeDecoderInterface $decoderType): void
47 2
    {
48 1
        $this->decoderTypes[$decoderType->getContentType()] = $decoderType;
49
    }
50
}
51