Completed
Push — master ( d2df87...5f50d2 )
by Christian
15s
created

JMSSerializerAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
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\ContextFactory\DeserializationContextFactoryInterface;
17
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
18
use JMS\Serializer\DeserializationContext;
19
use JMS\Serializer\SerializationContext;
20
use JMS\Serializer\SerializerInterface;
21
22
/**
23
 * Adapter to plug the JMS serializer into the FOSRestBundle Serializer API.
24
 *
25
 * @author Christian Flothmann <[email protected]>
26
 */
27
class JMSSerializerAdapter implements Serializer
28
{
29
    /**
30
     * @internal
31
     */
32
    const SERIALIZATION = 0;
33
    /**
34
     * @internal
35
     */
36
    const DESERIALIZATION = 1;
37
38 5
    private $serializer;
39
    private $serializationContextFactory;
40 5
    private $deserializationContextFactory;
41 5
42
    public function __construct(
43
        SerializerInterface $serializer,
44
        SerializationContextFactoryInterface $serializationContextFactory = null,
45
        DeserializationContextFactoryInterface $deserializationContextFactory = null
46 5
    ) {
47
        $this->serializer = $serializer;
48 5
        $this->serializationContextFactory = $serializationContextFactory;
49
        $this->deserializationContextFactory = $deserializationContextFactory;
50 5
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function serialize($data, $format, Context $context)
56
    {
57
        $context = $this->convertContext($context, self::SERIALIZATION);
58
59
        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 57 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...
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function deserialize($data, $type, $format, Context $context)
66
    {
67
        $context = $this->convertContext($context, self::DESERIALIZATION);
68
69 5
        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 67 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...
70
    }
71 5
72 5
    /**
73 5
     * @param Context $context
74
     * @param int     $direction {@see self} constants
75
     *
76
     * @return JMSContext
77
     */
78
    private function convertContext(Context $context, $direction)
79
    {
80
        if ($direction === self::SERIALIZATION) {
81
            $jmsContext = $this->serializationContextFactory
82
                ? $this->serializationContextFactory->createSerializationContext()
83 5
                : SerializationContext::create();
84 5
        } else {
85 5
            $jmsContext = $this->deserializationContextFactory
86
                ? $this->deserializationContextFactory->createDeserializationContext()
87 5
                : DeserializationContext::create();
88
            $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...
89
            if (null !== $maxDepth) {
90 5
                for ($i = 0; $i < $maxDepth; ++$i) {
91
                    $jmsContext->increaseDepth();
92
                }
93 5
            }
94
        }
95
96 5
        foreach ($context->getAttributes() as $key => $value) {
97 5
            $jmsContext->attributes->set($key, $value);
98 5
        }
99
100 5
        if (null !== $context->getVersion()) {
101
            $jmsContext->setVersion($context->getVersion());
102 5
        }
103
        if (null !== $context->getGroups()) {
104 5
            $jmsContext->setGroups($context->getGroups());
105
        }
106
        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...
107
            $jmsContext->enableMaxDepthChecks();
108
        }
109
        if (null !== $context->getSerializeNull()) {
110
            $jmsContext->setSerializeNull($context->getSerializeNull());
111
        }
112
113
        foreach ($context->getExclusionStrategies() as $strategy) {
114
            $jmsContext->addExclusionStrategy($strategy);
115
        }
116
117
        return $jmsContext;
118
    }
119
}
120