Skip to content

Commit

Permalink
NGSTACK-429: reuse existing methods and deprecate ID as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
pspanja committed Sep 14, 2020
1 parent 3ec212a commit 3c56bd4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 150 deletions.
51 changes: 1 addition & 50 deletions docs/reference/services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,55 +237,6 @@ Methods
:local:

``loadFieldRelation()``
.......................

Load single field relation from a specific field in a published version of a specific Content.

The method will return ``null`` if the field does not contain relations that can be loaded by the
current user. If the field contains multiple relations, the first one will be returned. The method
supports optional filtering by ContentType.

+----------------------------------------+------------------------------------------------------------------------------------+
| **Parameters** | 1. ``string|int $contentId`` |
| | 2. ``string $fieldDefinitionIdentifier`` |
| | 3. ``array $contentTypeIdentifiers = []`` |
+----------------------------------------+------------------------------------------------------------------------------------+
| **Returns** | :ref:`Content object<content_object>` or ``null`` |
+----------------------------------------+------------------------------------------------------------------------------------+
| **Example** | .. code-block:: php |
| | |
| | $content = $relationService->loadFieldRelation( |
| | 42, |
| | 'relations', |
| | ['articles'] |
| | ); |
| | |
+----------------------------------------+------------------------------------------------------------------------------------+

``loadFieldRelations()``
........................

Load all field relations from a specific field in a published version of a specific Content. The
method supports optional filtering by ContentType.

+----------------------------------------+------------------------------------------------------------------------------------+
| **Parameters** | 1. ``string|int $contentId`` |
| | 2. ``string $fieldDefinitionIdentifier`` |
| | 3. ``array $contentTypeIdentifiers = []`` |
+----------------------------------------+------------------------------------------------------------------------------------+
| **Returns** | An array of :ref:`Content objects<content_object>` |
+----------------------------------------+------------------------------------------------------------------------------------+
| **Example** | .. code-block:: php |
| | |
| | $contentItems = $relationService->loadFieldRelations( |
| | 42, |
| | 'relations', |
| | ['articles'] |
| | ); |
| | |
+----------------------------------------+------------------------------------------------------------------------------------+

``getFieldRelation()``
......................

Get single field relation from a specific field of a given Content.
Expand All @@ -311,7 +262,7 @@ supports optional filtering by ContentType.
| | |
+----------------------------------------+------------------------------------------------------------------------------------+

``getFieldRelations()``
``loadFieldRelations()``
.......................

