vendor/ibexa/activity-log/src/bundle/EventSubscriber/PostActivityListLoad/UserActivity.php line 36

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\Bundle\ActivityLog\EventSubscriber\PostActivityListLoad;
  8. use Ibexa\Contracts\ActivityLog\Event\PostActivityGroupListLoadEvent;
  9. use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
  10. use Ibexa\Contracts\Core\Repository\UserService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. final class UserActivity implements EventSubscriberInterface
  13. {
  14. private UserService $userService;
  15. public function __construct(
  16. UserService $userService
  17. ) {
  18. $this->userService = $userService;
  19. }
  20. public static function getSubscribedEvents(): array
  21. {
  22. return [
  23. PostActivityGroupListLoadEvent::class => ['loadUsers'],
  24. ];
  25. }
  26. /**
  27. * TODO: Handle lists in a more performant way.
  28. */
  29. public function loadUsers(PostActivityGroupListLoadEvent $event): void
  30. {
  31. // We will store User IDs that we have attempted to load to prevent loading the same object more than once.
  32. $visitedIds = [];
  33. $list = $event->getList();
  34. foreach ($list as $log) {
  35. $userId = $log->getUserId();
  36. try {
  37. if (!array_key_exists($userId, $visitedIds)) {
  38. $visitedIds[$userId] = $this->userService->loadUser($userId);
  39. }
  40. if ($visitedIds[$userId] === null) {
  41. continue;
  42. }
  43. $log->setUser($visitedIds[$userId]);
  44. } catch (NotFoundException $e) {
  45. // TODO: Check how to perform authorization checks
  46. // Log entries that have inaccessible components are expected
  47. $visitedIds[$userId] = null;
  48. }
  49. }
  50. }
  51. }