Completed
Push — master ( c00bbf...06e52a )
by Pavel
17:08 queued 22s
created

JmsContextFactory::deserialization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1.037
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use JMS\Serializer\Context;
6
use JMS\Serializer\DeserializationContext;
7
use JMS\Serializer\SerializationContext;
8
9
final class JmsContextFactory
10
{
11
    /**
12
     * @param null|array $context
13
     *
14
     * @return SerializationContext
15
     */
16 13
    public static function serialization(array $context = null)
17
    {
18 13
        $jmsContext = SerializationContext::create();
19
20 13
        self::configureContext($jmsContext, $context);
21
22 13
        return $jmsContext;
23
    }
24
25 2
    public static function deserialization(array $context = null)
26
    {
27 2
        $jmsContext = DeserializationContext::create();
28
29 2
        self::configureContext($jmsContext, $context);
30
31 2
        return $jmsContext;
32
    }
33
34 13
    private static function configureContext(Context $jmsContext, array $context = null)
35
    {
36 13
        $jmsContext->setSerializeNull(true);
37
38 13
        if (null === $context) {
39
            return;
40
        }
41
42 13
        if (array_key_exists('groups', $context)) {
43
            $jmsContext->setGroups($context['groups']);
44
        }
45
46 13
        if (array_key_exists('object_to_populate', $context)) {
47 2
            $jmsContext->setAttribute('target', $context['object_to_populate']);
48
        }
49 13
    }
50
}
51