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

QueryNormalizer::normalize()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 6
nop 3
dl 0
loc 24
rs 6.7272
c 0
b 0
f 0
ccs 12
cts 12
cp 1
crap 7
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