Passed
Push — master ( 783125...33c283 )
by Paul
02:18
created

JMSSerializerAdapter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 140
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 5 1
A toArray() 0 5 1
F convertContext() 0 40 12
A __construct() 0 8 1
A fromArray() 0 5 1
A deserialize() 0 9 2
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CCT\Component\Rest\Serializer\SerializerInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
14
/**
15
 * Adapter to plug the JMS serializer into the FOSRestBundle Serializer API.
16
 */
17
class JMSSerializerAdapter implements \CCT\Component\Rest\Serializer\SerializerInterface
18
{
19
    /**
20
     * @internal
21
     */
22
    const SERIALIZATION = 0;
23
24
    /**
25
     * @internal
26
     */
27
    const DESERIALIZATION = 1;
28
29
    /**
30
     * @var SerializerInterface|Serializer
31
     */
32
    private $serializer;
33
34
    /**
35
     * @var SerializationContextFactoryInterface
36
     */
37
    private $serializationContextFactory;
38
39
    /**
40
     * @var DeserializationContextFactoryInterface
41
     */
42
    private $deserializationContextFactory;
43
44
    public function __construct(
45
        SerializerInterface $serializer,
46
        SerializationContextFactoryInterface $serializationContextFactory = null,
47
        DeserializationContextFactoryInterface $deserializationContextFactory = null
48
    ) {
49
        $this->serializer = $serializer;
50
        $this->serializationContextFactory = $serializationContextFactory;
51
        $this->deserializationContextFactory = $deserializationContextFactory;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function serialize($data, $format, ContextInterface $context = null)
58
    {
59
        $context = $this->convertContext($context, self::SERIALIZATION);
60
61
        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\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

61
        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\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

61
        return $this->serializer->serialize($data, $format, /** @scrutinizer ignore-type */ $context);
Loading history...
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function deserialize($data, $type, $format, ContextInterface $context = null)
68
    {
69
        if (null === $context) {
70
            $context = $this->deserializationContextFactory->createDeserializationContext();
71
        }
72
73
        $context = $this->convertContext($context, self::DESERIALIZATION);
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type JMS\Serializer\DeserializationContext; however, parameter $context of CCT\Component\Rest\Seria...apter::convertContext() does only seem to accept null|CCT\Component\Rest\Serializer\Context\Context, 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

73
        $context = $this->convertContext(/** @scrutinizer ignore-type */ $context, self::DESERIALIZATION);
Loading history...
74
75
        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

75
        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

75
        return $this->serializer->deserialize($data, $type, $format, /** @scrutinizer ignore-type */ $context);
Loading history...
76
    }
77
78
    /**
79
     * @param Context $context
80
     * @param int $direction {@see self} constants
81
     *
82
     * @return JMSContext
83
     */
84
    private function convertContext(Context $context = null, $direction)
85
    {
86
        if (self::SERIALIZATION === $direction) {
87
            $jmsContext = $this->serializationContextFactory
88
                ? $this->serializationContextFactory->createSerializationContext()
89
                : SerializationContext::create();
90
        } else {
91
            $jmsContext = $this->deserializationContextFactory
92
                ? $this->deserializationContextFactory->createDeserializationContext()
93
                : DeserializationContext::create();
94
            $maxDepth = $context->getMaxDepth(false);
0 ignored issues
show
Deprecated Code introduced by
The function CCT\Component\Rest\Seria...\Context::getMaxDepth() has been deprecated: since version 2.1, to be removed in 3.0. Use {@link self::isMaxDepthEnabled()} instead ( Ignorable by Annotation )

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

94
            $maxDepth = /** @scrutinizer ignore-deprecated */ $context->getMaxDepth(false);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

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

94
            /** @scrutinizer ignore-call */ 
95
            $maxDepth = $context->getMaxDepth(false);

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...
95
            if (null !== $maxDepth) {
0 ignored issues
show
introduced by
The condition null !== $maxDepth is always true.
Loading history...
96
                for ($i = 0; $i < $maxDepth; ++$i) {
97
                    $jmsContext->increaseDepth();
98
                }
99
            }
100
        }
101
102
        foreach ($context->getAttributes() as $key => $value) {
103
            $jmsContext->attributes->set($key, $value);
104
        }
105
106
        if (null !== $context->getVersion()) {
107
            $jmsContext->setVersion($context->getVersion());
108
        }
109
        if (null !== $context->getGroups()) {
110
            $jmsContext->setGroups($context->getGroups());
111
        }
112
        if (null !== $context->isMaxDepthEnabled()) {
0 ignored issues
show
introduced by
The condition null !== $context->isMaxDepthEnabled() is always true.
Loading history...
113
            $jmsContext->enableMaxDepthChecks();
114
        }
115
        if (null !== $context->getSerializeNull()) {
0 ignored issues
show
introduced by
The condition null !== $context->getSerializeNull() is always true.
Loading history...
116
            $jmsContext->setSerializeNull($context->getSerializeNull());
117
        }
118
119
        foreach ($context->getExclusionStrategies() as $strategy) {
120
            $jmsContext->addExclusionStrategy($strategy);
121
        }
122
123
        return $jmsContext;
124
    }
125
126
    /**
127
     * Converts objects to an array structure.
128
     *
129
     * This is useful when the data needs to be passed on to other methods which expect array data.
130
     *
131
     * @param mixed $data anything that converts to an array, typically an object or an array of objects
132
     * @param ContextInterface|null $context
133
     *
134
     * @return array
135
     */
136
    public function toArray($data, ContextInterface $context = null)
137
    {
138
        $context = $this->convertContext($context, self::SERIALIZATION);
139
140
        $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

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