Completed
Push — master ( ee52bc...1e56e0 )
by Christian
03:09
created

StatementNormalizer::denormalize()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 20
cts 20
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 64
nop 4
crap 7
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 13
    public function normalize($object, $format = null, array $context = array())
28
    {
29 13
        if (!$object instanceof Statement) {
30
            return null;
31
        }
32
33
        $data = array(
34 13
            'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
35 13
            'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
36 13
            'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
37
        );
38
39 13
        if (null !== $id = $object->getId()) {
40 12
            $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 13
        if (null !== $authority = $object->getAuthority()) {
44 2
            $data['authority'] = $this->normalizeAttribute($authority, $format, $context);
45
        }
46
47 13
        if (null !== $result = $object->getResult()) {
48 1
            $data['result'] = $this->normalizeAttribute($result, $format, $context);
49
        }
50
51 13
        if (null !== $result = $object->getCreated()) {
52 1
            $data['timestamp'] = $this->normalizeAttribute($result, $format, $context);
53
        }
54
55 13
        if (null !== $result = $object->getStored()) {
56
            $data['stored'] = $this->normalizeAttribute($result, $format, $context);
57
        }
58
59 13
        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...
60 1
            $data['attachments'] = $this->normalizeAttribute($attachments, $format, $context);
61
        }
62
63 13
        return $data;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 96
    public function supportsNormalization($data, $format = null)
70
    {
71 96
        return $data instanceof Statement;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 14
    public function denormalize($data, $class, $format = null, array $context = array())
78
    {
79 14
        $id = isset($data['id']) ? StatementId::fromString($data['id']) : null;
80 14
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
81 14
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
82 14
        $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
83 14
        $result = null;
84 14
        $authority = null;
85 14
        $created = null;
86 14
        $stored = null;
87 14
        $attachments = null;
88
89 14
        if (isset($data['result'])) {
90 1
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
91
        }
92
93 14
        if (isset($data['authority'])) {
94 2
            $authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context);
95
        }
96
97 14
        if (isset($data['timestamp'])) {
98 1
            $created = $this->denormalizeData($data['timestamp'], 'DateTime', $format, $context);
99
        }
100
101 14
        if (isset($data['stored'])) {
102
            $stored = $this->denormalizeData($data['stored'], 'DateTime', $format, $context);
103
        }
104
105 14
        if (isset($data['attachments'])) {
106 1
            $attachments = $this->denormalizeData($data['attachments'], 'Xabbuh\XApi\Model\Attachment[]', $format, $context);
107
        }
108
109 14
        return new Statement($id, $actor, $verb, $object, $result, $authority, $created, $stored, null, $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...
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 97
    public function supportsDenormalization($data, $type, $format = null)
116
    {
117 97
        return 'Xabbuh\XApi\Model\Statement' === $type;
118
    }
119
}
120