Passed
Push — master ( ae31ea...e44bbb )
by Michel
02:38
created

QueryNormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 55.56%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 54
ccs 10
cts 18
cp 0.5556
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C normalize() 0 24 7
A supportsNormalization() 0 4 2
A supportsDenormalization() 0 4 1
A denormalize() 0 4 1
1
<?php
2
3
4
namespace TBolier\RethinkQL\Serializer;
5
6
7
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
8
use Symfony\Component\Serializer\Exception\LogicException;
9
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
10
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
11
use TBolier\RethinkQL\Query\MessageInterface;
12
use TBolier\RethinkQL\Query\OptionsInterface;
13
14
class QueryNormalizer extends AbstractNormalizer
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 13
    public function normalize($object, $format = null, array $context = array())
20
    {
21 13
        if ($this->isCircularReference($object, $context)) {
22
            return $this->handleCircularReference($object);
23
        }
24
25 13
        if (!$object instanceof \JsonSerializable && !$object instanceof \stdClass) {
26
            throw new InvalidArgumentException(sprintf('The ' . get_class($object) . ' must implement "%s".', \JsonSerializable::class));
27
        }
28
29 13
        if (!$this->serializer instanceof NormalizerInterface) {
30
            throw new LogicException('Cannot normalize object because injected serializer is not a normalizer');
31
        }
32
33 13
        if ($object instanceof \stdClass) {
34
            return (array)$object;
35
        }
36
37 13
        if ($object instanceof OptionsInterface) {
38 13
            return (object)$object->jsonSerialize();
39
        }
40
41 13
        return $this->serializer->normalize($object->jsonSerialize(), $format, $context);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 13
    public function supportsNormalization($data, $format = null)
48
    {
49 13
        return \is_object($data) && $format === 'json';
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function supportsDenormalization($data, $type, $format = null)
56
    {
57
        return false;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function denormalize($data, $class, $format = null, array $context = array())
64
    {
65
        throw new LogicException(sprintf('Cannot denormalize with "%s".', __CLASS__));
66
    }
67
}
68