Completed
Pull Request — master (#10)
by Christian
02:08
created

StatementNormalizer::denormalize()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 43
Code Lines 27

Duplication

Lines 5
Ratio 11.63 %

Importance

Changes 0
Metric Value
dl 5
loc 43
rs 3
c 0
b 0
f 0
cc 9
eloc 27
nc 256
nop 4
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\Statement;
15
use Xabbuh\XApi\Model\StatementId;
16
17
/**
18
 * Normalizes and denormalizes xAPI statements.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
final class StatementNormalizer extends Normalizer
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function normalize($object, $format = null, array $context = array())
28
    {
29
        if (!$object instanceof Statement) {
30
            return null;
31
        }
32
33
        $data = array(
34
            'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
35
            'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
36
            'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
37
        );
38
39
        if (null !== $id = $object->getId()) {
40
            $data['id'] = $id->getValue();
41
        }
42
43
        if (null !== $authority = $object->getAuthority()) {
44
            $data['authority'] = $this->normalizeAttribute($authority, $format, $context);
45
        }
46
47 View Code Duplication
        if (null !== $result = $object->getResult()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
            $data['result'] = $this->normalizeAttribute($result, $format, $context);
49
        }
50
51
        if (null !== $result = $object->getCreated()) {
52
            $data['timestamp'] = $this->normalizeAttribute($result, $format, $context);
53
        }
54
55 View Code Duplication
        if (null !== $result = $object->getStored()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            $data['stored'] = $this->normalizeAttribute($result, $format, $context);
57
        }
58
59 View Code Duplication
        if (null !== $object->getContext()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            $data['context'] = $this->normalizeAttribute($object->getContext(), $format, $context);
61
        }
62
63
        if (null !== $attachments = $object->getAttachments()) {
64
            $data['attachments'] = $this->normalizeAttribute($attachments, $format, $context);
65
        }
66
67
        return $data;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function supportsNormalization($data, $format = null)
74
    {
75
        return $data instanceof Statement;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function denormalize($data, $class, $format = null, array $context = array())
82
    {
83
        $id = isset($data['id']) ? StatementId::fromString($data['id']) : null;
84
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
85
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
86 View Code Duplication
        if (class_exists('Xabbuh\XApi\Model\StatementObject')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
       $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\StatementObject', $format, $context);
88
         } else {
89
       $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
90
       }
91
        $result = null;
92
        $authority = null;
93
        $created = null;
94
        $stored = null;
95
        $statementContext = null;
96
        $attachments = null;
97
98
        if (isset($data['result'])) {
99
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
100
        }
101
102
        if (isset($data['authority'])) {
103
            $authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context);
104
        }
105
106
        if (isset($data['timestamp'])) {
107
            $created = $this->denormalizeData($data['timestamp'], 'DateTime', $format, $context);
108
        }
109
110
        if (isset($data['stored'])) {
111
            $stored = $this->denormalizeData($data['stored'], 'DateTime', $format, $context);
112
        }
113
114
        if (isset($data['context'])) {
115
            $statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context);
116
        }
117
118
        if (isset($data['attachments'])) {
119
            $attachments = $this->denormalizeData($data['attachments'], 'Xabbuh\XApi\Model\Attachment[]', $format, $context);
120
        }
121
122
        return new Statement($id, $actor, $verb, $object, $result, $authority, $created, $stored, $statementContext, $attachments);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function supportsDenormalization($data, $type, $format = null)
129
    {
130
        return 'Xabbuh\XApi\Model\Statement' === $type;
131
    }
132
}
133