Passed
Push — main ( b2d943...872826 )
by Daniel
05:35
created

UserNormalizer::hasCacheableSupportsMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[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
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Serializer\Normalizer;
15
16
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
17
use Silverback\ApiComponentsBundle\Helper\User\UserDataProcessor;
18
use Silverback\ApiComponentsBundle\Mercure\MercureAuthorization;
19
use Silverback\ApiComponentsBundle\Serializer\ResourceMetadata\ResourceMetadataProvider;
20
use Symfony\Component\Security\Core\Role\RoleHierarchy;
21
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
22
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
23
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
24
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
25
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
26
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
27
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
28
29
/**
30
 * @author Daniel West <[email protected]>
31
 */
32
class UserNormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface
33
{
34
    use DenormalizerAwareTrait;
35
    use NormalizerAwareTrait;
36
37
    // public so that EntityPersistFormListener can deserialize an old user for comparison and skip this normalizer from processing changes
38
    public const ALREADY_CALLED = 'USER_NORMALIZER_ALREADY_CALLED';
39
40
    public function __construct(
41
        private readonly UserDataProcessor $userDataProcessor,
42
        private readonly RoleHierarchy $roleHierarchy,
43
        private readonly ResourceMetadataProvider $resourceMetadataProvider,
44
        private readonly MercureAuthorization $mercureAuthorization
45
    ) {
46
    }
47
48
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
49
    {
50
        return !isset($context[self::ALREADY_CALLED]) && is_subclass_of($type, AbstractUser::class);
51
    }
52
53
    public function denormalize($data, $type, $format = null, array $context = []): AbstractUser
54
    {
55
        $context[self::ALREADY_CALLED] = true;
56
57
        /** @var AbstractUser $oldObject */
58
        $oldObject = $context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? null;
59
60
        if ($oldObject) {
0 ignored issues
show
introduced by
$oldObject is of type Silverback\ApiComponents...ntity\User\AbstractUser, thus it always evaluated to true.
Loading history...
61
            $oldObject = clone $oldObject;
62
        }
63
64
        /** @var AbstractUser $object */
65
        $object = $this->denormalizer->denormalize($data, $type, $format, $context);
66
67
        $this->userDataProcessor->processChanges($object, $oldObject);
68
69
        return $object;
70
    }
71
72
    public function supportsNormalization($data, $format = null, array $context = []): bool
73
    {
74
        return !isset($context[self::ALREADY_CALLED]) && $data instanceof AbstractUser;
75
    }
76
77
    /**
78
     * @param AbstractUser $object
79
     * @param mixed|null   $format
80
     */
81
    public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
82
    {
83
        $context[self::ALREADY_CALLED] = true;
84
85
        $rolesAsEntities = $object->getRoles();
86
        $object->setRoles($this->roleHierarchy->getReachableRoleNames($rolesAsEntities));
87
88
        $subscribeTopics = $this->mercureAuthorization->getSubscribeTopics();
89
        $metadata = $this->resourceMetadataProvider->findResourceMetadata($object);
90
        $metadata->setMercureSubscribeTopics($subscribeTopics);
91
92
        return $this->normalizer->normalize($object, $format, $context);
93
    }
94
95
    public function getSupportedTypes(?string $format): array
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
    public function getSupportedTypes(/** @scrutinizer ignore-unused */ ?string $format): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
        return [AbstractUser::class => false];
98
    }
99
}
100