Completed
Pull Request — master (#21)
by Christian
03:19
created

VerbNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 39
ccs 12
cts 13
cp 0.9231
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSerializer() 0 4 1
A denormalize() 0 15 3
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\Verb;
10
11
/**
12
 * Denormalizes PHP arrays to {@link Verb} objects.
13
 *
14
 * @author Christian Flothmann <[email protected]>
15
 */
16
final class VerbNormalizer implements DenormalizerInterface, SerializerAwareInterface
17
{
18
    private $serializer;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 368
    public function setSerializer(SerializerInterface $serializer)
24
    {
25 368
        $this->serializer = $serializer;
26 368
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 42
    public function denormalize($data, $class, $format = null, array $context = array())
32
    {
33 42
        if (!$this->serializer instanceof DenormalizerInterface) {
34
            throw new LogicException('Cannot denormalize because the injected serializer is not a denormalizer.');
35
        }
36
37 42
        $id = $this->serializer->denormalize($data['id'], 'Xabbuh\XApi\Model\IRI', $format, $context);
38 42
        $display = null;
39
40 42
        if (isset($data['display'])) {
41 40
            $display = $this->serializer->denormalize($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
42
        }
43
44 42
        return new Verb($id, $display);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 87
    public function supportsDenormalization($data, $type, $format = null)
51
    {
52 87
        return 'Xabbuh\XApi\Model\Verb' === $type;
53
    }
54
}
55