Json::decode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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