Completed
Pull Request — master (#3)
by Christian
02:50
created

ActorNormalizer::denormalizeGroup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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