JsonHandler::deserialize()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 9
nc 2
nop 2
crap 3
1
<?php
2
3
namespace Majora\Framework\Serializer\Handler\Json;
4
5
use Majora\Framework\Normalizer\Model\NormalizableInterface;
6
use Majora\Framework\Serializer\Handler\AbstractFormatHandler;
7
use Majora\Framework\Serializer\Handler\Json\Exception\JsonDeserializationException;
8
9
/**
10
 * Handler implementation creating and using json.
11
 */
12
class JsonHandler extends AbstractFormatHandler
13
{
14
    /**
15
     * @see FormatHandlerInterface::serialize()
16
     */
17 6
    public function serialize($data, $scope)
18
    {
19 6
        return json_encode($data instanceof NormalizableInterface ?
20 6
            $this->normalizer->normalize($data, $scope) :
21 3
            $data
22 3
        );
23
    }
24
25
    /**
26
     * @see FormatHandlerInterface::deserialize()
27
     */
28 8
    public function deserialize($data, $output)
29
    {
30 8
        $arrayData = json_decode($data, true);
31 8
        if (null === $arrayData) {
32 2
            throw new JsonDeserializationException(sprintf(
33 2
                'Invalid json data, error %s : %s',
34 1
                json_last_error(),
35 2
                function_exists('json_last_error_msg') ? // php 5.4 compatibility
36 2
                    json_last_error_msg() : 'error message unavailable'
37 1
            ));
38
        }
39
40 6
        return $this->normalizer->denormalize($arrayData, $output);
41
    }
42
}
43