Get all field relations from a specific field of a given Content. The method supports optional
Expand Down
42 changes: 4 additions & 38 deletions lib/API/RelationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface RelationService
* Load single related Content from $fieldDefinitionIdentifier field in Content with given
* $contentId, optionally limited by a list of $contentTypeIdentifiers.
*
* @param int|string $contentId
* @param \Netgen\EzPlatformSiteApi\API\Values\Content $content
* @param string $fieldDefinitionIdentifier
* @param array $contentTypeIdentifiers
*
Expand All @@ -25,7 +25,7 @@ interface RelationService
* @return null|\Netgen\EzPlatformSiteApi\API\Values\Content
*/
public function loadFieldRelation(
$contentId,
$content,
string $fieldDefinitionIdentifier,
array $contentTypeIdentifiers = []
): ?Content;
Expand All @@ -34,7 +34,7 @@ public function loadFieldRelation(
* Load all related Content from $fieldDefinitionIdentifier field in Content with given
* $contentId, optionally limited by a list of $contentTypeIdentifiers and $limit.
*
* @param int|string $contentId
* @param \Netgen\EzPlatformSiteApi\API\Values\Content $content
* @param string $fieldDefinitionIdentifier
* @param array $contentTypeIdentifiers
* @param null|int $limit
Expand All @@ -45,41 +45,7 @@ public function loadFieldRelation(
* @return \Netgen\EzPlatformSiteApi\API\Values\Content[]
*/
public function loadFieldRelations(
$contentId,
string $fieldDefinitionIdentifier,
array $contentTypeIdentifiers = [],
?int $limit = null
): array;

/**
* Get single related Content from $fieldDefinitionIdentifier field in $content,
* optionally limited by a list of $contentTypeIdentifiers.
*
* @param \Netgen\EzPlatformSiteApi\API\Values\Content $content
* @param string $fieldDefinitionIdentifier
* @param array $contentTypeIdentifiers
*
* @return null|\Netgen\EzPlatformSiteApi\API\Values\Content
*/
public function getFieldRelation(
Content $content,
string $fieldDefinitionIdentifier,
array $contentTypeIdentifiers = []
): ?Content;

/**
* Get all related Content from $fieldDefinitionIdentifier field in $content,
* optionally limited by a list of $contentTypeIdentifiers and $limit.
*
* @param \Netgen\EzPlatformSiteApi\API\Values\Content $content
* @param string $fieldDefinitionIdentifier
* @param array $contentTypeIdentifiers
* @param null|int $limit
*
* @return \Netgen\EzPlatformSiteApi\API\Values\Content[]
*/
public function getFieldRelations(
Content $content,
$content,
string $fieldDefinitionIdentifier,
array $contentTypeIdentifiers = [],
?int $limit = null
Expand Down
57 changes: 10 additions & 47 deletions lib/Core/Site/RelationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public function __construct(
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function loadFieldRelation(
$contentId,
$content,
string $fieldDefinitionIdentifier,
array $contentTypeIdentifiers = []
): ?Content {
$relatedContentItems = $this->loadFieldRelations(
$contentId,
$content,
$fieldDefinitionIdentifier,
$contentTypeIdentifiers
);
Expand All @@ -70,57 +70,20 @@ public function loadFieldRelation(
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function loadFieldRelations(
$contentId,
$content,
string $fieldDefinitionIdentifier,
array $contentTypeIdentifiers = [],
?int $limit = null
): array {
$content = $this->site->getLoadService()->loadContent($contentId);

$field = $content->getField($fieldDefinitionIdentifier);
$relationResolver = $this->relationResolverRegistry->get($field->fieldTypeIdentifier);

$relatedContentIds = $relationResolver->getRelationIds($field);
$relatedContentItems = $this->getRelatedContentItems(
$relatedContentIds,
$contentTypeIdentifiers,
$limit
);
$this->sortByIdOrder($relatedContentItems, $relatedContentIds);

return $relatedContentItems;
}

/**
* {@inheritdoc}
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function getFieldRelation(
Content $content,
string $fieldDefinitionIdentifier,
array $contentTypeIdentifiers = []
): ?Content {
$relatedContentItems = $this->getFieldRelations(
$content,
$fieldDefinitionIdentifier,
$contentTypeIdentifiers
);
if (!$content instanceof Content) {
@trigger_error(
'Using loadFieldRelations() with Content ID as the first argument is deprecated since version 3.5, and will be removed in 4.0. Provide Content instance instead.',
E_USER_DEPRECATED
);

return $relatedContentItems[0] ?? null;
}
$content = $this->site->getLoadService()->loadContent($content);
}

/**
* {@inheritdoc}
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function getFieldRelations(
Content $content,
string $fieldDefinitionIdentifier,
array $contentTypeIdentifiers = [],
?int $limit = null
): array {
$field = $content->getField($fieldDefinitionIdentifier);
$relationResolver = $this->relationResolverRegistry->get($field->fieldTypeIdentifier);

Expand Down
6 changes: 3 additions & 3 deletions lib/Core/Site/Values/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,15 @@ public function filterLocations(int $maxPerPage = 25, int $currentPage = 1): Pag

public function getFieldRelation(string $fieldDefinitionIdentifier): ?APIContent
{
return $this->site->getRelationService()->getFieldRelation(
return $this->site->getRelationService()->loadFieldRelation(
$this,
$fieldDefinitionIdentifier
);
}

public function getFieldRelations(string $fieldDefinitionIdentifier, int $limit = 25): array
{
return $this->site->getRelationService()->getFieldRelations(
return $this->site->getRelationService()->loadFieldRelations(
$this,
$fieldDefinitionIdentifier,
[],
Expand All @@ -341,7 +341,7 @@ public function filterFieldRelations(
int $maxPerPage = 25,
int $currentPage = 1
): Pagerfanta {
$relations = $this->site->getRelationService()->getFieldRelations(
$relations = $this->site->getRelationService()->loadFieldRelations(
$this,
$fieldDefinitionIdentifier,
$contentTypeIdentifiers
Expand Down
24 changes: 12 additions & 12 deletions tests/lib/Integration/RelationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ public function testLoadFieldRelationsForNonexistentField(): void
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testGetFieldRelation(): void
public function testLoadFieldRelationWithContent(): void
{
[$identifier, $testApiContent, $testRelationId] = $this->prepareTestContent();

$relationService = $this->getSite()->getRelationService();
$loadService = $this->getSite()->getLoadService();
$testSiteContent = $loadService->loadContent($testApiContent->id);
$content = $relationService->getFieldRelation($testSiteContent, $identifier);
$content = $relationService->loadFieldRelation($testSiteContent, $identifier);

$this->assertInstanceOf(Content::class, $content);
$this->assertEquals($testRelationId, $content->id);
Expand All @@ -188,14 +188,14 @@ public function testGetFieldRelation(): void
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testGetFieldRelations(): void
public function testLoadFieldRelationsWithContent(): void
{
[$identifier, , , $testApiContent, $testRelationIds] = $this->prepareTestContent();

$relationService = $this->getSite()->getRelationService();
$loadService = $this->getSite()->getLoadService();
$testSiteContent = $loadService->loadContent($testApiContent->id);
$contentItems = $relationService->getFieldRelations($testSiteContent, $identifier);
$contentItems = $relationService->loadFieldRelations($testSiteContent, $identifier);

$this->assertSameSize($testRelationIds, $contentItems);

Expand All @@ -216,14 +216,14 @@ public function testGetFieldRelations(): void
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testGetFieldRelationsWithTypeFilter(): void
public function testLoadFieldRelationsWithTypeFilterWithContent(): void
{
[$identifier, , , $testApiContent, $testRelationIds] = $this->prepareTestContent();

$relationService = $this->getSite()->getRelationService();
$loadService = $this->getSite()->getLoadService();
$testSiteContent = $loadService->loadContent($testApiContent->id);
$contentItems = $relationService->getFieldRelations($testSiteContent, $identifier, ['landing_page']);
$contentItems = $relationService->loadFieldRelations($testSiteContent, $identifier, ['landing_page']);

$this->assertCount(1, $contentItems);

Expand All @@ -241,14 +241,14 @@ public function testGetFieldRelationsWithTypeFilter(): void
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testGetFieldRelationsWithLimit(): void
public function testLoadFieldRelationsWithLimitWithContent(): void
{
[$identifier, , , $testApiContent, $testRelationIds] = $this->prepareTestContent();

$relationService = $this->getSite()->getRelationService();
$loadService = $this->getSite()->getLoadService();
$testSiteContent = $loadService->loadContent($testApiContent->id);
$contentItems = $relationService->getFieldRelations($testSiteContent, $identifier, [], 1);
$contentItems = $relationService->loadFieldRelations($testSiteContent, $identifier, [], 1);

$this->assertCount(1, $contentItems);

Expand All @@ -266,14 +266,14 @@ public function testGetFieldRelationsWithLimit(): void
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testGetFieldRelationsWithTypeFilterAndLimit(): void
public function testLoadFieldRelationsWithTypeFilterAndLimitWithContent(): void
{
[$identifier, , , $testApiContent, $testRelationIds] = $this->prepareTestContent();

$relationService = $this->getSite()->getRelationService();
$loadService = $this->getSite()->getLoadService();
$testSiteContent = $loadService->loadContent($testApiContent->id);
$contentItems = $relationService->getFieldRelations($testSiteContent, $identifier, ['feedback_form'], 1);
$contentItems = $relationService->loadFieldRelations($testSiteContent, $identifier, ['feedback_form'], 1);

$this->assertCount(1, $contentItems);

Expand All @@ -291,14 +291,14 @@ public function testGetFieldRelationsWithTypeFilterAndLimit(): void
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testGetFieldRelationsForNonexistentField(): void
public function testLoadFieldRelationsForNonexistentFieldWithContent(): void
{
[, , , $testApiContent] = $this->prepareTestContent();

$relationService = $this->getSite()->getRelationService();
$loadService = $this->getSite()->getLoadService();
$testSiteContent = $loadService->loadContent($testApiContent->id);
$contentItems = $relationService->getFieldRelations($testSiteContent, 'nonexistent');
$contentItems = $relationService->loadFieldRelations($testSiteContent, 'nonexistent');

$this->assertCount(0, $contentItems);
}
Expand Down

0 comments on commit 3c56bd4

Please sign in to comment.