Json   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 32
ccs 10
cts 10
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 9 2
A encode() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LibraryCatalog\Transformer\Encoder;
6
7
class Json implements EncoderInterface
8
{
9
    /**
10
     * @param mixed $data
11
     * @throws Exception
12
     * @return string
13
     */
14 25
    public function encode($data): string
15
    {
16 25
        $res = json_encode($data);
17
18 25
        if ($res === false) {
19 1
            throw new Exception('Can not encode JSON');
20
        }
21
22 24
        return $res;
23
    }
24
25
    /**
26
     * @param string $value
27
     * @throws Exception
28
     * @return mixed
29
     */
30 17
    public function decode(string $value)
31
    {
32 17
        $res = json_decode($value, true);
33
34 17
        if ($res === null) {
35 3
            throw new Exception('Can not decode JSON');
36
        }
37
38 14
        return $res;
39
    }
40
}
41