vendor/ibexa/fieldtype-page/src/lib/Event/Subscriber/UserAttributeSerializationSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\FieldTypePage\Event\Subscriber;
  8. use Ibexa\AdminUi\Form\Type\UserChoiceType;
  9. use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
  10. use Ibexa\Contracts\Core\Repository\UserService;
  11. use Ibexa\Contracts\Core\Repository\Values\User\User;
  12. use Ibexa\FieldTypePage\Event\AttributeSerializationEvent;
  13. use Ibexa\FieldTypePage\Event\PageEvents;
  14. use Psr\Log\LoggerAwareInterface;
  15. use Psr\Log\LoggerAwareTrait;
  16. use Psr\Log\LoggerInterface;
  17. use Psr\Log\NullLogger;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. final class UserAttributeSerializationSubscriber implements EventSubscriberInterface, LoggerAwareInterface
  20. {
  21. use LoggerAwareTrait;
  22. private UserService $userService;
  23. public function __construct(
  24. UserService $userService,
  25. ?LoggerInterface $logger = null
  26. ) {
  27. $this->userService = $userService;
  28. $this->logger = $logger ?? new NullLogger();
  29. }
  30. public static function getSubscribedEvents(): array
  31. {
  32. return [
  33. PageEvents::ATTRIBUTE_SERIALIZATION => ['onAttributeSerialization', 10],
  34. PageEvents::ATTRIBUTE_DESERIALIZATION => ['onAttributeDeserialization', 10],
  35. ];
  36. }
  37. public function onAttributeSerialization(AttributeSerializationEvent $event): void
  38. {
  39. $attributeDefinition = $event->getAttributeDefinition();
  40. if ($attributeDefinition === null || $attributeDefinition->getType() !== UserChoiceType::class) {
  41. return;
  42. }
  43. $deserializedValue = $event->getDeserializedValue();
  44. if ($deserializedValue === null) {
  45. return;
  46. }
  47. if (!is_array($deserializedValue)) {
  48. $deserializedValue = [$deserializedValue];
  49. }
  50. $event->setSerializedValue(
  51. implode(
  52. ',',
  53. array_map(static fn (User $value): int => $value->getUserId(), $deserializedValue),
  54. ),
  55. );
  56. $event->stopPropagation();
  57. }
  58. public function onAttributeDeserialization(AttributeSerializationEvent $event): void
  59. {
  60. $serializedValue = $event->getSerializedValue();
  61. if (!is_string($serializedValue)) {
  62. return;
  63. }
  64. $attributeDefinition = $event->getAttributeDefinition();
  65. if ($attributeDefinition === null || $attributeDefinition->getType() !== UserChoiceType::class) {
  66. return;
  67. }
  68. $isMultiple = $attributeDefinition->getOptions()['multiple'] ?? false;
  69. if ($isMultiple) {
  70. $this->handleMultipleUsers($serializedValue, $event);
  71. return;
  72. }
  73. $this->handleSingleUser($serializedValue, $event);
  74. }
  75. private function handleSingleUser(string $serializedValue, AttributeSerializationEvent $event): void
  76. {
  77. try {
  78. $user = $this->userService->loadUser((int)$serializedValue);
  79. $event->setDeserializedValue($user);
  80. } catch (NotFoundException $e) {
  81. $this->logger->info(sprintf(
  82. 'User with ID %s was not found. Serialized value is discarded.',
  83. $serializedValue,
  84. ));
  85. }
  86. $event->stopPropagation();
  87. }
  88. private function handleMultipleUsers(string $serializedValue, AttributeSerializationEvent $event): void
  89. {
  90. $serializedValues = explode(',', $serializedValue);
  91. $users = [];
  92. foreach ($serializedValues as $serializedValue) {
  93. try {
  94. $users[] = $this->userService->loadUser((int)$serializedValue);
  95. } catch (NotFoundException $e) {
  96. $this->logger->info(sprintf(
  97. 'User with ID %s was not found. Serialized value is discarded.',
  98. $serializedValue,
  99. ));
  100. }
  101. }
  102. $event->setDeserializedValue($users);
  103. $event->stopPropagation();
  104. }
  105. }