Completed
Push — master ( 8eabd5...5d0f28 )
by Guilh
08:14
created

SymfonySerializerAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 6
c 4
b 1
f 0
lcom 1
cbo 2
dl 0
loc 50
ccs 20
cts 22
cp 0.9091
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deserialize() 0 6 1
A serialize() 0 7 1
A __construct() 0 4 1
A convertContext() 0 16 3
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 Symfony\Component\Serializer\SerializerInterface;
16
17
/**
18
 * Adapter to plug the Symfony serializer into the FOSRestBundle Serializer API.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
class SymfonySerializerAdapter implements Serializer
23
{
24
    private $serializer;
25
26 18
    public function __construct(SerializerInterface $serializer)
27
    {
28 18
        $this->serializer = $serializer;
29 18
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 9
    public function serialize($data, $format, Context $context)
35
    {
36 9
        $newContext = $this->convertContext($context);
37 9
        $newContext['serializeNull'] = $context->getSerializeNull();
38
39 9
        return $this->serializer->serialize($data, $format, $newContext);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function deserialize($data, $type, $format, Context $context)
46
    {
47 2
        $newContext = $this->convertContext($context);
48
49 2
        return $this->serializer->deserialize($data, $type, $format, $newContext);
50
    }
51
52
    /**
53
     * @param Context $context
54
     */
55 11
    private function convertContext(Context $context)
56
    {
57 11
        $newContext = array();
58 11
        foreach ($context->getAttributes() as $key => $value) {
59 9
            $newContext[$key] = $value;
60 11
        }
61
62 11
        if (null !== $context->getGroups()) {
63
            $newContext['groups'] = $context->getGroups();
64
        }
65 11
        $newContext['version'] = $context->getVersion();
66 11
        $newContext['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...
67 11
        $newContext['enable_max_depth'] = $context->isMaxDepthEnabled();
68
69 11
        return $newContext;
70
    }
71
}
72