Completed
Push — master ( 98c39c...c8155d )
by Pavel
16:50
created

JmsContextFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 42
ccs 15
cts 25
cp 0.6
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 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