vendor/ibexa/personalization/src/lib/Event/Subscriber/UserCollectionGeneratorEventSubscriber.php line 43

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\Personalization\Event\Subscriber;
  8. use Ibexa\Bundle\Personalization\Serializer\Normalizer\AttributeNormalizer;
  9. use Ibexa\Bundle\Personalization\Serializer\Normalizer\UserCollectionNormalizer;
  10. use Ibexa\Bundle\Personalization\Serializer\Normalizer\UserNormalizer;
  11. use Ibexa\Personalization\Event\GenerateUserCollectionDataEvent;
  12. use Ibexa\Personalization\Event\UpdateUserAPIEvent;
  13. use Ibexa\Personalization\Value\Output\UserCollection;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  16. use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
  17. use Symfony\Component\Serializer\Serializer;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. final class UserCollectionGeneratorEventSubscriber implements EventSubscriberInterface
  20. {
  21.     /** @var \Symfony\Component\EventDispatcher\EventDispatcher */
  22.     private $eventDispatcher;
  23.     public function __construct(EventDispatcherInterface $eventDispatcher)
  24.     {
  25.         $this->eventDispatcher $eventDispatcher;
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             UpdateUserAPIEvent::class => ['onRecommendationUpdateUserCollection'128],
  34.         ];
  35.     }
  36.     public function onRecommendationUpdateUserCollection(UpdateUserAPIEvent $userAPIEvent): void
  37.     {
  38.         $event = new GenerateUserCollectionDataEvent();
  39.         $this->eventDispatcher->dispatch($event);
  40.         $userCollection $event->getUserCollection();
  41.         if ($userCollection->isEmpty()) {
  42.             return;
  43.         }
  44.         $userApiRequest $userAPIEvent->getUserAPIRequest();
  45.         if ($event->hasUserCollectionName()) {
  46.             $userApiRequest->source $event->getUserCollectionName();
  47.         }
  48.         $userApiRequest->xmlBody $this->generateXml($userCollection);
  49.     }
  50.     /**
  51.      * Generates xml string based on UserCollection object.
  52.      */
  53.     private function generateXml(UserCollection $userCollection): string
  54.     {
  55.         $encoders = [new XmlEncoder()];
  56.         $normalizers = [
  57.             new UserCollectionNormalizer(),
  58.             new UserNormalizer(),
  59.             new AttributeNormalizer(),
  60.             new ArrayDenormalizer(),
  61.         ];
  62.         $serializer = new Serializer($normalizers$encoders);
  63.         return $serializer->serialize(
  64.             $userCollection,
  65.             'xml',
  66.             [
  67.             'xml_root_node_name' => 'users',
  68.             ]
  69.         );
  70.     }
  71. }
  72. class_alias(UserCollectionGeneratorEventSubscriber::class, 'EzSystems\EzRecommendationClient\Event\Subscriber\UserCollectionGeneratorEventSubscriber');