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

AttachmentNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 35
ccs 14
cts 16
cp 0.875
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSerializer() 0 4 1
A denormalize() 0 20 4
A supportsDenormalization() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Serializer\Normalizer;
13
14
use Symfony\Component\Serializer\Exception\LogicException;
15
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
16
use Symfony\Component\Serializer\SerializerAwareInterface;
17
use Symfony\Component\Serializer\SerializerInterface;
18
use Xabbuh\XApi\Model\Attachment;
19
use Xabbuh\XApi\Model\IRI;
20
21
/**
22
 * Denormalizes PHP arrays to {@link Attachment} objects.
23
 *
24
 * @author Christian Flothmann <[email protected]>
25
 */
26
final class AttachmentNormalizer implements DenormalizerInterface, SerializerAwareInterface
27
{
28
    private $serializer;
29
30 368
    public function setSerializer(SerializerInterface $serializer)
31
    {
32 368
        $this->serializer = $serializer;
33 368
    }
34
35 5
    public function denormalize($data, $class, $format = null, array $context = array())
36
    {
37 5
        if (!$this->serializer instanceof DenormalizerInterface) {
38
            throw new LogicException('Cannot denormalize because the injected serializer is not a denormalizer.');
39
        }
40
41 5
        $display = $this->serializer->denormalize($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
42 5
        $description = null;
43 5
        $fileUrl = null;
44
45 5
        if (isset($data['description'])) {
46
            $description = $this->serializer->denormalize($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
47
        }
48
49 5
        if (isset($data['fileUrl'])) {
50 1
            $fileUrl = $this->serializer->denormalize($data['fileUrl'], 'Xabbuh\XApi\Model\IRL', $format, $context);
51
        }
52
53 5
        return new Attachment(IRI::fromString($data['usageType']), $data['contentType'], $data['length'], $data['sha2'], $display, $description, $fileUrl);
54
    }
55
56 154
    public function supportsDenormalization($data, $type, $format = null)
57
    {
58 154
        return 'Xabbuh\XApi\Model\Attachment' === $type;
59
    }
60
}
61