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

AttachmentNormalizer::setSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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