Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved extractor options in the configuration #54

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/Builder/Capacity/Extractor/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ final class All implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $search = null;
private null|Node\Expr $code = null;
private null|Node\Expr $attributeCode = null;
private null|Node\Expr $assetFamilyCode = null;
private null|Node\Expr $file = null;
private null|string $type = null;

public function __construct()
Expand All @@ -33,9 +35,23 @@ public function withSearch(Node\Expr $search): self
return $this;
}

public function withCode(?Node\Expr $code): self
public function withAttributeOption(?Node\Expr $attributeCode): self
{
$this->code = $code;
$this->attributeCode = $attributeCode;

return $this;
}

public function withAssetManager(?Node\Expr $assetFamilyCode): self
{
$this->assetFamilyCode = $assetFamilyCode;

return $this;
}

public function withFile(Node\Expr $file): self
{
$this->file = $file;

return $this;
}
Expand Down Expand Up @@ -75,8 +91,16 @@ public function getNode(): Node
),
name: new Node\Identifier('queryParameters'),
),
null !== $this->code ? new Node\Arg(
value: $this->code,
null !== $this->attributeCode ? new Node\Arg(
value: $this->attributeCode,
name: $this->compileCodeNamedArgument($this->type),
) : null,
null !== $this->assetFamilyCode ? new Node\Arg(
value: $this->assetFamilyCode,
name: $this->compileCodeNamedArgument($this->type),
) : null,
null !== $this->file ? new Node\Arg(
value: $this->file,
name: $this->compileCodeNamedArgument($this->type),
) : null,
],
Expand Down
24 changes: 5 additions & 19 deletions src/Builder/Capacity/Extractor/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

namespace Kiboko\Plugin\Akeneo\Builder\Capacity\Extractor;

use Kiboko\Plugin\Akeneo\Handler\EndpointArgumentHandlerInterface;
use Kiboko\Plugin\Akeneo\MissingEndpointException;
use PhpParser\Builder;
use PhpParser\Node;

final class Get implements Builder
{
private null|Node\Expr|Node\Identifier $endpoint = null;
private null|Node\Expr $identifier = null;

public function __construct()
{
public function __construct(
private readonly EndpointArgumentHandlerInterface $handler,
) {
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
Expand All @@ -24,13 +25,6 @@ public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
return $this;
}

public function withIdentifier(?Node\Expr $identifier): self
{
$this->identifier = $identifier;

return $this;
}

public function getNode(): Node
{
if (null === $this->endpoint) {
Expand All @@ -52,16 +46,8 @@ class: new Node\Name\FullyQualified(name: \Kiboko\Component\Bucket\AcceptanceRes
name: $this->endpoint
),
name: new Node\Identifier('get'),
args: array_filter(
[
new Node\Arg(
value: $this->identifier,
name: new Node\Identifier('code'),
),
],
),
args: $this->handler->compileEndpointArguments(),
),
unpack: true,
),
],
),
Expand Down
23 changes: 15 additions & 8 deletions src/Capacity/Extractor/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

use function Kiboko\Component\SatelliteToolbox\Configuration\compileValue;
use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression;

final class All implements Akeneo\Capacity\CapacityInterface
Expand Down Expand Up @@ -79,11 +78,11 @@ private function compileFilters(array ...$filters): Node\Expr
}

$builder->addFilter(
field: compileValue($this->interpreter, $filter['field']),
operator: compileValue($this->interpreter, $filter['operator']),
value: \array_key_exists('value', $filter) ? compileValue($this->interpreter, $filter['value']) : null,
scope: \array_key_exists('scope', $filter) ? compileValue($this->interpreter, $filter['scope']) : null,
locale: \array_key_exists('locale', $filter) ? compileValue($this->interpreter, $filter['locale']) : null
field: compileValueWhenExpression($this->interpreter, $filter['field']),
operator: compileValueWhenExpression($this->interpreter, $filter['operator']),
value: \array_key_exists('value', $filter) ? compileValueWhenExpression($this->interpreter, $filter['value']) : null,
scope: \array_key_exists('scope', $filter) ? compileValueWhenExpression($this->interpreter, $filter['scope']) : null,
locale: \array_key_exists('locale', $filter) ? compileValueWhenExpression($this->interpreter, $filter['locale']) : null
);
}

