Skip to content

Commit

Permalink
IBX-9103: Added RelationType filtering to fetch relations methods (#440)
Browse files Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-9103 and #440

Key changes:

* Added RelationType for filtering purposes to fetch relations methods
  • Loading branch information
ViniTou authored Oct 23, 2024
1 parent 1a433a4 commit fed1a91
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 51 deletions.
25 changes: 12 additions & 13 deletions src/contracts/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Ibexa\Contracts\Core\Repository\Values\Content\LocationCreateStruct;
use Ibexa\Contracts\Core\Repository\Values\Content\Relation;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationType;
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\Filter\Filter;
Expand Down Expand Up @@ -419,7 +420,8 @@ public function loadRelations(VersionInfo $versionInfo): iterable;
public function loadRelationList(
VersionInfo $versionInfo,
int $offset = 0,
int $limit = self::DEFAULT_PAGE_SIZE
int $limit = self::DEFAULT_PAGE_SIZE,
?RelationType $type = null,
): RelationList;

/**
Expand All @@ -428,7 +430,7 @@ public function loadRelationList(
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
*/
public function countRelations(VersionInfo $versionInfo): int;
public function countRelations(VersionInfo $versionInfo, ?RelationType $type = null): int;

/**
* Counts all incoming relations for the given content object.
Expand All @@ -437,7 +439,7 @@ public function countRelations(VersionInfo $versionInfo): int;
*
* @return int The number of reverse relations ({@see \Ibexa\Contracts\Core\Repository\Values\Content\Relation})
*/
public function countReverseRelations(ContentInfo $contentInfo): int;
public function countReverseRelations(ContentInfo $contentInfo, ?RelationType $type = null): int;

/**
* Loads all incoming relations for a content object.
Expand All @@ -446,26 +448,23 @@ public function countReverseRelations(ContentInfo $contentInfo): int;
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $contentInfo
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Relation[]
*/
public function loadReverseRelations(ContentInfo $contentInfo): iterable;
public function loadReverseRelations(ContentInfo $contentInfo, ?RelationType $type = null): iterable;

/**
* Loads all incoming relations for a content object.
*
* The relations come only from published versions of the source content objects.
* If the user is not allowed to read specific version then UnauthorizedRelationListItem is returned
* {@see \Ibexa\Contracts\Core\Repository\Values\Content\RelationList\Item\UnauthorizedRelationListItem}
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $contentInfo
* @param int $offset
* @param int $limit
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\RelationList
*/
public function loadReverseRelationList(ContentInfo $contentInfo, int $offset = 0, int $limit = -1): RelationList;
public function loadReverseRelationList(
ContentInfo $contentInfo,
int $offset = 0,
int $limit = -1,
?RelationType $type = null
): RelationList;

/**
* Adds a relation of type common.
Expand Down
33 changes: 21 additions & 12 deletions src/contracts/Repository/Decorator/ContentServiceDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Ibexa\Contracts\Core\Repository\Values\Content\LocationCreateStruct;
use Ibexa\Contracts\Core\Repository\Values\Content\Relation;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationType;
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\Filter\Filter;
Expand Down Expand Up @@ -193,29 +194,37 @@ public function loadRelations(VersionInfo $versionInfo): iterable
return $this->innerService->loadRelations($versionInfo);
}

public function countRelations(VersionInfo $versionInfo): int
public function countRelations(VersionInfo $versionInfo, ?RelationType $type = null): int
{
return $this->innerService->countRelations($versionInfo);
return $this->innerService->countRelations($versionInfo, $type);
}

public function loadRelationList(VersionInfo $versionInfo, int $offset = 0, int $limit = self::DEFAULT_PAGE_SIZE): RelationList
{
return $this->innerService->loadRelationList($versionInfo, $offset, $limit);
public function loadRelationList(
VersionInfo $versionInfo,
int $offset = 0,
int $limit = self::DEFAULT_PAGE_SIZE,
?RelationType $type = null
): RelationList {
return $this->innerService->loadRelationList($versionInfo, $offset, $limit, $type);
}

public function countReverseRelations(ContentInfo $contentInfo): int
public function countReverseRelations(ContentInfo $contentInfo, ?RelationType $type = null): int
{
return $this->innerService->countReverseRelations($contentInfo);
return $this->innerService->countReverseRelations($contentInfo, $type);
}

public function loadReverseRelations(ContentInfo $contentInfo): iterable
public function loadReverseRelations(ContentInfo $contentInfo, ?RelationType $type = null): iterable
{
return $this->innerService->loadReverseRelations($contentInfo);
return $this->innerService->loadReverseRelations($contentInfo, $type);
}

public function loadReverseRelationList(ContentInfo $contentInfo, int $offset = 0, int $limit = -1): RelationList
{
return $this->innerService->loadReverseRelationList($contentInfo, $offset, $limit);
public function loadReverseRelationList(
ContentInfo $contentInfo,
int $offset = 0,
int $limit = -1,
?RelationType $type = null
): RelationList {
return $this->innerService->loadReverseRelationList($contentInfo, $offset, $limit, $type);
}

public function addRelation(
Expand Down
10 changes: 10 additions & 0 deletions src/contracts/Repository/Values/Content/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,44 @@ abstract class Relation extends ValueObject
* The relation type COMMON is a general relation between object set by a user.
*
* @var int
*
* @deprecated 5.0.0 const is deprecated and will be removed in 6.0.0. Use {@see RelationType::COMMON} instead.
*/
public const COMMON = 1;

/**
* the relation type EMBED is set for a relation which is anchored as embedded link in an attribute value.
*
* @var int
*
* @deprecated 5.0.0 const is deprecated and will be removed in 6.0.0. Use {@see RelationType::EMBED} instead.
*/
public const EMBED = 2;

/**
* the relation type LINK is set for a relation which is anchored as link in an attribute value.
*
* @var int
*
* @deprecated 5.0.0 const is deprecated and will be removed in 6.0.0. Use {@see RelationType::LINK} instead.
*/
public const LINK = 4;

/**
* the relation type FIELD is set for a relation which is part of an relation attribute value.
*
* @var int
*
* @deprecated 5.0.0 const is deprecated and will be removed in 6.0.0. Use {@see RelationType::FIELD} instead.
*/
public const FIELD = 8;

/**
* the relation type ASSET is set for a relation to asset in an attribute value.
*
* @var int
*
* @deprecated 5.0.0 const is deprecated and will be removed in 6.0.0. Use {@see RelationType::ASSET} instead.
*/
public const ASSET = 16;

Expand Down
37 changes: 37 additions & 0 deletions src/contracts/Repository/Values/Content/RelationType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\Contracts\Core\Repository\Values\Content;

enum RelationType: int
{
/**
* The relation type COMMON is a general relation between object set by a user.
*/
case COMMON = 1;

/**
* the relation type EMBED is set for a relation which is anchored as embedded link in an attribute value.
*/
case EMBED = 2;

/**
* the relation type LINK is set for a relation which is anchored as link in an attribute value.
*/
case LINK = 4;

/**
* the relation type FIELD is set for a relation which is part of an relation attribute value.
*/
case FIELD = 8;

/**
* the relation type ASSET is set for a relation to asset in an attribute value.
*/
case ASSET = 16;
}
48 changes: 33 additions & 15 deletions src/lib/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList\Item\RelationListItem;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList\Item\UnauthorizedRelationListItem;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationType;
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo as APIVersionInfo;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\Filter\Filter;
Expand Down Expand Up @@ -2045,7 +2046,7 @@ protected function internalLoadRelations(APIVersionInfo $versionInfo): array
return $relations;
}

