Failed Conditions
Pull Request — master (#39)
by Timon
03:26
created

QueryNormalizer::normalize()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

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