Passed
Pull Request — master (#7215)
by Angel Fernando Quiroz
19:05 queued 07:41
created

UserNormalizer::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 27
rs 9.6666
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Serializer\Normalizer\Scim;
8
9
use Chamilo\CoreBundle\Entity\User;
10
use Chamilo\CoreBundle\Helpers\NameConventionHelper;
11
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
12
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
13
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
14
15
class UserNormalizer implements NormalizerInterface, NormalizerAwareInterface
16
{
17
    use NormalizerAwareTrait;
18
19
    public const FORMAT = 'scim';
20
    private const ALREADY_CALLED = 'SCIM_USER_NORMALIZER_ALREADY_CALLED';
21
22
    public function __construct(
23
        private readonly NameConventionHelper $nameConventionHelper,
24
    ) {}
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
30
    {
31
        /** @var User $user */
32
        $user = $object;
33
34
        return [
35
            'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],
36
            'id' => $user->getResourceNode()->getUuid(),
37
            //'externalId' => $user->getAzureId(),
38
            'userName' => $user->getUsername(),
39
            'name' => [
40
                'formatted' => $this->nameConventionHelper->getPersonName($user),
41
                'givenName' => $user->getFirstname(),
42
                'familyName' => $user->getLastName(),
43
            ],
44
            'emails' => [
45
                [
46
                    'value' => $user->getEmail(),
47
                    'type' => 'work',
48
                    'primary' => true,
49
                ],
50
            ],
51
            'active' => $user->isEnabled(),
52
            'meta' => [
53
                'resourceType' => 'User',
54
                'created' => $user->getCreatedAt()?->format('c'),
55
                'lastModified' => $user->getUpdatedAt()?->format('c'),
56
            ],
57
        ];
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
64
    {
65
        if (isset($context[self::ALREADY_CALLED])) {
66
            return false;
67
        }
68
69
        return $data instanceof User && self::FORMAT === $format;
70
    }
71
72
    public function getSupportedTypes(?string $format): array
73
    {
74
        return [
75
            User::class => false,
76
        ];
77
    }
78
}
79