vendor/ibexa/activity-log/src/bundle/EventSubscriber/PostActivityListLoad/ContentActivity.php line 42

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\ContentService;
  10. use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
  11. use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
  12. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  13. use Psr\Log\LoggerAwareInterface;
  14. use Psr\Log\LoggerAwareTrait;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. final class ContentActivity implements EventSubscriberInterface, LoggerAwareInterface
  17. {
  18. use LoggerAwareTrait;
  19. private ContentService $contentService;
  20. public function __construct(
  21. ContentService $contentService
  22. ) {
  23. $this->contentService = $contentService;
  24. }
  25. public static function getSubscribedEvents(): array
  26. {
  27. return [
  28. PostActivityGroupListLoadEvent::class => ['loadContent'],
  29. ];
  30. }
  31. /**
  32. * TODO: Handle lists in a more performant way.
  33. */
  34. public function loadContent(PostActivityGroupListLoadEvent $event): void
  35. {
  36. // We will store Content IDs that we have attempted to load to prevent loading the same object more than once.
  37. $visitedIds = [];
  38. $list = $event->getList();
  39. foreach ($list as $group) {
  40. foreach ($group->getActivityLogs() as $log) {
  41. if ($log->getObjectClass() !== Content::class) {
  42. continue;
  43. }
  44. $contentId = $log->getObjectId();
  45. try {
  46. if (!array_key_exists($contentId, $visitedIds)) {
  47. $visitedIds[$contentId] = $this->loadContentById($contentId);
  48. }
  49. if ($visitedIds[$contentId] === null) {
  50. continue;
  51. }
  52. $log->setRelatedObject($visitedIds[$contentId]);
  53. } catch (NotFoundException|UnauthorizedException $e) {
  54. // Log entry contains relationship to an object that has been deleted, current user does not have
  55. // access to it, or is otherwise not reachable.
  56. $visitedIds[$contentId] = null;
  57. }
  58. }
  59. }
  60. }
  61. /**
  62. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  63. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  64. */
  65. private function loadContentById(string $contentId): ?Content
  66. {
  67. if (is_numeric($contentId) && $contentId == (int)$contentId) {
  68. return $this->contentService->loadContent((int)$contentId);
  69. }
  70. if (isset($this->logger)) {
  71. $this->logger->warning(sprintf(
  72. 'Failed to load Content using ID: "%s". Content ID has to be an integerish value.',
  73. $contentId,
  74. ));
  75. }
  76. return null;
  77. }
  78. }