Completed
Pull Request — master (#556)
by Evgenij
02:42
created

ConfiguredContextFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createDeserializationContext() 0 4 1
A createSerializationContext() 0 4 1
A configureContext() 0 14 3
1
<?php
2
3
namespace JMS\SerializerBundle\ContextFactory;
4
5
use JMS\Serializer\Context;
6
use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface;
7
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
8
use JMS\Serializer\DeserializationContext;
9
use JMS\Serializer\SerializationContext;
10
11
/**
12
 * Class ConfiguredContextFactory
13
 */
14
class ConfiguredContextFactory implements SerializationContextFactoryInterface, DeserializationContextFactoryInterface
15
{
16
    /**
17
     * Context config
18
     *
19
     * @var array
20
     */
21
    private $config;
22
23
    /**
24
     * ConfiguredContextFactory constructor.
25
     *
26
     * @param array $config Context configuration
27
     */
28
    public function __construct(array $config)
29
    {
30
        $this->config = $config;
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public function createDeserializationContext()
37
    {
38
        return $this->configureContext(new DeserializationContext());
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function createSerializationContext()
45
    {
46
        return $this->configureContext(new SerializationContext());
47
    }
48
49
    /**
50
     * Configures context according to configuration
51
     *
52
     * @param Context $context The context
53
     *
54
     * @return Context Given object
55
     */
56
    private function configureContext(Context $context)
57
    {
58
        foreach ($this->config['attributes'] as $key => $value) {
59
            $context->setAttribute($key, $value);
60
        }
61
62
        $context->setGroups($this->config['groups']);
63
        $context->setSerializeNull($this->config['serialize_null']);
64
        if ($this->config['version'] !== null) {
65
            $context->setVersion($this->config['version']);
66
        }
67
68
        return $context;
69
    }
70
}
71