From cd8c979ab1b32a7360130d3b362753b3bd5c62ac Mon Sep 17 00:00:00 2001 From: Dawid Parafinski Date: Thu, 24 Oct 2024 14:27:06 +0200 Subject: [PATCH] IBX-8534: Dropped deprecated Relation related methods usage --- .../Adapter/RelationListIteratorAdapter.php | 42 +++++++++++++++++++ .../Content/ContentHaveAssetRelation.php | 12 +----- .../Content/ContentHaveUniqueRelation.php | 21 +++++++--- 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 src/lib/Pagination/Adapter/RelationListIteratorAdapter.php diff --git a/src/lib/Pagination/Adapter/RelationListIteratorAdapter.php b/src/lib/Pagination/Adapter/RelationListIteratorAdapter.php new file mode 100644 index 0000000000..0568016aef --- /dev/null +++ b/src/lib/Pagination/Adapter/RelationListIteratorAdapter.php @@ -0,0 +1,42 @@ +contentService->loadRelationList( + $this->versionInfo, + $offset, + $limit, + $this->relationType + )->getIterator(); + + if ($iterator instanceof Iterator) { + return $iterator; + } + + return new IteratorIterator($iterator); + } +} diff --git a/src/lib/Specification/Content/ContentHaveAssetRelation.php b/src/lib/Specification/Content/ContentHaveAssetRelation.php index 746777d747..f602a9ae4d 100644 --- a/src/lib/Specification/Content/ContentHaveAssetRelation.php +++ b/src/lib/Specification/Content/ContentHaveAssetRelation.php @@ -11,8 +11,8 @@ use Ibexa\AdminUi\Exception\InvalidArgumentException; use Ibexa\Contracts\Core\Repository\ContentService; use Ibexa\Contracts\Core\Repository\Values\Content\Content; +use Ibexa\Contracts\Core\Repository\Values\Content\RelationType; use Ibexa\Contracts\Core\Specification\AbstractSpecification; -use Ibexa\Core\Repository\Values\Content\Relation; class ContentHaveAssetRelation extends AbstractSpecification { @@ -41,14 +41,6 @@ public function isSatisfiedBy($item): bool throw new InvalidArgumentException($item, sprintf('Must be an instance of %s', Content::class)); } - $relations = $this->contentService->loadRelations($item->versionInfo); - - foreach ($relations as $relation) { - if (Relation::ASSET === $relation->type) { - return true; - } - } - - return false; + return $this->contentService->countRelations($item->versionInfo, RelationType::ASSET) > 0; } } diff --git a/src/lib/Specification/Content/ContentHaveUniqueRelation.php b/src/lib/Specification/Content/ContentHaveUniqueRelation.php index 0cab15f362..5be72756f5 100644 --- a/src/lib/Specification/Content/ContentHaveUniqueRelation.php +++ b/src/lib/Specification/Content/ContentHaveUniqueRelation.php @@ -9,10 +9,12 @@ namespace Ibexa\AdminUi\Specification\Content; use Ibexa\AdminUi\Exception\InvalidArgumentException; +use Ibexa\AdminUi\Pagination\Adapter\RelationListIteratorAdapter; use Ibexa\Contracts\Core\Repository\ContentService; +use Ibexa\Contracts\Core\Repository\Iterator\BatchIterator; use Ibexa\Contracts\Core\Repository\Values\Content\Content; +use Ibexa\Contracts\Core\Repository\Values\Content\RelationType; use Ibexa\Contracts\Core\Specification\AbstractSpecification; -use Ibexa\Core\Repository\Values\Content\Relation; class ContentHaveUniqueRelation extends AbstractSpecification { @@ -41,12 +43,21 @@ public function isSatisfiedBy($item): bool throw new InvalidArgumentException($item, sprintf('Must be an instance of %s', Content::class)); } - $relations = $this->contentService->loadRelations($item->versionInfo); + $relationListIterator = new BatchIterator( + new RelationListIteratorAdapter( + $this->contentService, + $item->getVersionInfo(), + RelationType::ASSET + ) + ); - foreach ($relations as $relation) { - if (Relation::ASSET === $relation->type) { + /** @var \Ibexa\Contracts\Core\Repository\Values\Content\RelationList\Item\RelationListItem $relationItem */ + foreach ($relationListIterator as $relationItem) { + if ($relationItem->hasRelation()) { + /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Relation $relation */ + $relation = $relationItem->getRelation(); $relationsFromAssetSide = $this->contentService->countReverseRelations( - $relation->destinationContentInfo + $relation->getDestinationContentInfo() ); if ($relationsFromAssetSide > 1) {