Completed
Push — master ( e93435...670f0c )
by Pavel
09:43
created

JmsContextFactory::configureContext()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.5021

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 6
cts 11
cp 0.5455
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 2
crap 5.5021
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 6
    public static function serialization(array $context = null)
17
    {
18 6
        return self::configureContext(SerializationContext::create(), $context);
19
    }
20
21
    public static function deserialization($context)
22
    {
23
        return self::configureContext(DeserializationContext::create(), $context);
24
    }
25
26 6
    private static function configureContext(Context $jmsContext, array $context = null)
27
    {
28 6
        $jmsContext->setSerializeNull(true);
29
30 6
        if (null === $context) {
31
            return $jmsContext;
32
        }
33
34 6
        if (array_key_exists('groups', $context)) {
35
            $jmsContext->setGroups($context['groups']);
36
        }
37
38 6
        if (array_key_exists('object_to_populate', $context)) {
39
            $jmsContext->setAttribute('target', $context['object_to_populate']);
40
        }
41
42 6
        return $jmsContext;
43
    }
44
}
45