public function countRelations(APIVersionInfo $versionInfo): int
public function countRelations(APIVersionInfo $versionInfo, ?RelationType $type = null): int
{
$function = $versionInfo->isPublished() ? 'read' : 'versionread';

Expand All @@ -2057,12 +2058,17 @@ public function countRelations(APIVersionInfo $versionInfo): int

return $this->persistenceHandler->contentHandler()->countRelations(
$contentInfo->id,
$versionInfo->versionNo
$versionInfo->versionNo,
$type?->value
);
}

public function loadRelationList(APIVersionInfo $versionInfo, int $offset = 0, int $limit = self::DEFAULT_PAGE_SIZE): RelationList
{
public function loadRelationList(
APIVersionInfo $versionInfo,
int $offset = 0,
int $limit = self::DEFAULT_PAGE_SIZE,
?RelationType $type = null
): RelationList {
$function = $versionInfo->isPublished() ? 'read' : 'versionread';

$list = new RelationList();
Expand All @@ -2074,7 +2080,8 @@ public function loadRelationList(APIVersionInfo $versionInfo, int $offset = 0, i
$contentInfo = $versionInfo->getContentInfo();
$list->totalCount = $this->persistenceHandler->contentHandler()->countRelations(
$contentInfo->id,
$versionInfo->versionNo
$versionInfo->versionNo,
$type?->value
);

if ($list->totalCount === 0) {
Expand All @@ -2086,6 +2093,7 @@ public function loadRelationList(APIVersionInfo $versionInfo, int $offset = 0, i
$limit,
$offset,
$versionInfo->versionNo,
$type?->value
);

$destinationContentIds = array_column($persistenceRelationList, 'destinationContentId');
Expand Down Expand Up @@ -2120,13 +2128,13 @@ public function loadRelationList(APIVersionInfo $versionInfo, int $offset = 0, i
/**
* {@inheritdoc}
*/
public function countReverseRelations(ContentInfo $contentInfo): int
public function countReverseRelations(ContentInfo $contentInfo, ?RelationType $type = null): int
{
if (!$this->permissionResolver->canUser('content', 'reverserelatedlist', $contentInfo)) {
return 0;
}

return $this->persistenceHandler->contentHandler()->countReverseRelations($contentInfo->getId());
return $this->persistenceHandler->contentHandler()->countReverseRelations($contentInfo->getId(), $type?->value);
}

/**
Expand All @@ -2136,18 +2144,17 @@ public function countReverseRelations(ContentInfo $contentInfo): int
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $contentInfo
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Relation[]
*/
public function loadReverseRelations(ContentInfo $contentInfo): iterable
public function loadReverseRelations(ContentInfo $contentInfo, ?RelationType $type = null): iterable
{
if (!$this->permissionResolver->canUser('content', 'reverserelatedlist', $contentInfo)) {
throw new UnauthorizedException('content', 'reverserelatedlist', ['contentId' => $contentInfo->id]);
}

$spiRelations = $this->persistenceHandler->contentHandler()->loadReverseRelations(
$contentInfo->id
$contentInfo->id,
$type?->value
);

$returnArray = [];
Expand All @@ -2169,22 +2176,33 @@ public function loadReverseRelations(ContentInfo $contentInfo): iterable

/**
* {@inheritdoc}
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $contentInfo
* @param int $offset
* @param int $limit
* @param \Ibexa\Contracts\Core\Repository\Values\Content\RelationType|null $type
*/
public function loadReverseRelationList(ContentInfo $contentInfo, int $offset = 0, int $limit = -1): RelationList
{
public function loadReverseRelationList(
ContentInfo $contentInfo,
int $offset = 0,
int $limit = -1,
?RelationType $type = null
): RelationList {
$list = new RelationList();
if (!$this->repository->getPermissionResolver()->canUser('content', 'reverserelatedlist', $contentInfo)) {
return $list;
}

$list->totalCount = $this->persistenceHandler->contentHandler()->countReverseRelations(
$contentInfo->id
$contentInfo->id,
$type?->value
);
if ($list->totalCount > 0) {
$spiRelationList = $this->persistenceHandler->contentHandler()->loadReverseRelationList(
$contentInfo->id,
$offset,
$limit
$limit,
$type?->value
);
foreach ($spiRelationList as $spiRelation) {
$sourceContentInfo = $this->internalLoadContentInfoById($spiRelation->sourceContentId);
Expand Down
Loading

0 comments on commit fed1a91

Please sign in to comment.