Passed
Push — master ( 9c6953...58b0d3 )
by Paul
02:03
created

JMSSerializerAdapter::mapMaxDepth()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 13
rs 9.2
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 = self::SERIALIZATION)
80
    {
81
        if (null === $context) {
82
            return null;
83
        }
84
85
        $jmsContext = self::SERIALIZATION === $direction
86
            ? $this->createSerializationContext()
87
            : $this->createDeserializationContext();
88
89
        return $this->mapContextAttributes($context, $jmsContext);
90
    }
91
92
    private function createSerializationContext()
93
    {
94
        return $this->serializationContextFactory
95
            ? $this->serializationContextFactory->createSerializationContext()
96
            : SerializationContext::create();
97
    }
98
99
    private function createDeserializationContext()
100
    {
101
        return $jmsContext = $this->deserializationContextFactory
0 ignored issues
show
Unused Code introduced by
The assignment to $jmsContext is dead and can be removed.
Loading history...
102
            ? $this->deserializationContextFactory->createDeserializationContext()
103
            : DeserializationContext::create();
104
    }
105
106
    /**
107
     * Set jms context attributes from context
108
     *
109
     * @param ContextInterface|Context $context
110
     * @param JMSContext $jmsContext
111
     *
112
     * @return JMSContext|DeserializationContext|SerializationContext
113
     */
114
    private function mapContextAttributes(ContextInterface $context, JMSContext $jmsContext)
115
    {
116
117
        $this->mapMaxDepth($context, $jmsContext);
118
        foreach ($context->getAttributes() as $key => $value) {
119
            $jmsContext->attributes->set($key, $value);
120
        }
121
        if (null !== $context->getVersion()) {
122
            $jmsContext->setVersion($context->getVersion());
123
        }
124
        if (null !== $context->getGroups()) {
125
            $jmsContext->setGroups($context->getGroups());
126
        }
127
        if (null !== $context->isMaxDepthEnabled()) {
128
            $jmsContext->enableMaxDepthChecks();
129
        }
130
        if (null !== $context->getSerializeNull()) {
131
            $jmsContext->setSerializeNull($context->getSerializeNull());
132
        }
133
134
        foreach ($context->getExclusionStrategies() as $strategy) {
135
            $jmsContext->addExclusionStrategy($strategy);
136
        }
137
138
        return $jmsContext;
139
    }
140
141
    /**
142
     * @param ContextInterface $context
143
     * @param JMSContext|DeserializationContext|SerializationContext $jmsContext
144
     */
145
    private function mapMaxDepth(ContextInterface $context, JMSContext $jmsContext)
146
    {
147
        if ($jmsContext instanceof DeserializationContext) {
148
            return;
149
        }
150
151
        $maxDepth = $context->getMaxDepth();
152
        if (null === $maxDepth) {
153
            return;
154
        }
155
156
        for ($i = 0; $i < $maxDepth; ++$i) {
157
            $jmsContext->increaseDepth();
0 ignored issues
show
Bug introduced by
The method increaseDepth() does not exist on JMS\Serializer\Context. It seems like you code against a sub-type of JMS\Serializer\Context such as JMS\Serializer\DeserializationContext. ( Ignorable by Annotation )

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

157
            $jmsContext->/** @scrutinizer ignore-call */ 
158
                         increaseDepth();
Loading history...
158
        }
159
    }
160
161
    /**
162
     * Converts objects to an array structure.
163
     *
164
     * This is useful when the data needs to be passed on to other methods which expect array data.
165
     *
166
     * @param mixed $data anything that converts to an array, typically an object or an array of objects
167
     * @param ContextInterface|null $context
168
     *
169
     * @return array
170
     */
171
    public function toArray($data, ContextInterface $context = null)
172
    {
173
        $context = $this->convertContext($context, self::SERIALIZATION);
174
175
        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

175
        return $this->serializer->toArray($data, /** @scrutinizer ignore-type */ $context);
Loading history...
176
    }
177
178
    /**
179
     * Restores objects from an array structure.
180
     *
181
     * @param array $data
182
     * @param string $type
183
     * @param ContextInterface|null $context
184
     *
185
     * @return mixed this returns whatever the passed type is, typically an object or an array of objects
186
     */
187
    public function fromArray(array $data, $type, ContextInterface $context = null)
188
    {
189
        $context = $this->convertContext($context, self::DESERIALIZATION);
190
191
        return $this->serializer->fromArray($data, $context);
192
    }
193
}
194