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) { |
|
|
|
|
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 |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
return [AbstractUser::class => false]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|