Completed
Push — master ( 2e0cd6...c623e8 )
by Pavel
12:31
created

JmsSerializerAdapter::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
crap 2.0932
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use JMS\Serializer\DeserializationContext;
6
use JMS\Serializer\SerializationContext;
7
use JMS\Serializer\Serializer;
8
use JMS\Serializer\SerializerInterface as JmsSerializer;
9
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10
use Symfony\Component\Serializer\SerializerInterface;
11
12
final class JmsSerializerAdapter implements SerializerInterface, NormalizerInterface
13
{
14
    /** @var  JmsSerializer */
15
    private $serializer;
16
17
    /**
18
     * JmsSerializerAdapter constructor.
19
     *
20
     * @param JmsSerializer $serializer
21
     */
22 2
    public function __construct(JmsSerializer $serializer)
23
    {
24 2
        $this->serializer = $serializer;
25 2
    }
26
27
    /** {@inheritdoc} */
28 2 View Code Duplication
    public function serialize($data, $format, array $context = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30 2
        $jmsContext = SerializationContext::create();
31 2
        if (array_key_exists('groups', $context)) {
32
            $jmsContext->setGroups($context['groups']);
33
        }
34 2
        $jmsContext->setSerializeNull(true);
35
36 2
        return $this->serializer->serialize($data, $format, $jmsContext);
37
    }
38
39
    /** {@inheritdoc} */
40 View Code Duplication
    public function deserialize($data, $type, $format, array $context = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $jmsContext = DeserializationContext::create();
43
        if (array_key_exists('groups', $context)) {
44
            $jmsContext->setGroups($context['groups']);
45
        }
46
        $jmsContext->setSerializeNull(true);
47
48
        $this->serializer->deserialize($data, $type, $format, $jmsContext);
49
    }
50
51
    /** {@inheritdoc} */
52 2
    public function normalize($object, $format = null, array $context = [])
53
    {
54 2
        $jmsContext = SerializationContext::create();
55 2
        if (array_key_exists('groups', $context)) {
56
            $jmsContext->setGroups($context['groups']);
57
        }
58 2
        $jmsContext->setSerializeNull(true);
59
60 2
        if ($this->serializer instanceof Serializer) {
61 2
            return $this->serializer->toArray($object, $jmsContext);
62
        }
63
64
        return json_decode($this->serializer->serialize($object, 'json', $jmsContext), true);
65
    }
66
67
    /** {@inheritdoc} */
68
    public function supportsNormalization($data, $format = null)
69
    {
70
        return true;
71
    }
72
}
73