Completed
Push — master ( a4de9e...da24ae )
by Christian
14s queued 11s
created

StatementNormalizer::denormalize()   D

Complexity

Conditions 11
Paths 513

Size

Total Lines 55
Code Lines 32

Duplication

Lines 5
Ratio 9.09 %

Importance

Changes 0
Metric Value
dl 5
loc 55
rs 4.4999
c 0
b 0
f 0
cc 11
eloc 32
nc 513
nop 4

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