Completed
Push — master ( 6dd36b...4e0b95 )
by Christian
02:36
created

ResultNormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 56
ccs 19
cts 19
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 22 4
A supportsNormalization() 0 4 1
A denormalize() 0 8 3
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\Result;
15
16
/**
17
 * Normalizes and denormalizes xAPI statement results.
18
 *
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
final class ResultNormalizer extends Normalizer
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 1
    public function normalize($object, $format = null, array $context = array())
27
    {
28 1
        if (!$object instanceof Result) {
29
            return null;
30
        }
31
32
        $data = array(
33 1
            'score' => $this->normalizeAttribute($object->getScore(), 'Xabbuh\XApi\Model\Score', $context),
34 1
            'success' => $object->getSuccess(),
35 1
            'completion' => $object->getCompletion(),
36
        );
37
38 1
        if (null !== $response = $object->getResponse()) {
39 1
            $data['response'] = $response;
40
        }
41
42 1
        if (null !== $duration = $object->getDuration()) {
43 1
            $data['duration'] = $duration;
44
        }
45
46 1
        return $data;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 9
    public function supportsNormalization($data, $format = null)
53
    {
54 9
        return $data instanceof Result;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function denormalize($data, $class, $format = null, array $context = array())
61
    {
62 1
        $score = $this->denormalizeData($data['score'], 'Xabbuh\XApi\Model\Score', $format, $context);
63 1
        $response = isset($data['response']) ? $data['response'] : null;
64 1
        $duration = isset($data['duration']) ? $data['duration'] : null;
65
66 1
        return new Result($score, $data['success'], $data['completion'], $response, $duration);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 10
    public function supportsDenormalization($data, $type, $format = null)
73
    {
74 10
        return 'Xabbuh\XApi\Model\Result' === $type;
75
    }
76
}
77