Completed
Pull Request — master (#1125)
by
unknown
21:30
created

Callback   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.1%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 18
c 4
b 1
f 1
lcom 1
cbo 1
dl 0
loc 82
ccs 27
cts 31
cp 0.871
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setGroups() 0 8 4
A setSerializer() 0 7 2
A setVersion() 0 8 3
A setSerializeNull() 0 8 3
B serialize() 0 22 6
1
<?php
2
3
namespace FOS\ElasticaBundle\Serializer;
4
5
use JMS\Serializer\SerializationContext;
6
use Symfony\Component\Serializer\SerializerInterface;
7
use JMS\Serializer\SerializerInterface as JMSSerializer;
8
9
class Callback
10
{
11
    protected $serializer;
12
    protected $groups = array();
13
    protected $version;
14
    protected $serializeNull;
15
16
    /**
17
     * @param $serializer
18
     */
19 5
    public function setSerializer($serializer)
20
    {
21 5
        $this->serializer = $serializer;
22 5
        if (!method_exists($this->serializer, 'serialize')) {
23 1
            throw new \RuntimeException('The serializer must have a "serialize" method.');
24
        }
25 4
    }
26
27
    /**
28
     * @param array $groups
29
     */
30 4
    public function setGroups(array $groups)
31
    {
32 4
        $this->groups = $groups;
33
34 4
        if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface && !$this->serializer instanceof JMSSerializer) {
35
            throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer" or "Symfony\Component\Serializer\Serializer"');
36
        }
37 4
    }
38
39
    /**
40
     * @param $version
41
     */
42 3
    public function setVersion($version)
43
    {
44 3
        $this->version = $version;
45
46 3
        if ($this->version && !$this->serializer instanceof JMSSerializer) {
47
            throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
48
        }
49 3
    }
50
51
    /**
52
     * @param $serializeNull
53
     */
54 3
    public function setSerializeNull($serializeNull)
55
    {
56 3
        $this->serializeNull = $serializeNull;
57
58 3
        if (true === $this->serializeNull && !$this->serializer instanceof JMSSerializer) {
59
            throw new \RuntimeException('Setting null value serialization option requires using "JMS\Serializer\Serializer".');
60
        }
61 3
    }
62
63
    /**
64
     * @param $object
65
     *
66
     * @return mixed
67
     */
68 2
    public function serialize($object)
69
    {
70 2
        $context = $this->serializer instanceof JMSSerializer ? SerializationContext::create()->enableMaxDepthChecks() : array();
71
72 2
        if (!empty($this->groups)) {
73 1
            if ($context instanceof SerializationContext) {
74 1
                $context->setGroups($this->groups);
75
            } else {
76
                $context['groups'] = $this->groups;
77
            }
78
        }
79
80 2
        if ($this->version) {
81 1
            $context->setVersion($this->version);
82
        }
83
84 2
        if (!is_array($context)) {
85 2
          $context->setSerializeNull($this->serializeNull);
86
        }
87
88 2
        return $this->serializer->serialize($object, 'json', $context);
89
    }
90
}
91