Passed
Push — master ( 8f68b8...9c6953 )
by Paul
02:02
created

JMSSerializerAdapter::convertContext()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 2
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace CCT\Component\Rest\Serializer;
4
5
use CCT\Component\Rest\Serializer\Context\Context;
6
use JMS\Serializer\Context as JMSContext;
7
use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface;
8
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
9
use JMS\Serializer\DeserializationContext;
10
use JMS\Serializer\SerializationContext;
11
use JMS\Serializer\Serializer;
12
use JMS\Serializer\SerializerInterface as JMSSerializerInterface;
13
14
/**
15
 * Adapter to plug the JMS serializer into the FOSRestBundle Serializer API.
16
 */
17
class JMSSerializerAdapter implements SerializerInterface
18
{
19
    /**
20
     * @internal
21
     */
22
    const SERIALIZATION = 0;
23
    /**
24
     * @internal
25
     */
26
    const DESERIALIZATION = 1;
27
28
    /**
29
     * @var JMSSerializerInterface|Serializer
30
     */
31
    private $serializer;
32
33
    /**
34
     * @var SerializationContextFactoryInterface
35
     */
36
    private $serializationContextFactory;
37
38
    /**
39
     * @var DeserializationContextFactoryInterface
40
     */
41
    private $deserializationContextFactory;
42
43
    public function __construct(
44
        JMSSerializerInterface $serializer,
45
        SerializationContextFactoryInterface $serializationContextFactory = null,
46
        DeserializationContextFactoryInterface $deserializationContextFactory = null
47
    ) {
48
        $this->serializer = $serializer;
49
        $this->serializationContextFactory = $serializationContextFactory;
50
        $this->deserializationContextFactory = $deserializationContextFactory;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function serialize($data, $format, ContextInterface $context = null)
57
    {
58
        $context = $this->convertContext($context, self::SERIALIZATION);
59
60
        return $this->serializer->serialize($data, $format, $context);
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type JMS\Serializer\DeserializationContext; however, parameter $context of JMS\Serializer\SerializerInterface::serialize() does only seem to accept JMS\Serializer\SerializationContext|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        return $this->serializer->serialize($data, $format, /** @scrutinizer ignore-type */ $context);
Loading history...
Bug introduced by
It seems like $context can also be of type JMS\Serializer\DeserializationContext; however, parameter $context of JMS\Serializer\Serializer::serialize() does only seem to accept JMS\Serializer\SerializationContext|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        return $this->serializer->serialize($data, $format, /** @scrutinizer ignore-type */ $context);
Loading history...
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function deserialize($data, $type, $format, ContextInterface $context = null)
67
    {
68
        $context = $this->convertContext($context, self::DESERIALIZATION);
69
70
        return $this->serializer->deserialize($data, $type, $format, $context);
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type JMS\Serializer\SerializationContext; however, parameter $context of JMS\Serializer\SerializerInterface::deserialize() does only seem to accept null|JMS\Serializer\DeserializationContext, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        return $this->serializer->deserialize($data, $type, $format, /** @scrutinizer ignore-type */ $context);
Loading history...
Bug introduced by
It seems like $context can also be of type JMS\Serializer\SerializationContext; however, parameter $context of JMS\Serializer\Serializer::deserialize() does only seem to accept null|JMS\Serializer\DeserializationContext, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        return $this->serializer->deserialize($data, $type, $format, /** @scrutinizer ignore-type */ $context);
Loading history...
71
    }
72
73
    /**
74
     * @param ContextInterface|Context $context
75
     * @param int $direction {@see self} constants
76
     *
77
     * @return JMSContext|DeserializationContext|SerializationContext
78
     */
79
    private function convertContext(ContextInterface $context = null, $direction)
80
    {
81
        if (self::SERIALIZATION === $direction) {
82
            $jmsContext = $this->serializationContextFactory
83
                ? $this->serializationContextFactory->createSerializationContext()
84
                : SerializationContext::create();
85
        } else {
86
            $jmsContext = $this->deserializationContextFactory
87
                ? $this->deserializationContextFactory->createDeserializationContext()
88
                : DeserializationContext::create();
89
            $maxDepth = $context->getMaxDepth();
0 ignored issues
show
Bug introduced by
The method getMaxDepth() does not exist on CCT\Component\Rest\Serializer\ContextInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to CCT\Component\Rest\Serializer\ContextInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
            /** @scrutinizer ignore-call */ 
90
            $maxDepth = $context->getMaxDepth();
Loading history...
Bug introduced by
The method getMaxDepth() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
            /** @scrutinizer ignore-call */ 
90
            $maxDepth = $context->getMaxDepth();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
            if (null !== $maxDepth) {
91
                for ($i = 0; $i < $maxDepth; ++$i) {
92
                    $jmsContext->increaseDepth();
93
                }
94
            }
95
        }
96
97
        return $this->mapContextAttributes($context, $jmsContext);
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type null; however, parameter $context of CCT\Component\Rest\Seria...:mapContextAttributes() does only seem to accept CCT\Component\Rest\Serializer\ContextInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        return $this->mapContextAttributes(/** @scrutinizer ignore-type */ $context, $jmsContext);
Loading history...
98
    }
99
100
    /**
101
     * Set jms context attributes from context
102
     *
103
     * @param ContextInterface|Context $context
104
     * @param JMSContext $jmsContext
105
     *
106
     * @return JMSContext|DeserializationContext|SerializationContext
107
     */
108
    private function mapContextAttributes(ContextInterface $context, JMSContext $jmsContext)
109
    {
110
        foreach ($context->getAttributes() as $key => $value) {
111
            $jmsContext->attributes->set($key, $value);
112
        }
113
        if (null !== $context->getVersion()) {
114
            $jmsContext->setVersion($context->getVersion());
115
        }
116
        if (null !== $context->getGroups()) {
117
            $jmsContext->setGroups($context->getGroups());
118
        }
119
        if (null !== $context->isMaxDepthEnabled()) {
120
            $jmsContext->enableMaxDepthChecks();
121
        }
122
        if (null !== $context->getSerializeNull()) {
123
            $jmsContext->setSerializeNull($context->getSerializeNull());
124
        }
125
126
        foreach ($context->getExclusionStrategies() as $strategy) {
127
            $jmsContext->addExclusionStrategy($strategy);
128
        }
129
130
        return $jmsContext;
131
    }
132
133
    /**
134
     * Converts objects to an array structure.
135
     *
136
     * This is useful when the data needs to be passed on to other methods which expect array data.
137
     *
138
     * @param mixed $data anything that converts to an array, typically an object or an array of objects
139
     * @param ContextInterface|null $context
140
     *
141
     * @return array
142
     */
143
    public function toArray($data, ContextInterface $context = null)
144
    {
145
        $context = $this->convertContext($context, self::SERIALIZATION);
146
147
        return $this->serializer->toArray($data, $context);
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type JMS\Serializer\DeserializationContext; however, parameter $context of JMS\Serializer\Serializer::toArray() does only seem to accept JMS\Serializer\SerializationContext|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
        return $this->serializer->toArray($data, /** @scrutinizer ignore-type */ $context);
Loading history...
148
    }
149
150
    /**
151
     * Restores objects from an array structure.
152
     *
153
     * @param array $data
154
     * @param string $type
155
     * @param ContextInterface|null $context
156
     *
157
     * @return mixed this returns whatever the passed type is, typically an object or an array of objects
158
     */
159
    public function fromArray(array $data, $type, ContextInterface $context = null)
160
    {
161
        $context = $this->convertContext($context, self::DESERIALIZATION);
162
163
        return $this->serializer->fromArray($data, $context);
164
    }
165
}
166