Completed
Pull Request — master (#1)
by Christian
02:10
created

ResultNormalizer::normalize()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
rs 8.9197
cc 4
eloc 12
nc 5
nop 3
crap 4.0058
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
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class ResultNormalizer extends Normalizer
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 1
    public function normalize($object, $format = null, array $context = array())
25
    {
26 1
        if (!$object instanceof Result) {
27
            return null;
28
        }
29
30
        $data = array(
31 1
            'score' => $this->normalizeAttribute($object->getScore(), 'Xabbuh\XApi\Model\Score', $context),
32 1
            'success' => $object->getSuccess(),
33 1
            'completion' => $object->getCompletion(),
34 1
        );
35
36 1
        if (null !== $response = $object->getResponse()) {
37 1
            $data['response'] = $response;
38 1
        }
39
40 1
        if (null !== $duration = $object->getDuration()) {
41 1
            $data['duration'] = $duration;
42 1
        }
43
44 1
        return $data;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 9
    public function supportsNormalization($data, $format = null)
51
    {
52 9
        return $data instanceof Result;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function denormalize($data, $class, $format = null, array $context = array())
59
    {
60 1
        $score = $this->denormalizeData($data['score'], 'Xabbuh\XApi\Model\Score', $format, $context);
61 1
        $response = isset($data['response']) ? $data['response'] : null;
62 1
        $duration = isset($data['duration']) ? $data['duration'] : null;
63
64 1
        return new Result($score, $data['success'], $data['completion'], $response, $duration);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 10
    public function supportsDenormalization($data, $type, $format = null)
71
    {
72 10
        return 'Xabbuh\XApi\Model\Result' === $type;
73
    }
74
}
75