Completed
Pull Request — master (#5)
by Christian
02:59
created

ActorNormalizer::denormalizeAccount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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