Expand All @@ -101,8 +100,16 @@ public function getBuilder(array $config): Builder
$builder->withSearch($this->compileFilters(...$config['search']));
}

if (\in_array($config['type'], ['attributeOption', 'assetManager', 'referenceEntityRecord']) && \array_key_exists('code', $config)) {
$builder->withCode(compileValueWhenExpression($this->interpreter, $config['code']));
if (\in_array($config['type'], ['attributeOption'])) {
$builder->withAttributeOption(compileValueWhenExpression($this->interpreter, $config['attribute_code']));
}

if (\in_array($config['type'], ['assetManager'])) {
$builder->withAssetManager(compileValueWhenExpression($this->interpreter, $config['asset_family_code']));
}

if (\in_array($config['type'], ['productMediaFile', 'assetMediaFile'])) {
$builder->withFile(compileValueWhenExpression($this->interpreter, $config['file']));
}

return $builder;
Expand Down
18 changes: 7 additions & 11 deletions src/Capacity/Extractor/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use Kiboko\Plugin\Akeneo;
use PhpParser\Builder;
use PhpParser\Node;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression;

final class Get implements Akeneo\Capacity\CapacityInterface
{
Expand Down Expand Up @@ -43,8 +40,9 @@ final class Get implements Akeneo\Capacity\CapacityInterface
'assetManager',
];

public function __construct(private readonly ExpressionLanguage $interpreter)
{
public function __construct(
private readonly Akeneo\Handler\EndpointHandlerFactoryInterface $factory,
) {
}

public function applies(array $config): bool
Expand All @@ -57,12 +55,10 @@ public function applies(array $config): bool

public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Extractor\Get())
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
return (new Akeneo\Builder\Capacity\Extractor\Get($this->factory->create($config['type'], $config)))
->withEndpoint(
new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))),
)
;

$builder->withIdentifier(compileValueWhenExpression($this->interpreter, $config['identifier']));

