Completed
Push — master ( 06e52a...98c39c )
by Pavel
13:39 queued 10:37
created

JmsContextFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 42
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A serialization() 0 8 1
A deserialization() 0 8 1
A configureContext() 0 16 4
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
    public static function serialization(array $context = null)
17
    {
18
        $jmsContext = SerializationContext::create();
19
20
        self::configureContext($jmsContext, $context);
21
22
        return $jmsContext;
23
    }
24
25
    public static function deserialization(array $context = null)
26
    {
27
        $jmsContext = DeserializationContext::create();
28
29
        self::configureContext($jmsContext, $context);
30
31
        return $jmsContext;
32
    }
33
34
    private static function configureContext(Context $jmsContext, array $context = null)
35
    {
36
        $jmsContext->setSerializeNull(true);
37
38
        if (null === $context) {
39
            return;
40
        }
41
42
        if (array_key_exists('groups', $context)) {
43
            $jmsContext->setGroups($context['groups']);
44
        }
45
46
        if (array_key_exists('object_to_populate', $context)) {
47
            $jmsContext->setAttribute('target', $context['object_to_populate']);
48
        }
49
    }
50
}
51