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

UserNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supportsNormalization() 0 7 3
A normalize() 0 27 1
A getSupportedTypes() 0 4 1
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