Passed
Pull Request — master (#11)
by Michel
02:36
created

QueryNormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

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