Test Failed
Pull Request — master (#10)
by Yo
02:38
created

ComposerEncoder::decode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
ccs 0
cts 0
cp 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
namespace Yoanm\ComposerConfigManager\Application\Serializer\Encoder;
3
4
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
5
6
class ComposerEncoder
7
{
8 2
    /**
9
     * @param mixed $data
10 2
     *
11
     * @return string
12 2
     */
13 1
    public function encode($data)
14
    {
15 1
        $encodedJson = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
16
17
        if (JSON_ERROR_NONE !== json_last_error()) {
18 1
            throw new UnexpectedValueException(json_last_error_msg());
19
        } else {
20
            $encodedJson .= "\n";
21
        }
22
23
        return $encodedJson;
24
    }
25
26
    /**
27
     * @param string $data
28
     *
29
     * @return mixed
30
     */
31
    public function decode($data)
32
    {
33
        $decoded = json_decode($data, true);
34
35
        if (JSON_ERROR_NONE !== json_last_error()) {
36
            throw new UnexpectedValueException(json_last_error_msg());
37
        }
38
39
        return $decoded;
40
    }
41
}
42