Skip to content

Commit

Permalink
IBX-8534: Dropped deprecated Relation related methods usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ViniTou committed Nov 5, 2024
1 parent 2f9029c commit cd8c979
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
42 changes: 42 additions & 0 deletions src/lib/Pagination/Adapter/RelationListIteratorAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Pagination\Adapter;

use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Iterator\BatchIteratorAdapter;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationType;
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;
use Iterator;
use IteratorIterator;

final class RelationListIteratorAdapter implements BatchIteratorAdapter
{
public function __construct(
readonly private ContentService $contentService,
readonly private VersionInfo $versionInfo,
readonly private ?RelationType $relationType = null,
) {
}

public function fetch(int $offset, int $limit): Iterator
{
$iterator = $this->contentService->loadRelationList(
$this->versionInfo,
$offset,
$limit,
$this->relationType
)->getIterator();

if ($iterator instanceof Iterator) {
return $iterator;
}

return new IteratorIterator($iterator);
}
}
12 changes: 2 additions & 10 deletions src/lib/Specification/Content/ContentHaveAssetRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
21 changes: 16 additions & 5 deletions src/lib/Specification/Content/ContentHaveUniqueRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cd8c979

Please sign in to comment.