Completed
Pull Request — master (#7)
by
unknown
04:32
created

StatementNormalizer::denormalize()   F

Complexity

Conditions 10
Paths 257

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 3.7894
c 0
b 0
f 0
cc 10
eloc 29
nc 257
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Common\Exception\UnsupportedStatementVersionException;
15
use Xabbuh\XApi\Model\Statement;
16
use Xabbuh\XApi\Model\StatementId;
17
18
/**
19
 * Normalizes and denormalizes xAPI statements.
20
 *
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
final class StatementNormalizer extends Normalizer
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function normalize($object, $format = null, array $context = array())
29
    {
30
        if (!$object instanceof Statement) {
31
            return null;
32
        }
33
34
        $data = array(
35
            'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
36
            'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
37
            'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
38
            'version' => $object->getVersion(),
39
        );
40
41
        if (null !== $id = $object->getId()) {
42
            $data['id'] = $id->getValue();
43
        }
44
45
        if (null !== $authority = $object->getAuthority()) {
46
            $data['authority'] = $this->normalizeAttribute($authority, $format, $context);
47
        }
48
49 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...
50
            $data['result'] = $this->normalizeAttribute($result, $format, $context);
51
        }
52
53
        if (null !== $result = $object->getCreated()) {
54
            $data['timestamp'] = $this->normalizeAttribute($result, $format, $context);
55
        }
56
57 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...
58
            $data['stored'] = $this->normalizeAttribute($result, $format, $context);
59
        }
60
61 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...
62
            $data['context'] = $this->normalizeAttribute($object->getContext(), $format, $context);
63
        }
64
65
        if (null !== $attachments = $object->getAttachments()) {
66
            $data['attachments'] = $this->normalizeAttribute($attachments, $format, $context);
67
        }
68
69
        return $data;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function supportsNormalization($data, $format = null)
76
    {
77
        return $data instanceof Statement;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function denormalize($data, $class, $format = null, array $context = array())
84
    {
85
        $version = null;
86
87
        if (isset($data['version'])) {
88
            $version = $data['version'];
89
90
            if (!preg_match('/^1\.0(?:\.\d+)?$/', $version)) {
91
                throw new UnsupportedStatementVersionException(sprintf('Statements at version "%s" are not supported.', $version));
92
            }
93
        }
94
95
        $id = isset($data['id']) ? StatementId::fromString($data['id']) : null;
96
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
97
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
98
        $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
99
        $result = null;
100
        $authority = null;
101
        $created = null;
102
        $stored = null;
103
        $statementContext = null;
104
        $attachments = null;
105
106
        if (isset($data['result'])) {
107
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
108
        }
109
110
        if (isset($data['authority'])) {
111
            $authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context);
112
        }
113
114
        if (isset($data['timestamp'])) {
115
            $created = $this->denormalizeData($data['timestamp'], 'DateTime', $format, $context);
116
        }
117
118
        if (isset($data['stored'])) {
119
            $stored = $this->denormalizeData($data['stored'], 'DateTime', $format, $context);
120
        }
121
122
        if (isset($data['context'])) {
123
            $statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context);
124
        }
125
126
        if (isset($data['attachments'])) {
127
            $attachments = $this->denormalizeData($data['attachments'], 'Xabbuh\XApi\Model\Attachment[]', $format, $context);
128
        }
129
130
        return new Statement($id, $actor, $verb, $object, $result, $authority, $created, $stored, $statementContext, $attachments, $version);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function supportsDenormalization($data, $type, $format = null)
137
    {
138
        return 'Xabbuh\XApi\Model\Statement' === $type;
139
    }
140
}
141