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

ActorNormalizer::denormalize()   D

Complexity

Conditions 10
Paths 96

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 18
cts 18
cp 1
rs 4.8196
cc 10
eloc 15
nc 96
nop 4
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Actor;
15
use Xabbuh\XApi\Model\Agent;
16
use Xabbuh\XApi\Model\Group;
17
18
/**
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
final class ActorNormalizer extends Normalizer
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 10
    public function normalize($object, $format = null, array $context = array())
27
    {
28 10
        if (!$object instanceof Actor) {
29
            return null;
30
        }
31
32 10
        $data = array();
33
34 10
        if (null !== $mbox = $object->getMbox()) {
35 10
            $data['mbox'] = $mbox;
36 10
        }
37
38 10
        if (null !== $mboxSha1Sum = $object->getMboxSha1Sum()) {
39
            $data['mbox_sha1sum'] = $mboxSha1Sum;
40
        }
41
42 10
        if (null !== $openId = $object->getOpenId()) {
43 1
            $data['openid'] = $openId;
44 1
        }
45
46 10
        if (null !== $account = $object->getAccount()) {
47 3
            $data['account'] = $this->normalizeAttribute($account);
48 3
        }
49
50 10
        if (null !== $name = $object->getName()) {
51 2
            $data['name'] = $name;
52 2
        }
53
54 10
        if ($object instanceof Group) {
55 2
            $data['member'] = array();
56
57 2
            foreach ($object->getMembers() as $member) {
58 2
                $data['member'][] = $this->normalize($member);
59 2
            }
60
61 2
            $data['objectType'] = 'Group';
62 2
        } else {
63 10
            $data['objectType'] = 'Agent';
64
        }
65
66 10
        return $data;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 11
    public function supportsNormalization($data, $format = null)
73
    {
74 11
        return $data instanceof Actor;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 12
    public function denormalize($data, $class, $format = null, array $context = array())
81
    {
82 12
        $mbox = isset($data['mbox']) ? $data['mbox'] : null;
83 12
        $mboxSha1Sum = isset($data['mboxSha1Sum']) ? $data['mboxSha1Sum'] : null;
84 12
        $openId = isset($data['openid']) ? $data['openid'] : null;
85 12
        $name = isset($data['name']) ? $data['name'] : null;
86 12
        $account = null;
87
88 12
        if (isset($data['account'])) {
89 3
            $account = $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account');
90 3
        }
91
92 12
        if (isset($data['objectType']) && 'Group' === $data['objectType']) {
93 2
            $members = array();
94
95 2
            if (isset($data['member'])) {
96 2
                foreach ($data['member'] as $member) {
97 2
                    $members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent');
98 2
                }
99 2
            }
100
101 2
            return new Group($mbox, $mboxSha1Sum, $openId, $account, $name, $members);
102
        }
103
104 12
        return new Agent($mbox, $mboxSha1Sum, $openId, $account, $name);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 13
    public function supportsDenormalization($data, $type, $format = null)
111
    {
112 13
        return 'Xabbuh\XApi\Model\Actor' === $type;
113
    }
114
}
115