Completed
Push — master ( b5207e...bb017f )
by Guilh
06:54
created

JMSSerializerAdapter::convertContext()   D

Complexity

Conditions 11
Paths 224

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 23.2998

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 16
cts 30
cp 0.5333
rs 4.3063
c 0
b 0
f 0
cc 11
eloc 22
nc 224
nop 2
crap 23.2998

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Serializer;
13
14
use FOS\RestBundle\Context\Context;
15
use JMS\Serializer\Context as JMSContext;
16
use JMS\Serializer\DeserializationContext as JMSDeserializationContext;
17
use JMS\Serializer\SerializationContext as JMSSerializationContext;
18
use JMS\Serializer\SerializerInterface;
19
20
/**
21
 * Adapter to plug the JMS serializer into the FOSRestBundle Serializer API.
22
 *
23
 * @author Christian Flothmann <[email protected]>
24
 */
25
class JMSSerializerAdapter implements Serializer
26
{
27
    /**
28
     * @internal
29
     */
30
    const SERIALIZATION = 0;
31
    /**
32
     * @internal
33
     */
34
    const DESERIALIZATION = 1;
35
36
    private $serializer;
37
38 5
    public function __construct(SerializerInterface $serializer)
39
    {
40 5
        $this->serializer = $serializer;
41 5
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 5
    public function serialize($data, $format, Context $context)
47
    {
48 5
        $context = $this->convertContext($context, self::SERIALIZATION);
49
50 5
        return $this->serializer->serialize($data, $format, $context);
0 ignored issues
show
Bug introduced by
It seems like $context defined by $this->convertContext($c...t, self::SERIALIZATION) on line 48 can also be of type object<JMS\Serializer\DeserializationContext>; however, JMS\Serializer\SerializerInterface::serialize() does only seem to accept null|object<JMS\Serializer\SerializationContext>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function deserialize($data, $type, $format, Context $context)
57
    {
58
        $context = $this->convertContext($context, self::DESERIALIZATION);
59
60
        return $this->serializer->deserialize($data, $type, $format, $context);
0 ignored issues
show
Bug introduced by
It seems like $context defined by $this->convertContext($c... self::DESERIALIZATION) on line 58 can also be of type object<JMS\Serializer\SerializationContext>; however, JMS\Serializer\SerializerInterface::deserialize() does only seem to accept null|object<JMS\Serializ...DeserializationContext>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
    }
62
63
    /**
64
     * @param Context $context
65
     * @param int     $direction {@see self} constants
66
     *
67
     * @return JMSContext
68
     */
69 5
    private function convertContext(Context $context, $direction)
70
    {
71 5
        if ($direction === self::SERIALIZATION) {
72 5
            $jmsContext = JMSSerializationContext::create();
73 5
        } else {
74
            $jmsContext = JMSDeserializationContext::create();
75
            $maxDepth = $context->getMaxDepth(false);
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\Context\Context::getMaxDepth() has been deprecated with message: since version 2.1, to be removed in 3.0. Use {@link self::isMaxDepthEnabled()} instead

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

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

Loading history...
76
            if (null !== $maxDepth) {
77
                for ($i = 0; $i < $maxDepth; ++$i) {
78
                    $jmsContext->increaseDepth();
79
                }
80
            }
81
        }
82
83 5
        foreach ($context->getAttributes() as $key => $value) {
84 5
            $jmsContext->attributes->set($key, $value);
85 5
        }
86
87 5
        if (null !== $context->getVersion()) {
88
            $jmsContext->setVersion($context->getVersion());
89
        }
90 5
        if (null !== $context->getGroups()) {
91
            $jmsContext->setGroups($context->getGroups());
92
        }
93 5
        if (null !== $context->getMaxDepth(false) || null !== $context->isMaxDepthEnabled()) {
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\Context\Context::getMaxDepth() has been deprecated with message: since version 2.1, to be removed in 3.0. Use {@link self::isMaxDepthEnabled()} instead

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

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

Loading history...
94
            $jmsContext->enableMaxDepthChecks();
95
        }
96 5
        if (null !== $context->getSerializeNull()) {
97 5
            $jmsContext->setSerializeNull($context->getSerializeNull());
98 5
        }
99
100 5
        foreach($context->getExclusionStrategies() as $strategy) {
101
            $jmsContext->addExclusionStrategy($strategy);
102 5
        }
103
104 5
        return $jmsContext;
105
    }
106
}
107