diff --git a/src/Builder/AlternativeLookup.php b/src/Builder/AlternativeLookup.php index c3cee87..7c5e33b 100644 --- a/src/Builder/AlternativeLookup.php +++ b/src/Builder/AlternativeLookup.php @@ -14,8 +14,7 @@ final class AlternativeLookup implements Builder public function __construct( private readonly Builder $capacity, - ) { - } + ) {} public function withMerge(Builder $merge): self { diff --git a/src/Builder/Capacity/Extractor/All.php b/src/Builder/Capacity/Extractor/All.php index 495cb91..0e1eb8a 100644 --- a/src/Builder/Capacity/Extractor/All.php +++ b/src/Builder/Capacity/Extractor/All.php @@ -13,11 +13,8 @@ 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|string $type = null; - - public function __construct() - { - } + private null|Node\Expr $referenceEntity = null; + private null|Node\Expr $referenceEntityAttributeCode = null; public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { @@ -40,9 +37,16 @@ public function withCode(?Node\Expr $code): self return $this; } - public function withType(string $type): self + public function withReferenceEntityCode(?Node\Expr $referenceEntity): self + { + $this->referenceEntity = $referenceEntity; + + return $this; + } + + public function withReferenceEntityAttributeOption(?Node\Expr $referenceEntityAttrbuteCode): self { - $this->type = $type; + $this->referenceEntityAttributeCode = $referenceEntityAttrbuteCode; return $this; } @@ -64,23 +68,7 @@ public function getNode(): Node name: $this->endpoint ), name: new Node\Identifier('all'), - args: array_filter( - [ - new Node\Arg( - value: new Node\Expr\Array_( - items: $this->compileSearch(), - attributes: [ - 'kind' => Node\Expr\Array_::KIND_SHORT, - ] - ), - name: new Node\Identifier('queryParameters'), - ), - null !== $this->code ? new Node\Arg( - value: $this->code, - name: $this->compileCodeNamedArgument($this->type), - ) : null, - ], - ), + args: $this->compileArguments(), ), valueVar: new Node\Expr\Variable('item'), subNodes: [ @@ -116,12 +104,43 @@ private function compileSearch(): array ]; } - private function compileCodeNamedArgument(string $type): Node\Identifier + private function compileArguments(): array { - return match ($type) { - 'assetManager' => new Node\Identifier('assetFamilyCode'), - 'referenceEntityRecord' => new Node\Identifier('referenceEntityCode'), - default => new Node\Identifier('attributeCode') - }; + $args = []; + + if (null !== $this->search) { + $args[] = new Node\Arg( + value: new Node\Expr\Array_( + items: $this->compileSearch(), + attributes: [ + 'kind' => Node\Expr\Array_::KIND_SHORT, + ] + ), + name: new Node\Identifier('queryParameters'), + ); + } + + if (null !== $this->code) { + $args[] = new Node\Arg( + value: $this->code, + name: new Node\Identifier('attributeCode'), + ); + } + + if (null !== $this->referenceEntity) { + $args[] = new Node\Arg( + value: $this->referenceEntity, + name: new Node\Identifier('referenceEntityCode'), + ); + } + + if (null !== $this->referenceEntity) { + $args[] = new Node\Arg( + value: $this->referenceEntityAttributeCode, + name: new Node\Identifier('attributeCode'), + ); + } + + return $args; } } diff --git a/src/Builder/Capacity/Extractor/Get.php b/src/Builder/Capacity/Extractor/Get.php index 5925c17..8816611 100644 --- a/src/Builder/Capacity/Extractor/Get.php +++ b/src/Builder/Capacity/Extractor/Get.php @@ -13,9 +13,7 @@ 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() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Capacity/Extractor/ListPerPage.php b/src/Builder/Capacity/Extractor/ListPerPage.php index 9ffed46..ea019f2 100644 --- a/src/Builder/Capacity/Extractor/ListPerPage.php +++ b/src/Builder/Capacity/Extractor/ListPerPage.php @@ -14,9 +14,7 @@ final class ListPerPage implements Builder private null|Node\Expr $search = null; private null|Node\Expr $code = null; - public function __construct() - { - } + public function __construct() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Capacity/Lookup/All.php b/src/Builder/Capacity/Lookup/All.php index c6e1104..f4d1718 100644 --- a/src/Builder/Capacity/Lookup/All.php +++ b/src/Builder/Capacity/Lookup/All.php @@ -15,9 +15,7 @@ final class All implements Builder private null|Node\Expr $code = null; private string $type = ''; - public function __construct() - { - } + public function __construct() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Capacity/Lookup/Download.php b/src/Builder/Capacity/Lookup/Download.php index f665dfc..4e0673a 100644 --- a/src/Builder/Capacity/Lookup/Download.php +++ b/src/Builder/Capacity/Lookup/Download.php @@ -14,9 +14,7 @@ final class Download implements Builder private null|Node\Expr|Node\Identifier $endpoint = null; private null|Node\Expr $file = null; - public function __construct() - { - } + public function __construct() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Capacity/Lookup/Get.php b/src/Builder/Capacity/Lookup/Get.php index 9a3c138..33f1dcf 100644 --- a/src/Builder/Capacity/Lookup/Get.php +++ b/src/Builder/Capacity/Lookup/Get.php @@ -15,9 +15,7 @@ final class Get implements Builder private null|Node\Expr $code = null; private null|string $type = ''; - public function __construct() - { - } + public function __construct() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Capacity/Lookup/ListPerPage.php b/src/Builder/Capacity/Lookup/ListPerPage.php index f8ee225..2b5fce0 100644 --- a/src/Builder/Capacity/Lookup/ListPerPage.php +++ b/src/Builder/Capacity/Lookup/ListPerPage.php @@ -14,9 +14,7 @@ final class ListPerPage implements Builder private null|Node\Expr $search = null; private null|Node\Expr $code = null; - public function __construct() - { - } + public function __construct() {} public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self { diff --git a/src/Builder/Client.php b/src/Builder/Client.php index a143a3f..3293aa5 100644 --- a/src/Builder/Client.php +++ b/src/Builder/Client.php @@ -19,9 +19,7 @@ final class Client implements Builder private ?Node\Expr $httpStreamFactory = null; private ?Node\Expr $fileSystem = null; - public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret) - { - } + public function __construct(private readonly Node\Expr $baseUrl, private readonly Node\Expr $clientId, private readonly Node\Expr $secret) {} public function withToken(Node\Expr $token, Node\Expr $refreshToken): self { diff --git a/src/Builder/ConditionalLookup.php b/src/Builder/ConditionalLookup.php index 3b6bc65..2ca716b 100644 --- a/src/Builder/ConditionalLookup.php +++ b/src/Builder/ConditionalLookup.php @@ -14,9 +14,7 @@ final class ConditionalLookup implements StepBuilderInterface private iterable $alternatives = []; private ?Node\Expr $client = null; - public function __construct() - { - } + public function __construct() {} public function withClient(Node\Expr $client): self { diff --git a/src/Builder/Extractor.php b/src/Builder/Extractor.php index 8651637..ed74d05 100644 --- a/src/Builder/Extractor.php +++ b/src/Builder/Extractor.php @@ -15,8 +15,7 @@ final class Extractor implements StepBuilderInterface public function __construct( private readonly Builder $capacity, - ) { - } + ) {} public function withClient(Node\Expr $client): self { diff --git a/src/Builder/Inline.php b/src/Builder/Inline.php index abcafcd..4cb0d5d 100644 --- a/src/Builder/Inline.php +++ b/src/Builder/Inline.php @@ -13,9 +13,7 @@ final readonly class Inline implements Builder { - public function __construct(private ArrayBuilderInterface|ObjectBuilderInterface $mapper) - { - } + public function __construct(private ArrayBuilderInterface|ObjectBuilderInterface $mapper) {} public function getNode(): Node { diff --git a/src/Builder/Loader.php b/src/Builder/Loader.php index d5d4eb8..99e9b4f 100644 --- a/src/Builder/Loader.php +++ b/src/Builder/Loader.php @@ -15,8 +15,7 @@ final class Loader implements StepBuilderInterface public function __construct( private readonly Builder $capacity, - ) { - } + ) {} public function withClient(Node\Expr $client): self { diff --git a/src/Builder/Lookup.php b/src/Builder/Lookup.php index bd3a004..77800da 100644 --- a/src/Builder/Lookup.php +++ b/src/Builder/Lookup.php @@ -12,9 +12,7 @@ final class Lookup implements StepBuilderInterface private ?Node\Expr $logger = null; private ?Node\Expr $client = null; - public function __construct(private readonly AlternativeLookup $alternative) - { - } + public function __construct(private readonly AlternativeLookup $alternative) {} public function withClient(Node\Expr $client): self { diff --git a/src/Builder/Search.php b/src/Builder/Search.php index c618535..528f63b 100644 --- a/src/Builder/Search.php +++ b/src/Builder/Search.php @@ -11,8 +11,7 @@ final class Search implements Builder { public function __construct( private array $filters = [] - ) { - } + ) {} public function addFilter( Node\Expr $field, diff --git a/src/Capacity/Extractor/All.php b/src/Capacity/Extractor/All.php index 0b73402..47cdf98 100644 --- a/src/Capacity/Extractor/All.php +++ b/src/Capacity/Extractor/All.php @@ -55,9 +55,7 @@ final class All implements Akeneo\Capacity\CapacityInterface 'UNCLASSIFIED', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { @@ -94,17 +92,25 @@ public function getBuilder(array $config): Builder { $builder = (new Akeneo\Builder\Capacity\Extractor\All()) ->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type'])))) - ->withType($config['type']) ; - if (isset($config['search']) && \is_array($config['search'])) { + if (isset($config['search']) && \is_array($config['search']) && \count($config['search']) > 0) { $builder->withSearch($this->compileFilters(...$config['search'])); } - if (\in_array($config['type'], ['attributeOption', 'assetManager', 'referenceEntityRecord']) && \array_key_exists('code', $config)) { + if (\in_array($config['type'], ['attributeOption', 'assetManager']) && \array_key_exists('code', $config)) { $builder->withCode(compileValueWhenExpression($this->interpreter, $config['code'])); } + if ('referenceEntityRecord' == $config['type']) { + $builder->withReferenceEntityCode(compileValueWhenExpression($this->interpreter, $config['reference_entity'])); + } + + if ('referenceEntityAttributeOption' == $config['type']) { + $builder->withReferenceEntityCode(compileValueWhenExpression($this->interpreter, $config['reference_entity'])); + $builder->withReferenceEntityAttributeOption(compileValueWhenExpression($this->interpreter, $config['reference_entity_option'])); + } + return $builder; } } diff --git a/src/Capacity/Extractor/Get.php b/src/Capacity/Extractor/Get.php index f7f897e..a6a0b2d 100644 --- a/src/Capacity/Extractor/Get.php +++ b/src/Capacity/Extractor/Get.php @@ -43,9 +43,7 @@ final class Get implements Akeneo\Capacity\CapacityInterface 'assetManager', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { diff --git a/src/Capacity/Extractor/ListPerPage.php b/src/Capacity/Extractor/ListPerPage.php index 644d94b..a743b1e 100644 --- a/src/Capacity/Extractor/ListPerPage.php +++ b/src/Capacity/Extractor/ListPerPage.php @@ -53,9 +53,7 @@ final class ListPerPage implements Akeneo\Capacity\CapacityInterface 'UNCLASSIFIED', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { diff --git a/src/Capacity/Loader/Upsert.php b/src/Capacity/Loader/Upsert.php index 5f939b9..f5b983d 100644 --- a/src/Capacity/Loader/Upsert.php +++ b/src/Capacity/Loader/Upsert.php @@ -45,9 +45,7 @@ final class Upsert implements Akeneo\Capacity\CapacityInterface 'referenceEntity', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { diff --git a/src/Capacity/Lookup/All.php b/src/Capacity/Lookup/All.php index 2ac7f6f..bc6637b 100644 --- a/src/Capacity/Lookup/All.php +++ b/src/Capacity/Lookup/All.php @@ -54,9 +54,7 @@ final class All implements Akeneo\Capacity\CapacityInterface 'UNCLASSIFIED', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { diff --git a/src/Capacity/Lookup/Download.php b/src/Capacity/Lookup/Download.php index fa46a6c..1a15cde 100644 --- a/src/Capacity/Lookup/Download.php +++ b/src/Capacity/Lookup/Download.php @@ -22,9 +22,7 @@ final class Download implements Akeneo\Capacity\CapacityInterface 'assetMediaFile', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { diff --git a/src/Capacity/Lookup/Get.php b/src/Capacity/Lookup/Get.php index 5336e60..6be374d 100644 --- a/src/Capacity/Lookup/Get.php +++ b/src/Capacity/Lookup/Get.php @@ -43,9 +43,7 @@ final class Get implements Akeneo\Capacity\CapacityInterface 'assetManager', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { diff --git a/src/Capacity/Lookup/ListPerPage.php b/src/Capacity/Lookup/ListPerPage.php index 4f02b46..5ee4f69 100644 --- a/src/Capacity/Lookup/ListPerPage.php +++ b/src/Capacity/Lookup/ListPerPage.php @@ -53,9 +53,7 @@ final class ListPerPage implements Akeneo\Capacity\CapacityInterface 'UNCLASSIFIED', ]; - public function __construct(private readonly ExpressionLanguage $interpreter) - { - } + public function __construct(private readonly ExpressionLanguage $interpreter) {} public function applies(array $config): bool { diff --git a/src/Configuration/Extractor.php b/src/Configuration/Extractor.php index 3e2c7db..f9ce045 100644 --- a/src/Configuration/Extractor.php +++ b/src/Configuration/Extractor.php @@ -160,7 +160,7 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder ->validate() ->ifTrue(fn ($data) => \array_key_exists('code', $data) && \array_key_exists('type', $data) - && !\in_array($data['type'], ['attributeOption', 'referenceEntityRecord', 'assetManager'], true)) + && !\in_array($data['type'], ['attributeOption', 'assetManager'], true)) ->thenInvalid('The code option should only be used with the "attributeOption" and "assetManager" endpoints.') ->end() ->validate() @@ -173,6 +173,30 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder ->ifTrue(fn ($data) => \array_key_exists('identifier', $data) && \array_key_exists('method', $data) && 'get' !== $data['method']) ->thenInvalid('The identifier option should only be used with the "get" method.') ->end() + ->validate() + ->ifTrue(fn ($data) => \array_key_exists('reference_entity', $data) + && \array_key_exists('type', $data) + && !\in_array($data['type'], ['referenceEntity', 'referenceEntityAttributeOption'], true)) + ->thenInvalid('The reference_entity option should only be used with the "referenceEntity" and "referenceEntityAttributeOption" endpoints.') + ->end() + ->validate() + ->ifTrue(fn ($data) => \array_key_exists('reference_entity_option', $data) + && \array_key_exists('type', $data) + && !\in_array($data['type'], ['referenceEntityAttributeOption'], true)) + ->thenInvalid('The reference_entity_option option should only be used with the "referenceEntityAttributeOption" endpoint.') + ->end() + ->validate() + ->ifTrue(fn ($data) => !\array_key_exists('reference_entity', $data) + && \array_key_exists('type', $data) + && \in_array($data['type'], ['referenceEntityRecord', 'referenceEntityAttributeOption'], true)) + ->thenInvalid('The reference_entity option should be used with the "referenceEntityRecord" and "referenceEntityAttributeOption" endpoints.') + ->end() + ->validate() + ->ifTrue(fn ($data) => !\array_key_exists('reference_entity_option', $data) + && \array_key_exists('type', $data) + && \in_array($data['type'], ['referenceEntityAttributeOption'], true)) + ->thenInvalid('The reference_entity option should be used with the "referenceEntityAttributeOption" endpoint.') + ->end() ->children() ->scalarNode('type') ->isRequired() @@ -202,6 +226,18 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder ->then(asExpression()) ->end() ->end() + ->scalarNode('reference_entity') + ->validate() + ->ifTrue(isExpression()) + ->then(asExpression()) + ->end() + ->end() + ->scalarNode('reference_entity_option') + ->validate() + ->ifTrue(isExpression()) + ->then(asExpression()) + ->end() + ->end() ->append((new Search())->getConfigTreeBuilder()->getRootNode()) ->end() ; diff --git a/src/Factory/NoApplicableCapacityException.php b/src/Factory/NoApplicableCapacityException.php index 779c4c4..38ef19c 100644 --- a/src/Factory/NoApplicableCapacityException.php +++ b/src/Factory/NoApplicableCapacityException.php @@ -4,6 +4,4 @@ namespace Kiboko\Plugin\Akeneo\Factory; -final class NoApplicableCapacityException extends \OutOfRangeException -{ -} +final class NoApplicableCapacityException extends \OutOfRangeException {} diff --git a/src/MissingAuthenticationMethodException.php b/src/MissingAuthenticationMethodException.php index 58d3fd6..c666271 100644 --- a/src/MissingAuthenticationMethodException.php +++ b/src/MissingAuthenticationMethodException.php @@ -4,6 +4,4 @@ namespace Kiboko\Plugin\Akeneo; -final class MissingAuthenticationMethodException extends \RuntimeException -{ -} +final class MissingAuthenticationMethodException extends \RuntimeException {} diff --git a/src/MissingEndpointException.php b/src/MissingEndpointException.php index 37d752f..04cc6c2 100644 --- a/src/MissingEndpointException.php +++ b/src/MissingEndpointException.php @@ -4,6 +4,4 @@ namespace Kiboko\Plugin\Akeneo; -final class MissingEndpointException extends \RuntimeException -{ -} +final class MissingEndpointException extends \RuntimeException {} diff --git a/src/MissingParameterException.php b/src/MissingParameterException.php index 7375c9f..b7f1de9 100644 --- a/src/MissingParameterException.php +++ b/src/MissingParameterException.php @@ -4,6 +4,4 @@ namespace Kiboko\Plugin\Akeneo; -final class MissingParameterException extends \UnexpectedValueException -{ -} +final class MissingParameterException extends \UnexpectedValueException {}