ComposerEncoder::encode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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