Skip to content

Commit

Permalink
IBX-8006: Fixed matrix field resolving based on content's origin (#43)
Browse files Browse the repository at this point in the history
* IBX-8006: Fixed matrix field resolving based on content's origin

* added thrown exceptions
  • Loading branch information
konradoboza authored Mar 28, 2024
1 parent f3f6cbf commit bddfbfe
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 7 deletions.
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
},
"autoload": {
"psr-4": {
"EzSystems\\EzPlatformMatrixFieldtypeBundle\\": "src/bundle/",
"EzSystems\\EzPlatformMatrixFieldtype\\": "src/lib/",
"Ibexa\\FieldTypeMatrix\\": "src/lib/",
"Ibexa\\Bundle\\FieldTypeMatrix\\": "src/bundle/"
"Ibexa\\Bundle\\FieldTypeMatrix\\": "src/bundle/",
"EzSystems\\EzPlatformMatrixFieldtypeBundle\\": "src/bundle/",
"EzSystems\\EzPlatformMatrixFieldtype\\": "src/lib/"
}
},
"autoload-dev": {
Expand Down Expand Up @@ -57,5 +57,8 @@
"branch-alias": {
"dev-main": "4.5.x-dev"
}
},
"config": {
"allow-plugins": false
}
}
8 changes: 7 additions & 1 deletion src/bundle/Resources/config/services/graphql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ services:
- { name: ibexa.graphql.field_type.input.handler, fieldtype: ezmatrix }

Ibexa\FieldTypeMatrix\GraphQL\FieldValueResolver:
arguments:
$strategies: !tagged_iterator ibexa.graphql.field_type.matrix_resolver.content.strategy
tags:
- { name: overblog_graphql.resolver, alias: "MatrixFieldValue", method: "resolveMatrixFieldValue" }

Ibexa\FieldTypeMatrix\GraphQL\Strategy\ItemContentResolvingStrategy:
tags:
- {name: overblog_graphql.resolver, alias: "MatrixFieldValue", method: "resolveMatrixFieldValue"}
- { name: ibexa.graphql.field_type.matrix_resolver.content.strategy, priority: -20 }
37 changes: 34 additions & 3 deletions src/lib/GraphQL/FieldValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,48 @@

namespace Ibexa\FieldTypeMatrix\GraphQL;

use Ibexa\Core\Base\Exceptions\BadStateException;
use Ibexa\FieldTypeMatrix\FieldType\Value\RowsCollection;
use Ibexa\GraphQL\Value\Item;

class FieldValueResolver
{
public function resolveMatrixFieldValue(Item $item, string $fieldDefIdentifier): RowsCollection
/** @var iterable<\Ibexa\FieldTypeMatrix\GraphQL\Strategy\ContentResolvingStrategyInterface> */
private iterable $strategies;

/**
* @param iterable<\Ibexa\FieldTypeMatrix\GraphQL\Strategy\ContentResolvingStrategyInterface> $strategies
*/
public function __construct(iterable $strategies)
{
$this->strategies = $strategies;
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
*/
public function resolveMatrixFieldValue(object $item, string $fieldDefIdentifier): RowsCollection
{
$silentRows = [];
$content = null;

foreach ($this->strategies as $strategy) {
if (!$strategy->supports($item)) {
continue;
}

$content = $strategy->resolveContent($item);
}

if ($content === null) {
throw new BadStateException(
'$item',
'GraphQL item cannot be resolved to a content.'
);
}

/** @var \Ibexa\FieldTypeMatrix\FieldType\Value\RowsCollection $rows $rows */
$rows = $item->getContent()->getFieldValue($fieldDefIdentifier)->getRows();
$rows = $content->getFieldValue($fieldDefIdentifier)->getRows();
foreach ($rows as $row) {
$silentRows[] = new SilentRow($row->getCells());
}
Expand Down
18 changes: 18 additions & 0 deletions src/lib/GraphQL/Strategy/ContentResolvingStrategyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\FieldTypeMatrix\GraphQL\Strategy;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;

interface ContentResolvingStrategyInterface
{
public function resolveContent(object $item): Content;

public function supports(object $item): bool;
}
26 changes: 26 additions & 0 deletions src/lib/GraphQL/Strategy/ItemContentResolvingStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\FieldTypeMatrix\GraphQL\Strategy;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\GraphQL\Value\Item;

final class ItemContentResolvingStrategy implements ContentResolvingStrategyInterface
{
public function resolveContent(object $item): Content
{
/** @var \Ibexa\GraphQL\Value\Item $item */
return $item->getContent();
}

public function supports(object $item): bool
{
return $item instanceof Item;
}
}

0 comments on commit bddfbfe

Please sign in to comment.