Completed
Pull Request — master (#12)
by Christian
03:53
created

ObjectNormalizer::normalize()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 17
cts 17
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 8
nop 3
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\Activity;
15
use Xabbuh\XApi\Model\Object;
16
use Xabbuh\XApi\Model\Statement;
17
use Xabbuh\XApi\Model\StatementReference;
18
use Xabbuh\XApi\Model\SubStatement;
19
20
/**
21
 * Normalizes and denormalizes xAPI statement objects.
22
 *
23
 * @author Christian Flothmann <[email protected]>
24
 */
25
final class ObjectNormalizer extends Normalizer
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 36
    public function normalize($object, $format = null, array $context = array())
31
    {
32 36
        if ($object instanceof Activity) {
33
            $activityData = array(
34 23
                'objectType' => 'Activity',
35 23
                'id' => $object->getId(),
36
            );
37
38 23
            if (null !== $definition = $object->getDefinition()) {
39 4
                $activityData['definition'] = $this->normalizeAttribute($definition, $format, $context);
40
            }
41
42 23
            return $activityData;
43
        }
44
45 25
        if ($object instanceof StatementReference) {
46
            return array(
47 4
                'objectType' => 'StatementRef',
48 4
                'id' => $object->getStatementId(),
49
            );
50
        }
51
52 22
        if ($object instanceof SubStatement) {
53
            $data = array(
54 22
                'objectType' => 'SubStatement',
55 22
                'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
56 22
                'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
57 22
                'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
58
            );
59
60 22
            if (null !== $result = $object->getResult()) {
61
                $data['result'] = $this->normalizeAttribute($result, $format, $context);
62
            }
63
64 22
            if (null !== $statementContext = $object->getContext()) {
0 ignored issues
show
Bug introduced by
The method getContext() does not seem to exist on object<Xabbuh\XApi\Model\SubStatement>.

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...
65
                $data['context'] = $this->normalizeAttribute($statementContext, $format, $context);
66
            }
67
68
            return $data;
69
        }
70
71
        return null;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 78
    public function supportsNormalization($data, $format = null)
78
    {
79 78
        return $data instanceof Object;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 37
    public function denormalize($data, $class, $format = null, array $context = array())
86
    {
87 37
        if (!isset($data['objectType']) || 'Activity' === $data['objectType']) {
88 24
            return $this->denormalizeActivity($data, $format, $context);
89
        }
90
91 25
        if (isset($data['objectType']) && ('Agent' === $data['objectType'] || 'Group' === $data['objectType'])) {
92 9
            return $this->denormalizeData($data, 'Xabbuh\XApi\Model\Actor', $format, $context);
93
        }
94
95 25
        if (isset($data['objectType']) && 'SubStatement' === $data['objectType']) {
96 22
            return $this->denormalizeSubStatement($data, $format, $context);
97
        }
98
99 4
        if (isset($data['objectType']) && 'StatementRef' === $data['objectType']) {
100 4
            return new StatementReference($data['id']);
101
        }
102
103
        return null;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 79
    public function supportsDenormalization($data, $type, $format = null)
110
    {
111 79
        return 'Xabbuh\XApi\Model\Activity' === $type || 'Xabbuh\XApi\Model\Object' === $type || 'Xabbuh\XApi\Model\StatementReference' === $type || 'Xabbuh\XApi\Model\SubStatement' === $type;
112
    }
113
114 24
    private function denormalizeActivity(array  $data, $format = null, array $context = array())
115
    {
116 24
        $definition = null;
117
118 24
        if (isset($data['definition'])) {
119 4
            $definition = $this->denormalizeData($data['definition'], 'Xabbuh\XApi\Model\Definition', $format, $context);
120
        }
121
122 24
        return new Activity($data['id'], $definition);
123
    }
124
125 22
    private function denormalizeSubStatement(array  $data, $format = null, array $context = array())
126
    {
127 22
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
128 22
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
129 22
        $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
130 22
        $result = null;
131 22
        $statementContext = null;
132
133 22
        if (isset($data['result'])) {
134
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
135
        }
136
137 22
        if (isset($data['context'])) {
138
            $statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context);
139
        }
140
141 22
        return new SubStatement(null, $actor, $verb, $object, $result, $statementContext);
0 ignored issues
show
Unused Code introduced by
The call to SubStatement::__construct() has too many arguments starting with $statementContext.

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...
142
    }
143
}
144