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

ObjectNormalizer::denormalizeSubStatement()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 5
Ratio 20.83 %

Importance

Changes 0
Metric Value
dl 5
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 14
nc 8
nop 3
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\Model\Activity;
15
use Xabbuh\XApi\Model\IRI;
16
use Xabbuh\XApi\Model\Object as LegacyStatementObject;
17
use Xabbuh\XApi\Model\StatementId;
18
use Xabbuh\XApi\Model\StatementObject;
19
use Xabbuh\XApi\Model\StatementReference;
20
use Xabbuh\XApi\Model\SubStatement;
21
22
/**
23
 * Normalizes and denormalizes xAPI statement objects.
24
 *
25
 * @author Christian Flothmann <[email protected]>
26
 */
27
final class ObjectNormalizer extends Normalizer
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function normalize($object, $format = null, array $context = array())
33
    {
34
        if ($object instanceof Activity) {
35
            $activityData = array(
36
                'objectType' => 'Activity',
37
                'id' => $object->getId()->getValue(),
38
            );
39
40
            if (null !== $definition = $object->getDefinition()) {
41
                $activityData['definition'] = $this->normalizeAttribute($definition, $format, $context);
42
            }
43
44
            return $activityData;
45
        }
46
47
        if ($object instanceof StatementReference) {
48
            return array(
49
                'objectType' => 'StatementRef',
50
                'id' => $object->getStatementId()->getValue(),
51
            );
52
        }
53
54
        if ($object instanceof SubStatement) {
55
            $data = array(
56
                'objectType' => 'SubStatement',
57
                'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
58
                'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
59
                'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
60
            );
61
62 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...
63
                $data['result'] = $this->normalizeAttribute($result, $format, $context);
64
            }
65
66 View Code Duplication
            if (null !== $statementContext = $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...
67
                $data['context'] = $this->normalizeAttribute($statementContext, $format, $context);
68
            }
69
70
            return $data;
71
        }
72
73
        return null;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function supportsNormalization($data, $format = null)
80
    {
81
        return $data instanceof LegacyStatementObject || $data instanceof StatementObject;
0 ignored issues
show
Bug introduced by
The class Xabbuh\XApi\Model\Object does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function denormalize($data, $class, $format = null, array $context = array())
88
    {
89
        if (!isset($data['objectType']) || 'Activity' === $data['objectType']) {
90
            return $this->denormalizeActivity($data, $format, $context);
91
        }
92
93
        if (isset($data['objectType']) && ('Agent' === $data['objectType'] || 'Group' === $data['objectType'])) {
94
            return $this->denormalizeData($data, 'Xabbuh\XApi\Model\Actor', $format, $context);
95
        }
96
97
        if (isset($data['objectType']) && 'SubStatement' === $data['objectType']) {
98
            return $this->denormalizeSubStatement($data, $format, $context);
99
        }
100
101
        if (isset($data['objectType']) && 'StatementRef' === $data['objectType']) {
102
            return new StatementReference(StatementId::fromString($data['id']));
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function supportsDenormalization($data, $type, $format = null)
112
    {
113
        return in_array($type, array(Activity::class, LegacyStatementObject::class, StatementObject::class, StatementReference::class, SubStatement::class), true);
114
    }
115
116
    private function denormalizeActivity(array $data, $format = null, array $context = array())
117
    {
118
        $definition = null;
119
120
        if (isset($data['definition'])) {
121
            $definition = $this->denormalizeData($data['definition'], 'Xabbuh\XApi\Model\Definition', $format, $context);
122
        }
123
124
        return new Activity(IRI::fromString($data['id']), $definition);
125
    }
126
127
    private function denormalizeSubStatement(array  $data, $format = null, array $context = array())
128
    {
129
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
130
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
131
132 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...
133
            $object = $this->denormalizeData($data['object'], StatementObject::class, $format, $context);
134
        } else {
135
            $object = $this->denormalizeData($data['object'], LegacyStatementObject::class, $format, $context);
136
        }
137
138
        $result = null;
139
        $statementContext = null;
140
141
        if (isset($data['result'])) {
142
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
143
        }
144
145
        if (isset($data['context'])) {
146
            $statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context);
147
        }
148
149
        return new SubStatement($actor, $verb, $object, $result, $statementContext);
150
    }
151
}
152