Skip to content

Commit 897141f

Browse files
authored
IBX-2870: Implemented ImageAssetMapper strategy needed for DAM and GraphQL synchronization (#126)
* IBX-2870: Implemented ImageAssetMapper strategy * IBX-2870: Removed the strategy interface * IBX-2870: Moved Mapper to GraphQL bundle * IBX-2870: CS * IBX-2870: Applied review remarks
1 parent 47596d6 commit 897141f

File tree

5 files changed

+96
-25
lines changed

5 files changed

+96
-25
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
namespace Ibexa\GraphQL\GraphQL\Mapper;
8+
9+
use eZ\Publish\API\Repository\Values\Content\Field;
10+
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
11+
use eZ\Publish\Core\FieldType\ImageAsset;
12+
use EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\ContentLoader;
13+
14+
final class ContentImageAssetMapperStrategy implements ImageAssetMapperStrategyInterface
15+
{
16+
/* @var \eZ\Publish\Core\FieldType\ImageAsset\AssetMapper */
17+
private $assetMapper;
18+
19+
/** @var \EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\ContentLoader */
20+
private $contentLoader;
21+
22+
public function __construct(
23+
ImageAsset\AssetMapper $assetMapper,
24+
ContentLoader $contentLoader
25+
) {
26+
$this->assetMapper = $assetMapper;
27+
$this->contentLoader = $contentLoader;
28+
}
29+
30+
public function canProcess(ImageAsset\Value $value): bool
31+
{
32+
return $value->source === null;
33+
}
34+
35+
/**
36+
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
37+
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException|
38+
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
39+
*/
40+
public function process(ImageAsset\Value $value): Field
41+
{
42+
$assetField = $this->assetMapper->getAssetField(
43+
$this->contentLoader->findSingle(new Criterion\ContentId($value->destinationContentId))
44+
);
45+
46+
if (empty($assetField->value->alternativeText)) {
47+
$assetField->value->alternativeText = $value->alternativeText;
48+
}
49+
50+
return $assetField;
51+
}
52+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
namespace Ibexa\GraphQL\GraphQL\Mapper;
8+
9+
use eZ\Publish\API\Repository\Values\Content\Field;
10+
use eZ\Publish\Core\FieldType\ImageAsset;
11+
12+
interface ImageAssetMapperStrategyInterface
13+
{
14+
public function canProcess(ImageAsset\Value $value): bool;
15+
16+
public function process(ImageAsset\Value $value): Field;
17+
}

src/GraphQL/Resolver/ImageAssetFieldResolver.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,45 @@
66
*/
77
namespace EzSystems\EzPlatformGraphQL\GraphQL\Resolver;
88

9-
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
10-
use eZ\Publish\Core\FieldType\ImageAsset\AssetMapper;
11-
use EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\ContentLoader;
129
use EzSystems\EzPlatformGraphQL\GraphQL\Value\Field;
10+
use Ibexa\GraphQL\GraphQL\Mapper\ImageAssetMapperStrategyInterface;
1311

1412
/**
1513
* @internal
1614
*/
1715
class ImageAssetFieldResolver
1816
{
17+
/* @var array<\Ibexa\Core\FieldType\ImageAsset\ImageAssetMapperStrategyInterface> */
18+
private $strategies;
19+
1920
/**
20-
* @var DomainContentResolver
21-
*/
22-
private $domainContentResolver;
23-
/**
24-
* @var ContentLoader
25-
*/
26-
private $contentLoader;
27-
/**
28-
* @var AssetMapper
21+
* @param iterable<\Ibexa\Core\FieldType\ImageAsset\ImageAssetMapperStrategyInterface> $strategies
2922
*/
30-
private $assetMapper;
31-
32-
public function __construct(ContentLoader $contentLoader, DomainContentResolver $domainContentResolver, AssetMapper $assetMapper)
23+
public function __construct(iterable $strategies)
3324
{
34-
$this->domainContentResolver = $domainContentResolver;
35-
$this->contentLoader = $contentLoader;
36-
$this->assetMapper = $assetMapper;
25+
foreach ($strategies as $strategy) {
26+
if ($strategy instanceof ImageAssetMapperStrategyInterface) {
27+
$this->strategies[] = $strategy;
28+
}
29+
}
3730
}
3831

39-
public function resolveDomainImageAssetFieldValue(Field $field)
32+
public function resolveDomainImageAssetFieldValue(Field $field): ?Field
4033
{
4134
$destinationContentId = $field->value->destinationContentId;
4235

4336
if ($destinationContentId === null) {
4437
return null;
4538
}
4639

47-
$assetField = $this->assetMapper->getAssetField(
48-
$this->contentLoader->findSingle(new Criterion\ContentId($destinationContentId))
49-
);
40+
foreach ($this->strategies as $strategy) {
41+
if ($strategy->canProcess($field->value)) {
42+
$assetField = $strategy->process($field->value);
5043

51-
if (empty($assetField->value->alternativeText)) {
52-
$assetField->value->alternativeText = $field->value->alternativeText;
44+
return Field::fromField($assetField);
45+
}
5346
}
5447

55-
return Field::fromField($assetField);
48+
return null;
5649
}
5750
}

src/Resources/config/services/resolvers.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ services:
126126
- { name: overblog_graphql.resolver, alias: "DateTimeFormat", method: "resolveDateToFormat"}
127127

128128
EzSystems\EzPlatformGraphQL\GraphQL\Resolver\ImageAssetFieldResolver:
129+
arguments:
130+
$strategies: !tagged_iterator ibexa.field_type.image_asset.mapper.strategy
129131
tags:
130132
- { name: overblog_graphql.resolver, alias: "DomainImageAssetFieldValue", method: "resolveDomainImageAssetFieldValue"}
131133

src/Resources/config/services/services.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ services:
2121
EzSystems\EzPlatformGraphQL\Schema\Domain\Content\NameHelper:
2222
arguments:
2323
$fieldNameOverrides: '%ezplatform_graphql.schema.content.field_name.override%'
24+
25+
Ibexa\GraphQL\GraphQL\Mapper\ContentImageAssetMapperStrategy:
26+
arguments:
27+
$assetMapper: '@eZ\Publish\Core\FieldType\ImageAsset\AssetMapper'
28+
$contentLoader: '@EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\ContentLoader'
29+
tags:
30+
- { name: ibexa.field_type.image_asset.mapper.strategy, priority: 0 }

0 commit comments

Comments
 (0)