AttachmentNormalizer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 4
dl 0
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 24 4
A supportsNormalization() 0 4 1
A denormalize() 0 21 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\Symfony\Normalizer;
13
14
use Xabbuh\XApi\Model\Attachment;
15
use Xabbuh\XApi\Model\IRI;
16
use Xabbuh\XApi\Model\IRL;
17
18
/**
19
 * Denormalizes PHP arrays to {@link Attachment} objects.
20
 *
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
final class AttachmentNormalizer extends Normalizer
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function normalize($object, $format = null, array $context = array())
29
    {
30
        if (!$object instanceof Attachment) {
31
            return;
32
        }
33
34
        $data = array(
35
            'usageType' => $object->getUsageType()->getValue(),
36
            'contentType' => $object->getContentType(),
37
            'length' => $object->getLength(),
38
            'sha2' => $object->getSha2(),
39
            'display' => $this->normalizeAttribute($object->getDisplay(), $format, $context),
40
        );
41
42
        if (null !== $description = $object->getDescription()) {
43
            $data['description'] = $this->normalizeAttribute($description, $format, $context);
44
        }
45
46
        if (null !== $fileUrl = $object->getFileUrl()) {
47
            $data['fileUrl'] = $fileUrl->getValue();
48
        }
49
50
        return $data;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function supportsNormalization($data, $format = null)
57
    {
58
        return $data instanceof Attachment;
59
    }
60
61
    public function denormalize($data, $class, $format = null, array $context = array())
62
    {
63
        $display = $this->denormalizeData($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
64
        $description = null;
65
        $fileUrl = null;
66
        $content = null;
67
68
        if (isset($data['description'])) {
69
            $description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
70
        }
71
72
        if (isset($data['fileUrl'])) {
73
            $fileUrl = IRL::fromString($data['fileUrl']);
74
        }
75
76
        if (isset($context['xapi_attachments'][$data['sha2']])) {
77
            $content = $context['xapi_attachments'][$data['sha2']]['content'];
78
        }
79
80
        return new Attachment(IRI::fromString($data['usageType']), $data['contentType'], $data['length'], $data['sha2'], $display, $description, $fileUrl, $content);
81
    }
82
83
    public function supportsDenormalization($data, $type, $format = null)
84
    {
85
        return 'Xabbuh\XApi\Model\Attachment' === $type;
86
    }
87
}
88