Passed
Pull Request — master (#7302)
by Angel Fernando Quiroz
18:19 queued 07:15
created

UserDenormalizer::getPrimaryValue()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 8
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 17
rs 8.8333
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Serializer\Denormalizer\Scim;
8
9
use Chamilo\CoreBundle\Entity\User;
10
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
11
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
12
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
13
14
class UserDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface
15
{
16
    use DenormalizerAwareTrait;
17
18
    public const FORMAT = 'scim';
19
    private const ALREADY_CALLED = 'SCIM_USER_DENORMALIZER_ALREADY_CALLED';
20
21
    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = [])
22
    {
23
        $context[self::ALREADY_CALLED] = true;
24
25
        $user = $context['object_to_populate'] ?? new User();
26
27
        if (isset($data['userName'])) {
28
            $user->setUsername($data['userName']);
29
        }
30
31
        if (isset($data['active'])) {
32
            $user->setActive((int) $data['active']);
33
        }
34
35
        if (isset($data['name'])) {
36
            $name = $data['name'];
37
38
            if (isset($name['givenName'])) {
39
                $user->setFirstName($name['givenName']);
40
            }
41
42
            if (isset($name['familyName'])) {
43
                $user->setLastName($name['familyName']);
44
            }
45
        }
46
47
        if ($email = self::getPrimaryValue($data, 'emails')) {
48
            $user->setEmail($email);
49
        }
50
51
        if ($phone = self::getPrimaryValue($data, 'phoneNumbers')) {
52
            $user->setPhone($phone);
53
        }
54
55
        if ($address = self::getPrimaryValue($data, 'addresses', 'formatted')) {
56
            $user->setAddress($address);
57
        }
58
59
        if (isset($data['locale'])) {
60
            $user->setLocale(substr($data['locale'], 0, 10));
61
        }
62
63
        if (isset($data['timezone'])) {
64
            $user->setTimezone($data['timezone']);
65
        }
66
67
        return $user;
68
    }
69
70
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
71
    {
72
        if (isset($context[self::ALREADY_CALLED])) {
73
            return false;
74
        }
75
76
        return User::class === $type
77
            && \is_array($data)
78
            && self::FORMAT === $format;
79
    }
80
81
    public function getSupportedTypes(?string $format): array
82
    {
83
        return [
84
            User::class => false,
85
        ];
86
    }
87
88
    public static function getPrimaryValue(array $values, string $propertyName, string $subPropertyName = 'value'): mixed
89
    {
90
        if (!isset($values[$propertyName]) || !is_array($values[$propertyName])) {
91
            return null;
92
        }
93
94
        foreach ($values[$propertyName] as $value) {
95
            if (!empty($value['primary']) && true === $value['primary']) {
96
                return $value[$subPropertyName];
97
            }
98
        }
99
100
        if (!empty($values[$propertyName][0][$subPropertyName])) {
101
            return $values[$propertyName][0][$subPropertyName];
102
        }
103
104
        return null;
105
    }
106
}
107