Completed
Push — master ( b458df...beeeff )
by Christian
06:18 queued 04:09
created

DefinitionNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 45
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSerializer() 0 4 1
B denormalize() 0 21 6
A supportsDenormalization() 0 4 1
1
<?php
2
3
namespace Xabbuh\XApi\Serializer\Normalizer;
4
5
use Symfony\Component\Serializer\Exception\LogicException;
6
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
7
use Symfony\Component\Serializer\SerializerAwareInterface;
8
use Symfony\Component\Serializer\SerializerInterface;
9
use Xabbuh\XApi\Model\Definition;
10
11
/**
12
 * Denormalizes PHP arrays to {@link Definition} instances.
13
 *
14
 * @author Christian Flothmann <[email protected]>
15
 */
16
final class DefinitionNormalizer implements DenormalizerInterface, SerializerAwareInterface
17
{
18
    private $serializer;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 324
    public function setSerializer(SerializerInterface $serializer)
24
    {
25 324
        $this->serializer = $serializer;
26 324
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 13
    public function denormalize($data, $class, $format = null, array $context = array())
32
    {
33 13
        if (!$this->serializer instanceof DenormalizerInterface) {
34
            throw new LogicException('Cannot denormalize because the injected serializer is not a denormalizer.');
35
        }
36
37 13
        $name = null;
38 13
        $description = null;
39 13
        $type = isset($data['type']) ? $data['type'] : null;
40 13
        $moreInfo = isset($data['moreInfo']) ? $data['moreInfo'] : null;
41
42 13
        if (isset($data['name'])) {
43 5
            $name = $this->serializer->denormalize($data['name'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
44
        }
45
46 13
        if (isset($data['description'])) {
47 3
            $description = $this->serializer->denormalize($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
48
        }
49
50 13
        return new Definition($name, $description, $type, $moreInfo);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 132
    public function supportsDenormalization($data, $type, $format = null)
57
    {
58 132
        return 'Xabbuh\XApi\Model\Definition' === $type;
59
    }
60
}
61