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

SymfonySerializerAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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 Symfony\Component\Serializer\Serializer;
7
use Symfony\Component\Serializer\SerializerInterface as SymfonySerializerInterface;
8
9
/**
10
 * Adapter to plug the Symfony serializer into the FOSRestBundle Serializer API.
11
 */
12
class SymfonySerializerAdapter implements SerializerInterface
13
{
14
    /**
15
     * @var SymfonySerializerInterface|Serializer
16
     */
17
    private $serializer;
18
19
    public function __construct(SymfonySerializerInterface $serializer)
20
    {
21
        $this->serializer = $serializer;
22
    }
23
24
    /**
25
     * Serializes the given data to the specified output format.
26
     *
27
     * @param object|array|scalar $data
28
     * @param string $format
29
     * @param ContextInterface|null|Context $context
30
     *
31
     * @return string
32
     */
33
    public function serialize($data, $format, ContextInterface $context = null)
34
    {
35
        $newContext = $this->convertContext($context);
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...apter::convertContext() does only seem to accept 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

35
        $newContext = $this->convertContext(/** @scrutinizer ignore-type */ $context);
Loading history...
36
        $newContext['serializeNull'] = $context->getSerializeNull();
0 ignored issues
show
Bug introduced by
The method getSerializeNull() 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

36
        /** @scrutinizer ignore-call */ 
37
        $newContext['serializeNull'] = $context->getSerializeNull();

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...
37
38
        return $this->serializer->serialize($data, $format, $newContext);
39
    }
40
41
    /**
42
     * @param string $data
43
     * @param string $type
44
     * @param string $format
45
     * @param ContextInterface|null|Context $context
46
     *
47
     * @return object
48
     */
49
    public function deserialize($data, $type, $format, ContextInterface $context = null)
50
    {
51
        $newContext = $this->convertContext($context);
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...apter::convertContext() does only seem to accept 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

51
        $newContext = $this->convertContext(/** @scrutinizer ignore-type */ $context);
Loading history...
52
53
        return $this->serializer->deserialize($data, $type, $format, $newContext);
54
    }
55
56
    /**
57
     * @param Context $context
58
     *
59
     * @return array
60
     */
61
    private function convertContext(Context $context)
62
    {
63
        $newContext = array();
64
        foreach ($context->getAttributes() as $key => $value) {
65
            $newContext[$key] = $value;
66
        }
67
68
        if (null !== $context->getGroups()) {
69
            $newContext['groups'] = $context->getGroups();
70
        }
71
        $newContext['version'] = $context->getVersion();
72
        $newContext['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

72
        $newContext['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...
73
        $newContext['enable_max_depth'] = $context->isMaxDepthEnabled();
74
75
        return $newContext;
76
    }
77
78
    /**
79
     * Converts objects to an array structure.
80
     *
81
     * This is useful when the data needs to be passed on to other methods which expect array data.
82
     *
83
     * @param mixed $data anything that converts to an array, typically an object or an array of objects
84
     * @param ContextInterface|null $context
85
     *
86
     * @return array
87
     */
88
    public function toArray($data, ContextInterface $context = null)
89
    {
90
        $newContext = $this->convertContext($context);
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...apter::convertContext() does only seem to accept 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

90
        $newContext = $this->convertContext(/** @scrutinizer ignore-type */ $context);
Loading history...
Unused Code introduced by
The assignment to $newContext is dead and can be removed.
Loading history...
91
92
        $this->serializer->normalize($data, null, $context);
0 ignored issues
show
Bug introduced by
$context of type null|CCT\Component\Rest\...alizer\ContextInterface is incompatible with the type array expected by parameter $context of Symfony\Component\Serial...Serializer::normalize(). ( Ignorable by Annotation )

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

92
        $this->serializer->normalize($data, null, /** @scrutinizer ignore-type */ $context);
Loading history...
93
    }
94
95
    /**
96
     * Restores objects from an array structure.
97
     *
98
     * @param array $data
99
     * @param string $type
100
     * @param ContextInterface|null $context
101
     *
102
     * @return mixed this returns whatever the passed type is, typically an object or an array of objects
103
     */
104
    public function fromArray(array $data, $type, ContextInterface $context = null)
105
    {
106
        $newContext = $this->convertContext($context);
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...apter::convertContext() does only seem to accept 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

106
        $newContext = $this->convertContext(/** @scrutinizer ignore-type */ $context);
Loading history...
107
108
        $this->serializer->denormalize($data, $type, null, $newContext);
0 ignored issues
show
Bug introduced by
The method denormalize() does not exist on Symfony\Component\Serializer\SerializerInterface. It seems like you code against a sub-type of Symfony\Component\Serializer\SerializerInterface such as Symfony\Component\Serializer\Serializer. ( Ignorable by Annotation )

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

108
        $this->serializer->/** @scrutinizer ignore-call */ 
109
                           denormalize($data, $type, null, $newContext);
Loading history...
109
    }
110
}
111