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

UserNormalizer::normalize()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 60
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 35
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 60
rs 9.36

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Chamilo\CoreBundle\Helpers\ScimHelper;
12
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
14
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
15
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16
17
class UserNormalizer implements NormalizerInterface, NormalizerAwareInterface
18
{
19
    use NormalizerAwareTrait;
20
21
    public const FORMAT = 'scim';
22
    private const ALREADY_CALLED = 'SCIM_USER_NORMALIZER_ALREADY_CALLED';
23
24
    public function __construct(
25
        private readonly NameConventionHelper $nameConventionHelper,
26
        private readonly UrlGeneratorInterface $router,
27
        private readonly ScimHelper $scimHelper,
28
    ) {}
29
30
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
31
    {
32
        /** @var User $user */
33
        $user = $object;
34
35
        $uuid = $user->getUuid();
36
37
        $userInfo = [
38
            'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],
39
            'id' => $uuid,
40
            'userName' => $user->getUsername(),
41
            'name' => [
42
                'formatted' => $this->nameConventionHelper->getPersonName($user),
43
                'givenName' => $user->getFirstname(),
44
                'familyName' => $user->getLastName(),
45
            ],
46
            'emails' => [
47
                [
48
                    'value' => $user->getEmail(),
49
                    'type' => 'work',
50
                    'primary' => true,
51
                ],
52
            ],
53
            'active' => $user->isActive(),
54
            'timezone' => $user->getTimezone(),
55
            'meta' => [
56
                'resourceType' => 'User',
57
                'created' => $user->getCreatedAt()?->format('c'),
58
                'lastModified' => $user->getUpdatedAt()?->format('c'),
59
                'location' => $this->router->generate(
60
                    'scim_user',
61
                    ['uuid' => $uuid],
62
                    UrlGeneratorInterface::ABSOLUTE_URL
63
                ),
64
            ],
65
        ];
66
67
        if ($externalId = $this->scimHelper->getExternalId($user)) {
68
            $userInfo['externalId'] = $externalId;
69
        }
70
71
        if ($phone = $user->getPhone()) {
72
            $userInfo['phoneNumbers'] = [
73
                [
74
                    'type' => 'work',
75
                    'value' => $phone,
76
                ],
77
            ];
78
        }
79
80
        if ($address = $user->getAddress()) {
81
            $userInfo['addresses'] = [
82
                [
83
                    'type' => 'work',
84
                    'formatted' => $address,
85
                ]
86
            ];
87
        }
88
89
        return $userInfo;
90
    }
91
92
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
93
    {
94
        if (isset($context[self::ALREADY_CALLED])) {
95
            return false;
96
        }
97
98
        return $data instanceof User && self::FORMAT === $format;
99
    }
100
101
    public function getSupportedTypes(?string $format): array
102
    {
103
        return [
104
            User::class => false,
105
        ];
106
    }
107
}
108