Completed
Push — master ( 17be65...a4de9e )
by Christian
13s queued 10s
created

StatementNormalizer::normalize()   F

Complexity

Conditions 10
Paths 257

Size

Total Lines 46
Code Lines 24

Duplication

Lines 9
Ratio 19.57 %

Importance

Changes 0
Metric Value
dl 9
loc 46
rs 3.2727
c 0
b 0
f 0
cc 10
eloc 24
nc 257
nop 3

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