Completed
Push — master ( ee52bc...1e56e0 )
by Christian
03:09
created

AttachmentNormalizer::supportsDenormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
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
20
/**
21
 * Denormalizes PHP arrays to {@link Attachment} objects.
22
 *
23
 * @author Christian Flothmann <[email protected]>
24
 */
25
final class AttachmentNormalizer implements DenormalizerInterface, SerializerAwareInterface
26
{
27
    private $serializer;
28
29 368
    public function setSerializer(SerializerInterface $serializer)
30
    {
31 368
        $this->serializer = $serializer;
32 368
    }
33
34 4
    public function denormalize($data, $class, $format = null, array $context = array())
35
    {
36 4
        if (!$this->serializer instanceof DenormalizerInterface) {
37
            throw new LogicException('Cannot denormalize because the injected serializer is not a denormalizer.');
38
        }
39
40 4
        $display = $this->serializer->denormalize($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
41 4
        $description = null;
42 4
        $fileUrl = isset($data['fileUrl']) ? $data['fileUrl'] : null;
43
44 4
        if (isset($data['description'])) {
45
            $description = $this->serializer->denormalize($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
46
        }
47
48 4
        return new Attachment($data['usageType'], $data['contentType'], $data['length'], $data['sha2'], $display, $description, $fileUrl);
49
    }
50
51 164
    public function supportsDenormalization($data, $type, $format = null)
52
    {
53 164
        return 'Xabbuh\XApi\Model\Attachment' === $type;
54
    }
55
}
56