Completed
Pull Request — master (#1)
by Christian
03:15 queued 49s
created

ActorNormalizer   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 98
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
D normalize() 0 42 9
A supportsNormalization() 0 4 1
D denormalize() 0 30 11
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\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
    public function normalize($object, $format = null, array $context = array())
27
    {
28
        if (!$object instanceof Actor) {
29
            return null;
30
        }
31
32
        $data = array();
33
34
        if (null !== $mbox = $object->getMbox()) {
35
            $data['mbox'] = $mbox;
36
        }
37
38
        if (null !== $mboxSha1Sum = $object->getMboxSha1Sum()) {
39
            $data['mbox_sha1sum'] = $mboxSha1Sum;
40
        }
41
42
        if (null !== $openId = $object->getOpenId()) {
43
            $data['openid'] = $openId;
44
        }
45
46
        if (null !== $account = $object->getAccount()) {
47
            $data['account'] = $this->normalizeAttribute($account);
48
        }
49
50
        if (null !== $name = $object->getName()) {
51
            $data['name'] = $name;
52
        }
53
54
        if ($object instanceof Group) {
55
            $data['member'] = array();
56
57
            foreach ($object->getMembers() as $member) {
58
                $data['member'][] = $this->normalize($member);
59
            }
60
61
            $data['objectType'] = 'Group';
62
        } else {
63
            $data['objectType'] = 'Agent';
64
        }
65
66
        return $data;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function supportsNormalization($data, $format = null)
73
    {
74
        return $data instanceof Actor;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function denormalize($data, $class, $format = null, array $context = array())
81
    {
82
        $mbox = isset($data['mbox']) ? $data['mbox'] : null;
83
        $mboxSha1Sum = isset($data['mboxSha1Sum']) ? $data['mboxSha1Sum'] : null;
84
        $openId = isset($data['openid']) ? $data['openid'] : null;
85
        $name = isset($data['name']) ? $data['name'] : null;
86
        $account = null;
87
88
        if (isset($data['account'])) {
89
            $account = $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account');
90
        }
91
92
        if (isset($data['name'])) {
93
            $actorData['name'] = $data['name'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$actorData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $actorData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
94
        }
95
96
        if (isset($data['objectType']) && 'Group' === $data['objectType']) {
97
            $members = array();
98
99
            if (isset($data['member'])) {
100
                foreach ($data['member'] as $member) {
101
                    $members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent');
102
                }
103
            }
104
105
            return new Group($mbox, $mboxSha1Sum, $openId, $account, $name, $members);
106
        }
107
108
        return new Agent($mbox, $mboxSha1Sum, $openId, $account, $name);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function supportsDenormalization($data, $type, $format = null)
115
    {
116
        return 'Xabbuh\XApi\Model\Actor' === $type;
117
    }
118
}
119