Completed
Pull Request — master (#5)
by Christian
10:47 queued 05:51
created

ActorNormalizer::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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