vendor/ibexa/personalization/src/lib/Event/Subscriber/LocationEventSubscriber.php line 101

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\Contracts\Core\Repository\ContentService as CoreContentService;
  9. use Ibexa\Contracts\Core\Repository\Events\Location\CopySubtreeEvent;
  10. use Ibexa\Contracts\Core\Repository\Events\Location\CreateLocationEvent;
  11. use Ibexa\Contracts\Core\Repository\Events\Location\HideLocationEvent;
  12. use Ibexa\Contracts\Core\Repository\Events\Location\MoveSubtreeEvent;
  13. use Ibexa\Contracts\Core\Repository\Events\Location\SwapLocationEvent;
  14. use Ibexa\Contracts\Core\Repository\Events\Location\UnhideLocationEvent;
  15. use Ibexa\Contracts\Core\Repository\Events\Location\UpdateLocationEvent;
  16. use Ibexa\Contracts\Core\Repository\LocationService;
  17. use Ibexa\Contracts\Core\Repository\Repository;
  18. use Ibexa\Contracts\Core\Repository\SearchService;
  19. use Ibexa\Core\Query\QueryFactoryInterface;
  20. use Ibexa\Personalization\Config\ItemType\IncludedItemTypeResolverInterface;
  21. use Ibexa\Personalization\Event\PersonalizationEvent;
  22. use Ibexa\Personalization\Service\Content\ContentServiceInterface;
  23. use Ibexa\Personalization\Service\Setting\SettingServiceInterface;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. final class LocationEventSubscriber extends AbstractRepositoryEventSubscriber implements EventSubscriberInterface
  26. {
  27. private SettingServiceInterface $settingService;
  28. public function __construct(
  29. CoreContentService $coreContentService,
  30. ContentServiceInterface $contentService,
  31. IncludedItemTypeResolverInterface $includedItemTypeResolver,
  32. LocationService $locationService,
  33. Repository $repository,
  34. SettingServiceInterface $settingService,
  35. QueryFactoryInterface $queryFactory,
  36. SearchService $searchService
  37. ) {
  38. parent::__construct(
  39. $coreContentService,
  40. $contentService,
  41. $includedItemTypeResolver,
  42. $locationService,
  43. $repository,
  44. $queryFactory,
  45. $searchService
  46. );
  47. $this->settingService = $settingService;
  48. }
  49. public static function getSubscribedEvents(): array
  50. {
  51. return [
  52. CopySubtreeEvent::class => ['onCopySubtree', PersonalizationEvent::DEFAULT_PRIORITY],
  53. CreateLocationEvent::class => ['onCreateLocation', PersonalizationEvent::DEFAULT_PRIORITY],
  54. HideLocationEvent::class => ['onHideLocation', PersonalizationEvent::DEFAULT_PRIORITY],
  55. MoveSubtreeEvent::class => ['onMoveSubtree', PersonalizationEvent::DEFAULT_PRIORITY],
  56. SwapLocationEvent::class => ['onSwapLocation', PersonalizationEvent::DEFAULT_PRIORITY],
  57. UnhideLocationEvent::class => ['onUnhideLocation', PersonalizationEvent::DEFAULT_PRIORITY],
  58. UpdateLocationEvent::class => ['onUpdateLocation', PersonalizationEvent::DEFAULT_PRIORITY],
  59. ];
  60. }
  61. public function onCopySubtree(CopySubtreeEvent $event): void
  62. {
  63. if (!$this->settingService->isInstallationKeyFound()) {
  64. return;
  65. }
  66. $this->updateLocationWithChildren($event->getLocation());
  67. }
  68. public function onCreateLocation(CreateLocationEvent $event): void
  69. {
  70. if (!$this->settingService->isInstallationKeyFound()) {
  71. return;
  72. }
  73. $content = $event->getLocation()->getContent();
  74. if (!$this->includedItemTypeResolver->isContentIncluded($content)) {
  75. return;
  76. }
  77. $this->contentService->updateContent($content);
  78. }
  79. public function onHideLocation(HideLocationEvent $event): void
  80. {
  81. if (!$this->settingService->isInstallationKeyFound()) {
  82. return;
  83. }
  84. $this->deleteLocationWithChildren($event->getLocation());
  85. }
  86. public function onMoveSubtree(MoveSubtreeEvent $event): void
  87. {
  88. if (!$this->settingService->isInstallationKeyFound()) {
  89. return;
  90. }
  91. $this->updateLocationWithChildren($event->getLocation());
  92. }
  93. public function onSwapLocation(SwapLocationEvent $event): void
  94. {
  95. if (!$this->settingService->isInstallationKeyFound()) {
  96. return;
  97. }
  98. $this->updateLocationsWithChildren(
  99. [
  100. $event->getLocation1(),
  101. $event->getLocation2(),
  102. ]
  103. );
  104. }
  105. public function onUnhideLocation(UnhideLocationEvent $event): void
  106. {
  107. if (!$this->settingService->isInstallationKeyFound()) {
  108. return;
  109. }
  110. $this->updateLocationWithChildren($event->getLocation(), true);
  111. }
  112. public function onUpdateLocation(UpdateLocationEvent $event): void
  113. {
  114. if (!$this->settingService->isInstallationKeyFound()) {
  115. return;
  116. }
  117. $this->updateLocationWithChildren($event->getLocation());
  118. }
  119. }
  120. class_alias(LocationEventSubscriber::class, 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber');