Passed
Push — master ( 6139d1...678a4c )
by Petr
13:22 queued 10:27
created

Json   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A unpack() 0 7 3
A pack() 0 9 3
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Shared\FormatFiles;
4
5
6
use kalanis\kw_mapper\Interfaces\IFileFormat;
7
use kalanis\kw_mapper\MapperException;
8
9
10
/**
11
 * Class Json
12
 * @package kalanis\kw_mapper\Storage\Shared\FormatFiles
13
 */
14
class Json implements IFileFormat
15
{
16 1
    public function unpack(string $content): array
17
    {
18 1
        $result = @json_decode($content, true);
19 1
        if (is_null($result) && json_last_error()) {
20 1
            throw new MapperException('Cannot parse JSON input - ' . json_last_error_msg());
21
        }
22 1
        return $result;
23
    }
24
25 1
    public function pack(array $records): string
26
    {
27 1
        $result = json_encode($records);
28 1
        if (false === $result && json_last_error()) {
29
            // @codeCoverageIgnoreStart
30
            throw new MapperException('Cannot parse JSON output - ' . json_last_error_msg());
31
        }
32
        // @codeCoverageIgnoreEnd
33 1
        return strval($result);
34
    }
35
}
36