vendor/ibexa/migrations/src/bundle/Event/ObjectRelationFieldTypeSubscriber.php line 59

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\Migration\Event;
  8. use Ibexa\Contracts\Core\Repository\ContentService;
  9. use Ibexa\Core\FieldType\Relation\Value;
  10. use Ibexa\Migration\Event\FieldValueFromHashEvent;
  11. use Ibexa\Migration\Event\HashFromFieldValueEvent;
  12. use Ibexa\Migration\Event\MigrationEvents;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. final class ObjectRelationFieldTypeSubscriber implements EventSubscriberInterface
  15. {
  16. private ContentService $contentService;
  17. public function __construct(ContentService $contentService)
  18. {
  19. $this->contentService = $contentService;
  20. }
  21. public static function getSubscribedEvents(): array
  22. {
  23. return [
  24. MigrationEvents::AFTER_HASH_FROM_FIELD_VALUE => [
  25. ['addRemoteContentId', -100],
  26. ],
  27. MigrationEvents::BEFORE_FIELD_VALUE_FROM_HASH => [
  28. ['convertRemoteContentId', -100],
  29. ],
  30. ];
  31. }
  32. public function addRemoteContentId(HashFromFieldValueEvent $event): void
  33. {
  34. $value = $event->getSpiFieldTypeValue();
  35. if (!$value instanceof Value) {
  36. return;
  37. }
  38. if ($value->destinationContentId === null) {
  39. return;
  40. }
  41. $hash = $event->getHash();
  42. $content = $this->contentService->loadContent($value->destinationContentId);
  43. $hash['contentRemoteId'] = $content->contentInfo->remoteId;
  44. $event->setHash($hash);
  45. }
  46. public function convertRemoteContentId(FieldValueFromHashEvent $event): void
  47. {
  48. if ($event->getFieldTypeIdentifier() !== 'ezobjectrelation') {
  49. return;
  50. }
  51. $hash = $event->getHash();
  52. if (!isset($hash['contentRemoteId'])) {
  53. return;
  54. }
  55. $content = $this->contentService->loadContentByRemoteId($hash['contentRemoteId']);
  56. $event->setHash(array_merge($hash, [
  57. 'destinationContentId' => $content->id,
  58. ]));
  59. }
  60. }