Completed
Pull Request — master (#1)
by Christian
02:38 queued 25s
created

ResultNormalizer::normalize()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 8.9197
cc 4
eloc 12
nc 5
nop 3
crap 4
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