Passed
Push — master ( 95e744...8d5d5e )
by Dominik
01:52
created

Decoder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 51
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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