return $builder;
}
}
80 changes: 68 additions & 12 deletions src/Configuration/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,64 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
/* @phpstan-ignore-next-line */
$builder->getRootNode()
->validate()
->ifArray()
->then(function (array $item) {
if (!\in_array($item['method'], self::$endpoints[$item['type']])) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
}

return $item;
})
->ifTrue(fn ($data) => (\array_key_exists('asset_family_code', $data) || \array_key_exists('asset_code', $data))
&& \array_key_exists('type', $data)
&& !\in_array($data['type'], ['assetManager'], true))
->thenInvalid('The asset_family_code and asset_code options should only be used with the "assetManager" endpoint.')
->end()
->validate()
->ifTrue(fn ($data) => (!\array_key_exists('asset_family_code', $data) || !\array_key_exists('asset_code', $data))
&& 'get' === $data['method']
&& \array_key_exists('type', $data)
&& \in_array($data['type'], ['assetManager'], true))
->thenInvalid('The asset_family_code and the asset_code options should be used with the "assetManager" endpoint.')
->end()
->validate()
->ifTrue(fn ($data) => \array_key_exists('code', $data)
->ifTrue(fn ($data) => !\array_key_exists('asset_family_code', $data)
&& 'all' === $data['method']
&& \array_key_exists('type', $data)
&& \in_array($data['type'], ['assetManager'], true))
->thenInvalid('The asset_family_code option should be used with the "assetManager" endpoint.')
->end()
->validate()
->ifTrue(fn ($data) => (\array_key_exists('attribute_code', $data) || \array_key_exists('code', $data))
&& 'get' === $data['method']
&& \array_key_exists('type', $data)
&& !\in_array($data['type'], ['attributeOption', 'referenceEntityRecord', 'assetManager'], true))
->thenInvalid('The code option should only be used with the "attributeOption" and "assetManager" endpoints.')
&& !\in_array($data['type'], ['attributeOption'], true))
->thenInvalid('The attribute_code and code options should only be used with the "attributeOption" endpoint.')
->end()
->validate()
->ifTrue(fn ($data) => \array_key_exists('attribute_code', $data)
&& 'all' === $data['method']
&& \array_key_exists('type', $data)
&& !\in_array($data['type'], ['attributeOption'], true))
->thenInvalid('The attribute_code option should only be used with the "attributeOption" endpoint.')
->end()
->validate()
->ifTrue(fn ($data) => (!\array_key_exists('attribute_code', $data) || !\array_key_exists('code', $data))
&& 'get' === $data['method']
&& \array_key_exists('type', $data)
&& \in_array($data['type'], ['attributeOption'], true))
->thenInvalid('The attribute_code and code options should be used with the "attributeOption" endpoint.')
->end()
->validate()
->ifTrue(fn ($data) => !\array_key_exists('attribute_code', $data)
&& 'all' === $data['method']
&& \array_key_exists('type', $data)
&& \in_array($data['type'], ['attributeOption'], true))
->thenInvalid('The attribute_code option should be used with the "attributeOption" endpoint.')
->end()
->validate()
->ifTrue(fn ($data) => \array_key_exists('file', $data)
&& \array_key_exists('type', $data)
&& !\in_array($data['type'], ['productMediaFile', 'assetMediaFile'], true))
->thenInvalid('The file option should only be used with the "productMediaFile" endpoint.')
->thenInvalid('The file option should only be used with the "productMediaFile" and the "assetMediaFile" endpoints.')
->end()
->validate()
->ifTrue(fn ($data) => !\array_key_exists('file', $data)
&& \array_key_exists('type', $data)
&& \in_array($data['type'], ['productMediaFile', 'assetMediaFile'], true))
->thenInvalid('The file option should be used with the "productMediaFile" and the "assetMediaFile" endpoints.')
->end()
->validate()
->ifTrue(fn ($data) => \array_key_exists('identifier', $data) && \array_key_exists('method', $data) && 'get' !== $data['method'])
Expand Down Expand Up @@ -202,6 +240,24 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
->then(asExpression())
->end()
->end()
->scalarNode('attribute_code')
->validate()
->ifTrue(isExpression())
->then(asExpression())
->end()
->end()
->scalarNode('asset_family_code')
->validate()
->ifTrue(isExpression())
->then(asExpression())
->end()
->end()
->scalarNode('asset_code')
->validate()
->ifTrue(isExpression())
->then(asExpression())
->end()
->end()
->append((new Search())->getConfigTreeBuilder()->getRootNode())
->end()
;
Expand Down
16 changes: 16 additions & 0 deletions src/DTO/GetAssetManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Akeneo\DTO;

use PhpParser\Node;

final class GetAssetManager
{
public function __construct(
public Node\Expr $assetFamilyCode,
public Node\Expr $assetCode,
) {
}
}
16 changes: 16 additions & 0 deletions src/DTO/GetAttributeOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Akeneo\DTO;

use PhpParser\Node;

final class GetAttributeOption
{
public function __construct(
public Node\Expr $attributeCode,
public Node\Expr $code,
) {
}
}
15 changes: 15 additions & 0 deletions src/DTO/GetDefaultEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Akeneo\DTO;

use PhpParser\Node;

final class GetDefaultEndpoint
{
public function __construct(
public Node\Expr $identifier,
) {
}
}
4 changes: 3 additions & 1 deletion src/Factory/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public function __construct(
$this->configuration = new Akeneo\Configuration\Extractor();
$this->capacities = [
new Akeneo\Capacity\Extractor\All($this->interpreter),
new Akeneo\Capacity\Extractor\Get($this->interpreter),
new Akeneo\Capacity\Extractor\Get(
new Akeneo\Handler\GetEndpointHandlerFactory($this->interpreter),
),
new Akeneo\Capacity\Extractor\ListPerPage($this->interpreter),
];
}
Expand Down
10 changes: 10 additions & 0 deletions src/Handler/EndpointArgumentHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Akeneo\Handler;

interface EndpointArgumentHandlerInterface
{
public function compileEndpointArguments(): array;
}
10 changes: 10 additions & 0 deletions src/Handler/EndpointHandlerFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Akeneo\Handler;

interface EndpointHandlerFactoryInterface
{
public function create(string $endpointType, array $config): EndpointArgumentHandlerInterface;
}
Loading