YamlTypeDecoder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 23
ccs 6
cts 6
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentType() 0 3 1
A decode() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Decoder;
6
7
use Chubbyphp\Deserialization\DeserializerRuntimeException;
8
use Symfony\Component\Yaml\Exception\ParseException;
9
use Symfony\Component\Yaml\Yaml;
10
11
final class YamlTypeDecoder implements TypeDecoderInterface
12
{
13
    public function getContentType(): string
14
    {
15
        return 'application/x-yaml';
16 3
    }
17
18 3
    /**
19
     * @throws DeserializerRuntimeException
20
     */
21
    public function decode(string $data): array
22
    {
23
        try {
24
            $decoded = Yaml::parse($data);
25
        } catch (ParseException $e) {
26
            throw DeserializerRuntimeException::createNotParsable($this->getContentType());
27
        }
28 4
29
        if (!is_array($decoded)) {
30
            throw DeserializerRuntimeException::createNotParsable($this->getContentType());
31 4
        }
32 1
33 1
        return $decoded;
34
    }
35
}
36