Completed
Push — master ( 1e56e0...8865c9 )
by Christian
03:19 queued 14s
created

StatementNormalizer   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 2
dl 0
loc 107
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
D normalize() 0 42 9
A supportsNormalization() 0 4 1
D denormalize() 0 39 8
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\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 14
    public function normalize($object, $format = null, array $context = array())
28
    {
29 14
        if (!$object instanceof Statement) {
30
            return null;
31
        }
32
33
        $data = array(
34 14
            'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
35 14
            'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
36 14
            'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
37
        );
38
39 14
        if (null !== $id = $object->getId()) {
40 13
            $data['id'] = $id->getValue();
0 ignored issues
show
Bug introduced by
The method getValue cannot be called on $id (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
41
        }
42
43 14
        if (null !== $authority = $object->getAuthority()) {
44 3
            $data['authority'] = $this->normalizeAttribute($authority, $format, $context);
45
        }
46
47 14
        if (null !== $result = $object->getResult()) {
48 2
            $data['result'] = $this->normalizeAttribute($result, $format, $context);
49
        }
50
51 14
        if (null !== $result = $object->getCreated()) {
52 2
            $data['timestamp'] = $this->normalizeAttribute($result, $format, $context);
53
        }
54
55 14
        if (null !== $result = $object->getStored()) {
56 1
            $data['stored'] = $this->normalizeAttribute($result, $format, $context);
57
        }
58
59 14
        if (null !== $object->getContext()) {
60 1
            $data['context'] = $this->normalizeAttribute($object->getContext(), $format, $context);
61
        }
62
63 14
        if (null !== $attachments = $object->getAttachments()) {
0 ignored issues
show
Bug introduced by
The method getAttachments() does not seem to exist on object<Xabbuh\XApi\Model\Statement>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64 2
            $data['attachments'] = $this->normalizeAttribute($attachments, $format, $context);
65
        }
66
67 14
        return $data;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 97
    public function supportsNormalization($data, $format = null)
74
    {
75 97
        return $data instanceof Statement;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 15
    public function denormalize($data, $class, $format = null, array $context = array())
82
    {
83 15
        $id = isset($data['id']) ? StatementId::fromString($data['id']) : null;
84 15
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
85 15
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
86 15
        $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
87 15
        $result = null;
88 15
        $authority = null;
89 15
        $created = null;
90 15
        $stored = null;
91 15
        $statementContext = null;
92 15
        $attachments = null;
93
94 15
        if (isset($data['result'])) {
95 2
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
96
        }
97
98 15
        if (isset($data['authority'])) {
99 3
            $authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context);
100
        }
101
102 15
        if (isset($data['timestamp'])) {
103 2
            $created = $this->denormalizeData($data['timestamp'], 'DateTime', $format, $context);
104
        }
105
106 15
        if (isset($data['stored'])) {
107 1
            $stored = $this->denormalizeData($data['stored'], 'DateTime', $format, $context);
108
        }
109
110 15
        if (isset($data['context'])) {
111 1
            $statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context);
112
        }
113
114 15
        if (isset($data['attachments'])) {
115 2
            $attachments = $this->denormalizeData($data['attachments'], 'Xabbuh\XApi\Model\Attachment[]', $format, $context);
116
        }
117
118 15
        return new Statement($id, $actor, $verb, $object, $result, $authority, $created, $stored, $statementContext, $attachments);
0 ignored issues
show
Unused Code introduced by
The call to Statement::__construct() has too many arguments starting with $attachments.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 98
    public function supportsDenormalization($data, $type, $format = null)
125
    {
126 98
        return 'Xabbuh\XApi\Model\Statement' === $type;
127
    }
128
}
129