JsonHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 31
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 7 2
A deserialize() 0 14 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