Passed
Push — master ( 5d8af1...e44bd6 )
by Petr
09:24
created

Json::unpack()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
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