vendor/ibexa/segmentation/src/lib/Event/Subscriber/PersonalizationUserSegmentsSubscriber.php line 51

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\Segmentation\Event\Subscriber;
  8. use Ibexa\Contracts\Core\Repository\Repository;
  9. use Ibexa\Contracts\Segmentation\SegmentationServiceInterface;
  10. use Ibexa\Personalization\Event\PersonalizationSegmentsResolverEvent;
  11. use Ibexa\Personalization\Event\PersonalizationUserTrackingRenderOptionsEvent;
  12. use Ibexa\Segmentation\Exception\Persistence\SegmentNotFoundException;
  13. use Ibexa\Segmentation\Value\Segment;
  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 PersonalizationUserSegmentsSubscriber implements EventSubscriberInterfaceLoggerAwareInterface
  20. {
  21.     use LoggerAwareTrait;
  22.     private const SEGMENTS_KEY 'segments';
  23.     private Repository $repository;
  24.     private SegmentationServiceInterface $segmentationService;
  25.     public function __construct(
  26.         Repository $repository,
  27.         SegmentationServiceInterface $segmentationService,
  28.         ?LoggerInterface $logger null
  29.     ) {
  30.         $this->repository $repository;
  31.         $this->segmentationService $segmentationService;
  32.         $this->logger $logger ?? new NullLogger();
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             PersonalizationUserTrackingRenderOptionsEvent::class => ['onUserTrackingRender'255],
  38.             PersonalizationSegmentsResolverEvent::class => ['onSegmentsResolver'255],
  39.         ];
  40.     }
  41.     public function onUserTrackingRender(PersonalizationUserTrackingRenderOptionsEvent $event): void
  42.     {
  43.         $segments array_column(
  44.             $this->segmentationService->loadSegmentsAssignedToCurrentUser(),
  45.             'id'
  46.         );
  47.         $previousSegments $event->getOptions()[self::SEGMENTS_KEY] ?? [];
  48.         $event->setOption(self::SEGMENTS_KEYarray_merge($previousSegments$segments));
  49.     }
  50.     public function onSegmentsResolver(PersonalizationSegmentsResolverEvent $event): void
  51.     {
  52.         $resultMapping $event->getSegmentsMapping();
  53.         foreach ($event->getSegmentsIds() as $id) {
  54.             try {
  55.                 $resultMapping[$id] = $this->repository->sudo(
  56.                     fn (): Segment => $this->segmentationService->loadSegment($id)
  57.                 );
  58.             } catch (SegmentNotFoundException $e) {
  59.                 $this->logger->debug(sprintf('Segment not found with id: %s'$id));
  60.             }
  61.         }
  62.         $event->setSegmentsMapping($resultMapping);
  63.     }
  64. }