Completed
Push — master ( 130df5...607289 )
by Pavel
04:43
created

JmsSerializerAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 60%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 45
ccs 9
cts 15
cp 0.6
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 4 1
A deserialize() 0 4 1
A normalize() 0 10 2
A supportsNormalization() 0 4 1
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use JMS\Serializer\Serializer;
6
use JMS\Serializer\SerializerInterface as JmsSerializer;
7
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
8
use Symfony\Component\Serializer\SerializerInterface;
9
10
final class JmsSerializerAdapter implements SerializerInterface, NormalizerInterface
11
{
12
    /** @var  JmsSerializer */
13
    private $serializer;
14
15
    /**
16
     * JmsSerializerAdapter constructor.
17
     *
18
     * @param JmsSerializer $serializer
19
     */
20 6
    public function __construct(JmsSerializer $serializer)
21
    {
22 6
        $this->serializer = $serializer;
23 6
    }
24
25
    /** {@inheritdoc} */
26 6
    public function serialize($data, $format, array $context = [])
27
    {
28 6
        return $this->serializer->serialize($data, $format, JmsContextFactory::serialization($context));
29
    }
30
31
    /** {@inheritdoc} */
32
    public function deserialize($data, $type, $format, array $context = [])
33
    {
34
        $this->serializer->deserialize($data, $type, $format, JmsContextFactory::deserialization($context));
0 ignored issues
show
Documentation introduced by
\ScayTrase\Api\Cruds\Ada...serialization($context) is of type object<JMS\Serializer\Context>, but the function expects a null|object<JMS\Serializ...DeserializationContext>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
    }
36
37
    /** {@inheritdoc} */
38 5
    public function normalize($object, $format = null, array $context = [])
39
    {
40 5
        $jmsContext = JmsContextFactory::serialization($context);
41
42 5
        if ($this->serializer instanceof Serializer) {
43 5
            return $this->serializer->toArray($object, $jmsContext);
44
        }
45
46
        return json_decode($this->serializer->serialize($object, 'json', $jmsContext), true);
47
    }
48
49
    /** {@inheritdoc} */
50
    public function supportsNormalization($data, $format = null)
51
    {
52
        return true;
53
    }
54
}
55