Completed
Pull Request — master (#5)
by Christian
13:09 queued 10:57
created

ActorNormalizer::denormalizeGroup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
ccs 4
cts 6
cp 0.6667
rs 9.4285
cc 3
eloc 6
nc 2
nop 5
crap 3.3332
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
use Xabbuh\XApi\Model\InverseFunctionalIdentifier;
18
19
/**
20
 * Normalizes and denormalizes xAPI statement actors.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
final class ActorNormalizer extends Normalizer
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 8
    public function normalize($object, $format = null, array $context = array())
30
    {
31 8
        if (!$object instanceof Actor) {
32
            return null;
33
        }
34
35 8
        $data = array();
36
37 8
        $this->normalizeInverseFunctionalIdentifier($object->getInverseFunctionalIdentifier(), $data);
38
39 8
        if (null !== $name = $object->getName()) {
40
            $data['name'] = $name;
41
        }
42
43 8
        if ($object instanceof Group) {
44 1
            $data['member'] = array();
45
46 1
            foreach ($object->getMembers() as $member) {
47
                $data['member'][] = $this->normalize($member);
48
            }
49
50 1
            $data['objectType'] = 'Group';
51
        } else {
52 8
            $data['objectType'] = 'Agent';
53
        }
54
55 8
        return $data;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 9
    public function supportsNormalization($data, $format = null)
62
    {
63 9
        return $data instanceof Actor;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 9
    public function denormalize($data, $class, $format = null, array $context = array())
70
    {
71 9
        $iri = $this->denormalizeInverseFunctionalIdentifier($data, $format, $context);
72 9
        $name = isset($data['name']) ? $data['name'] : null;
73
74 9
        if (isset($data['objectType']) && 'Group' === $data['objectType']) {
75 1
            return $this->denormalizeGroup($iri, $name, $data, $format, $context);
76
        }
77
78 9
        return new Agent($iri, $name);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 10
    public function supportsDenormalization($data, $type, $format = null)
85
    {
86 10
        return 'Xabbuh\XApi\Model\Actor' === $type;
87
    }
88
89 8
    private function normalizeInverseFunctionalIdentifier(InverseFunctionalIdentifier $iri = null, &$data)
90
    {
91 8
        if (null === $iri) {
92
            return;
93
        }
94
95 8
        if (null !== $mbox = $iri->getMbox()) {
96 8
            $data['mbox'] = $mbox;
97
        }
98
99 8
        if (null !== $mboxSha1Sum = $iri->getMboxSha1Sum()) {
100
            $data['mbox_sha1sum'] = $mboxSha1Sum;
101
        }
102
103 8
        if (null !== $openId = $iri->getOpenId()) {
104
            $data['openid'] = $openId;
105
        }
106
107 8
        if (null !== $account = $iri->getAccount()) {
108 1
            $data['account'] = $this->normalizeAttribute($account);
109
        }
110 8
    }
111
112 9
    private function denormalizeInverseFunctionalIdentifier($data, $format = null, array $context = array())
113
    {
114 9
        if (isset($data['mbox'])) {
115 9
            return InverseFunctionalIdentifier::withMbox($data['mbox']);
116
        }
117
118 1
        if (isset($data['mboxSha1Sum'])) {
119
            return InverseFunctionalIdentifier::withMboxSha1Sum($data['mboxSha1Sum']);
120
        }
121
122 1
        if (isset($data['openid'])) {
123
            return InverseFunctionalIdentifier::withOpenId($data['openid']);
124
        }
125
126 1
        if (isset($data['account'])) {
127 1
            return InverseFunctionalIdentifier::withAccount($this->denormalizeAccount($data, $format, $context));
128
        }
129
    }
130
131 1
    private function denormalizeAccount($data, $format = null, array $context = array())
132
    {
133 1
        if (!isset($data['account'])) {
134
            return null;
135
        }
136
137 1
        return $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account', $format, $context);
138
    }
139
140 1
    private function denormalizeGroup(InverseFunctionalIdentifier $iri = null, $name, $data, $format = null, array $context = array())
141
    {
142 1
        $members = array();
143
144 1
        if (isset($data['member'])) {
145
            foreach ($data['member'] as $member) {
146
                $members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent', $format, $context);
147
            }
148
        }
149
150 1
        return new Group($iri, $name, $members);
151
    }
152
}
153